is:cos:centos服务搭建-01-php环境

CentOS 7.0 (64位)服务器环境搭建(Apache+MariaDB+PHP)

  • 关闭firewall服务
    # systemctl stop firewalld.service 禁止firewall开机启动
    # systemctl disable firewalld.service
    # yum install iptables-services 安装iptables防火墙
    
  • 编辑防火墙配置文件
    # vim /etc/sysconfig/iptables
    # 按i进入insert模式,添加如下内容
    -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
    -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
    # 然后按ESC退出INSERT模式,输入:wq保存并退出
    # systemctl restart iptables.service 重启防火墙使配置生效
    # systemctl enable iptables.service 设置防火墙开机启动
    
  • 关闭SELINUX
    # vim /etc/selinux/config
    # 按i进入INSERT模式,
    # 将SELINUXTYPE=targeted  #注释掉
    # 按ESC退出,输入:wq保存并退出。
    
  • SELinux完全关闭
    # setenforce 0 使配置立即生效
    # 注:
    setenforce 1 设置SELinux 成为enforcing模式
    setenforce 0 设置SELinux 成为permissive模式
    
  • 安装Apache
    # yum install httpd
    # 可能会用到的命令:
    systemctl start httpd.service #启动apache
    systemctl stop httpd.service #停止apache
    systemctl restart httpd.service #重启apache
    systemctl enable httpd.service #设置apache开机启动
    
  • 重启Apache:重启后,在浏览器输入域名或IP地址,若是本机可以输入localhost进行测试
    # systemctl enable httpd.service 设置Apache开机启动
    
  • 安装MariaDB
    # yum install mariadb mariadb-server 注:CentOS 7.0中,已经使用MariaDB替代了MySQL数据库
    # 可能会用到的命令:
    systemctl start mariadb.service #启动MariaDB
    systemctl stop mariadb.service #停止MariaDB
    systemctl restart mariadb.service #重启MariaDB
    systemctl enable mariadb.service #设置开机启动
    
  • 拷贝配置文件
    # cp /usr/share/mysql/my-huge.cnf /etc/my.cnf 拷贝配置文件
    # 注:拷贝配置文件(注意:如果/etc目录下面默认有一个my.cnf,直接覆盖即可)
    # systemctl restart mariadb.service 重启MariaDB
    # 若出现异常,说明MariaDB未启动,可按CTRL+C退出,并启动数据库。
    
  • 设置MariaDB root密码
    # mysql_secure_installation 为root设置密码
    # 此处直接按回车即可(一开始没有密码,以后改密码则需要输入已设定的密码)
    # 然后会要求你输入两次密码,然后回车
    # 然后一路按Y加回车
    # 直到出现Thanks for using MariaDB
    # systemctl restart mariadb.service 重启MariaDB
    # systemctl enable mariadb.service 设置为开机启动
    
  • 安装PHP及组件
    # yum install php
    # yum install php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-bcmath php-mhash 安装PHP组件,使PHP支持 MariaDB
    
  • 重启服务
    # systemctl restart mariadb.service
    # systemctl restart httpd.service
    
  • 编辑Apache配置文件
    # vim /etc/httpd/conf/httpd.conf 编辑配置文件
    # 按i进去insert模式
    # 按照以下要求修改配置文件:
    ServerSignature On  #添加,在错误页中显示Apache的版本,Off为不显示
    AllowOverride None  #修改为:AllowOverride All (允许.htaccess)
    Options Indexes FollowSymLinks(部分可能为Options None)  #修改为:Options Includes ExecCGI FollowSymLinks(允许服务器执行CGI及SSI,禁止列出目录)
    #AddHandler cgi-script .cgi #修改为:AddHandler cgi-script .cgi .pl (允许扩展名为.pl的CGI脚本运行)
    AddDefaultCharset UTF-8 #修改为:AddDefaultCharset GB2312 (添加GB2312为默认编码)
    DirectoryIndex index.html   #修改为:DirectoryIndex index.html index.htm Default.html Default.htm index.php(设置默认首页文件,增加index.php)
    MaxKeepAliveRequests 50  #添加MaxKeepAliveRequests 50 (增加同时连接数) 若有域名,可以在ServerName处把域名改为自己的域名。
    # 最后按ESC退出insert模式,输入:wq保存并退出
    # systemctl restart httpd.service 重启Apache
    # rm -f /etc/httpd/conf.d/welcome.conf /var/www/error/noindex.html 删除默认测试首页(可忽略此步)
    
  • 编辑PHP配置文件
    # vim /etc/php.ini 编辑配置文件
    # 按i进入insert模式
    # 按以下要求进行修改(列出PHP可以禁用的函数,如果某些程序需要用到这个函数,可以删除,取消禁用。)
    date.timezone = PRC #把前面的分号去掉,改为date.timezone = PRC
    disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid, posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname
    expose_php = Off #禁止显示php版本的信息
    short_open_tag = ON #支持php短标签
    open_basedir = .:/tmp/  #设置表示允许访问当前目录(即PHP脚本文件所在之目录)和/tmp/目录,可以防止php木马跨站,如果改了之后安装程序有问题(例如:织梦内容管理系统),可以注销此行,或者直接写上程序的目录/data/www.osyunwei.com/:/tmp/
    # 按ESC退出insert模式,输入:wq退出并保存
    
  • 重启服务
    # systemctl restart mariadb.service
    # systemctl restart httpd.service
    
  • 创建测试文件
    # cd /var/www/html 进入服务器的根目录(默认是var/www/html,可在配置文件中修改)
    # vim index.php 创建index.php文件
    # 写入以下内容
    <?php
    phpinfo();
    ?>
    # 按ESC退出insert模式,输入:wq保存并退出
    
  • 在浏览器输入服务器ip或域名,本机可输入localhost,出现如下图所示结果则为成功

查看示例

  • is/cos/centos服务搭建-01-php环境.txt
  • 最后更改: 2025/02/17 04:03
  • A.L