CentOS服务搭建-01-PHP环境

来自DeerGrove Wiki
Aker讨论 | 贡献2020年6月3日 (三) 11:18的版本 (创建页面,内容为“category:IS CentOS 7.0 (64位)服务器环境搭建(Apache+MariaDB+PHP) CentOS 7默认使用的是Firewall作为防火墙,将其改为iptables防火墙…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航搜索


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

CentOS 7默认使用的是Firewall作为防火墙,将其改为iptables防火墙 关闭firewall服务 命令:systemctl stop firewalld.service

禁止firewall开机启动

命令:systemctl disable firewalld.service


安装iptables防火墙 命令:yum install iptables-services


编辑防火墙配置文件 命令: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保存并退出 注:若与截图不相同,可按以上要求修改配置文件

使配置立即生效 命令:setenforce 0 截图说明,SELinux已完全关闭 注: 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进行测试 出现如下截图,说明已经安装成功。


设置Apache开机启动 命令:systemctl enable httpd.service


安装数据库 命令: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,直接覆盖即可)


重启MariaDB 命令:systemctl restart mariadb.service 若出现以下情况,说明MariaDB未启动,可按CTRL+C退出,并启动数据库。


为root设置密码 命令:mysql_secure_installation 此处直接按回车即可(一开始没有密码,以后改密码则需要输入已设定的密码) 然后会要求你输入两次密码,然后回车 然后一路按Y加回车 直到出现Thanks for using MariaDB


重启MariaDB,并设置为开机启动 命令:systemctl restart mariadb.service 命令:systemctl enable mariadb.service

安装PHP 命令:yum install php

安装PHP组件,使PHP支持 MariaDB 命令:yum install php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-bcmath php-mhash

重启Apache以及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,禁止列出目录)

  1. 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保存并退出


重启Apache 命令:systemctl restart httpd.service


删除默认测试首页(可忽略此步) 命令:rm -f /etc/httpd/conf.d/welcome.conf /var/www/error/noindex.html


配置PHP 编辑配置文件 命令:vim /etc/php.ini 按i进入insert模式 按以下要求进行修改 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

  1. 列出PHP可以禁用的函数,如果某些程序需要用到这个函数,可以删除,取消禁用。

expose_php = Off #禁止显示php版本的信息 short_open_tag = ON #支持php短标签 open_basedir = .:/tmp/ #设置表示允许访问当前目录(即PHP脚本文件所在之目录)和/tmp/目录,可以防止php木马跨站,如果改了之后安装程序有问题(例如:织梦内容管理系统),可以注销此行,或者直接写上程序的目录/data/www.osyunwei.com/:/tmp/ 按ESC退出insert模式,输入:wq退出并保存


重启Apache和MariaDB 命令:systemctl restart mariadb.service 命令:systemctl restart httpd.service


测试 进入服务器的根目录(默认是var/www/html,可在配置文件中修改) 命令:cd /var/www/html


创建index.php文件 命令:vim index.php


写入以下内容 按i进入insert模式 输入以下内容: <?php phpinfo(); ?> 按ESC退出insert模式,输入:wq保存并退出


查看最后成果 在浏览器输入服务器ip或域名,本机可输入localhost,出现如下图所示结果则为成功

https://jingyan.baidu.com/article/a3f121e4b0dd75fc9052bbc6.html