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

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

两侧同时换到之前的修订记录 前一修订版
is:cos:centos服务搭建-01-php环境 [2025/02/17 04:03] – 移除 - 外部编辑 (未知日期) 127.0.0.1is:cos:centos服务搭建-01-php环境 [2025/02/17 04:03] (当前版本) – ↷ 页面is:centos服务搭建-01-php环境被移动至is:cos:centos服务搭建-01-php环境 A.L
行 1: 行 1:
 +~~NOTOC~~
 +~~NOCACHE~~
  
 +====== CentOS 7.0 (64位)服务器环境搭建(Apache+MariaDB+PHP) ======
 +
 +===== CentOS 7默认使用的是Firewall作为防火墙,将其改为iptables防火墙 =====
 +
 +  * 关闭firewall服务
 +    <code>
 +    # systemctl stop firewalld.service 禁止firewall开机启动
 +    # systemctl disable firewalld.service
 +    # yum install iptables-services 安装iptables防火墙
 +    </code>
 +
 +  * 编辑防火墙配置文件
 +    <code>
 +    # 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 设置防火墙开机启动
 +    </code>
 +
 +  * 关闭SELINUX
 +    <code>
 +    # vim /etc/selinux/config
 +    # 按i进入INSERT模式,
 +    # 将SELINUXTYPE=targeted  #注释掉
 +    # 按ESC退出,输入:wq保存并退出。
 +    </code>
 +
 +  * SELinux完全关闭
 +    <code>
 +    # setenforce 0 使配置立即生效
 +    # 注:
 +    setenforce 1 设置SELinux 成为enforcing模式
 +    setenforce 0 设置SELinux 成为permissive模式
 +    </code>
 +
 +===== 安装apache =====
 +
 +  * 安装Apache
 +    <code>
 +    # yum install httpd
 +    # 可能会用到的命令:
 +    systemctl start httpd.service #启动apache
 +    systemctl stop httpd.service #停止apache
 +    systemctl restart httpd.service #重启apache
 +    systemctl enable httpd.service #设置apache开机启动
 +    </code>
 +
 +  * 重启Apache:重启后,在浏览器输入域名或IP地址,若是本机可以输入localhost进行测试
 +    <code>
 +    # systemctl enable httpd.service 设置Apache开机启动
 +    </code>
 +
 +===== 安装数据库 =====
 +
 +  * 安装MariaDB
 +    <code>
 +    # 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 #设置开机启动
 +    </code>
 +
 +  * 拷贝配置文件
 +    <code>
 +    # cp /usr/share/mysql/my-huge.cnf /etc/my.cnf 拷贝配置文件
 +    # 注:拷贝配置文件(注意:如果/etc目录下面默认有一个my.cnf,直接覆盖即可)
 +    # systemctl restart mariadb.service 重启MariaDB
 +    # 若出现异常,说明MariaDB未启动,可按CTRL+C退出,并启动数据库。
 +    </code>
 +
 +  * 设置MariaDB root密码
 +    <code>
 +    # mysql_secure_installation 为root设置密码
 +    # 此处直接按回车即可(一开始没有密码,以后改密码则需要输入已设定的密码)
 +    # 然后会要求你输入两次密码,然后回车
 +    # 然后一路按Y加回车
 +    # 直到出现Thanks for using MariaDB
 +    # systemctl restart mariadb.service 重启MariaDB
 +    # systemctl enable mariadb.service 设置为开机启动
 +    </code>
 +
 +===== 安装PHP =====
 +
 +  * 安装PHP及组件
 +    <code>
 +    # 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
 +    </code>
 +
 +===== 重启Apache以及MariaDB =====
 +
 +  * 重启服务
 +    <code>
 +    # systemctl restart mariadb.service
 +    # systemctl restart httpd.service
 +    </code>
 +
 +===== 配置Apache =====
 +
 +  * 编辑Apache配置文件
 +    <code>
 +    # 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 删除默认测试首页(可忽略此步)
 +    </code>
 +
 +===== 配置PHP =====
 +
 +  * 编辑PHP配置文件
 +    <code>
 +    # 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退出并保存
 +    </code>
 +
 +===== 重启Apache和MariaDB =====
 +
 +  * 重启服务
 +    <code>
 +    # systemctl restart mariadb.service
 +    # systemctl restart httpd.service
 +    </code>
 +
 +===== 测试 =====
 +
 +  * 创建测试文件
 +    <code>
 +    # cd /var/www/html 进入服务器的根目录(默认是var/www/html,可在配置文件中修改)
 +    # vim index.php 创建index.php文件
 +    # 写入以下内容
 +    <?php
 +    phpinfo();
 +    ?>
 +    # 按ESC退出insert模式,输入:wq保存并退出
 +    </code>
 +
 +===== 查看最后成果 =====
 +
 +  * 在浏览器输入服务器ip或域名,本机可输入localhost,出现如下图所示结果则为成功
 +    [[https://jingyan.baidu.com/article/a3f121e4b0dd75fc9052bbc6.html|查看示例]]