美文网首页LAMPPHP经验分享
Centos7 Mysql + PHP + Nginx 环境配置

Centos7 Mysql + PHP + Nginx 环境配置

作者: 茶艺瑶 | 来源:发表于2016-12-09 16:31 被阅读407次

    Nginx (读“engine x”)是一款免费、开源、高性能的HTTP服务器。 Nginx 因性能稳定、功能丰富、配置简单、资源消耗低而著称。
    本文介绍如何在Linux服务器(Cent OS7)上安装Nginx、MySQL和PHP,这个环境也简称 LNMP 或LEMP 。其中,Nginx和PHP的采用PHP-FPM方式。

    先看虚拟机配置【电脑位32G内存,所以可以这么任性 -_-】

    Paste_Image.png Paste_Image.png

    本问中的测试主机是用虚拟机IP地址来访问的,请根据具体情况替换
    教程中的命令都是在root权限下操作,请切换到root用户,命令

    su
    

    输入密码后进入root用户控制台

    Paste_Image.png

    安装 MySQL5

    与CentOS 6不同,CentOS 7服务器必须通过社区仓库来安装MySQL。如果像CentOS 6中用:yum install mysql
    ,默认就会安装 MariaDB 数据库。(说明:MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可。开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此社区采用分支的方式来避开这个风险,目前来看没有太大必要。)
    MySQL的社区仓库:https://dev.mysql.com/downloads/repo/yum/,安装步骤:

    wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
    rpm -ivh mysql-community-release-el7-5.noarch.rpm
    yum update
    yum install mysql-server
    

    在询问的时候键入y
    确定安装。然后启动MySQL,并设置开机启动:

    systemctl start mysqld # 启动 MySQL
    systemctl enable mysqld # 开机启动ySMySQL
    

    MySQL默认会绑定到地址 localhost(127.0.0.1),默认用户名是root
    ,密码为空。使用下面的命令修改密码:

    mysql_secure_installation
    

    搞死人 --- 安装半个 源不在国内。。

    Paste_Image.png

    安装 Nginx

    如果系统安装了Apache,先卸载。

    service httpd stop
    systemctl disable httpd
    yum remove httpd
    

    再安装Nginx:

    yum install epel-release
    yum -y install nginx
    

    启动 Nginx,并设置为开机启动:

    systemctl start nginx
    systemctl enable nginx
    

    又10多分钟过去了

    我们输入

    ip addr  //查看我们虚拟机的ip地址
    
    Paste_Image.png

    我们还有开启80端口的权限

    iptables -I INPUT -p tcp --dport 80 -j ACCEPT 
    
    Paste_Image.png

    可以访问了

    安装 PHP5-FPM

    yum install php php-mysql php-fpm
    

    然后是配置。打开文件 /etc/php.ini,设置cgi.fix_pathinfo=0(要先删除前面的;注释符),如下:

    [...]
    cgi.fix_pathinfo=0
    [...]
    

    用vim打开 直接/cgi.fix_pathinfo 回车就可以到达

    再配置PHP-FPM。打开文件 /etc/php-fpm.d/www.conf

    将 127.0.0.1:9000 改为 php-fpm.sock 文件
    取消 listen.owner和listen.group前面的注释
    将user 和 group 的值由apache 改为 nginx
    

    如下

    [...]
    listen = /var/run/php-fpm/php-fpm.sock
    [...]
    listen.owner = nobodylisten.group = nobody
    [...]
    user = nginxgroup = nginx
    [...]
    

    启动 PHP-FPM,并设置为开机启动:

    systemctl start php-fpm
    systemctl enable php-fpm
    

    PHP-FPM 启动之后,会生成 socket 文件 /var/run/php-fpm/php-fpm.sock
    作为守护进程运行 FastCGI 服务。接下来配置 Nginx 的时候会用到这个 socket 文件。

    配置 Nginx

    Nginx 的配置文件是:/etc/nginx/nginx.conf,使用 vim 打开:

    vi /etc/nginx/nginx.conf
    

    配置项非常简单,如果需要了解详细内容,可看:https://www.nginx.com/resources/wiki/start/topics/examples/full/。下面介绍基本的配置。
    首先,根据情况调整worker_processes和keepalive_timeout(可选):

    [...]
    worker_processes 4;
    [...]
    keepalive_timeout 2;
    [...]
    

    虚拟主机定义在 server{} 容器中,修改为如下内容:

    [...] 
    server { 
        listen 80; 
        listen [::]:80 default_server;  
        server_name _; root /usr/share/nginx/html; 
        index index.php index.html index.htm; 
        location / { 
        # First attempt to serve request as file, then 
        # as directory, then fall back to displaying a 404. 
        try_files $uri $uri/ =404; 
        } 
        error_page 404 /404.html; 
        location = 40x.html { 
            root /usr/share/nginx/html; 
        } 
        error_page 500 502 503 504 /50x.html; 
        location = /50x.html { 
            root /usr/share/nginx/html; 
        } 
        location ~ \.php$ { 
            try_files $uri =404; 
            fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; 
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
            fastcgi_index index.php; include fastcgi_params; 
        } 
    }
    [...]
    

    关于配置的一些说明:
    首先,我们打开两个listen
    ,让 Nginx 同时侦听 IPv4 和 IPv6 的80
    端口。
    server_name _;
    绑定所有的 vhost(可以指定主机名,如 www.example.com)。
    脚本根目录 root 不变,依然是 /usr/share/nginx/html

    索引首页文件 index 添加上 index.php

    其中针对 PHP 很重要的部分在location ~.php$ {}
    中。为防止零日攻击(详见:http://wiki.nginx.org/Pitfalls#Passing_Uncontrolled_Requests_to_PHP andhttp://forum.nginx.org/read.php?2,88845,page=3),该部分开头设置了try_files $uri =404;

    保存配置文件后,检查配置文件的语法,再重新加载 Nginx:

    nginx -t # 检查配置文件语法
    systemctl reload nginx # 重新加载nginx
    

    在 /usr/share/nginx/html 目录下创建文件 index.php,内容为:

    <?php 
        phpinfo()
    ;?>
    
    Paste_Image.png

    可以看到,PHP5 已经工作,根据 Server API 行看出,是通过 FPM/FastCGI 方式工作的,往下拉会看到 PHP5 加载的其他模块,包括MySQL、cURL、sqlite模块的支持。

    PHP-FPM 使用 TCP 连接

    默认情况下,PHP-FPM 通过 /var/run/php-fpm/php-fpm.sock 文件侦听 socket。当然,也可以设置 PHP-FPM 使用 TCP 连接。打开文件 /etc/php-fpm.d/www.conf,设置 listen
    值如下:

    [...]
    ;listen = /var/run/php-fpm/php-fpm.sock
    listen = 127.0.0.1:9000
    [...]
    

    这样 PHP-FPM 会侦听地址 127.0.0.1(localhost)和端口9000,确保这个端口没有被其他程序占用。然后重新加载 PHP-FPM:

    systemctl reload php-fpm
    

    接下来,编辑 /etc/nginx/nginx.conf 文件,修改如下一行:

    [...]
    #fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
    fastcgi_pass 127.0.0.1:9000;
    [...]
    

    然后,重载 nginx 就可以了:

    sytemctl reload nginx
    

    这样,整个LNMP环境就搭好了。

    相关文章

      网友评论

      本文标题:Centos7 Mysql + PHP + Nginx 环境配置

      本文链接:https://www.haomeiwen.com/subject/fqiemttx.html