CentOS 7安装最新 PHP 7.2,MySQL 5.7和Nginx

Step 1 — 在CentOS 7 安装 Nginx Step 2 — 安装 MariaDB Step 3 — 安装 PHP v7.2 Step 4 — 配置 Nginx 和 PHP 7.2 匹配 Step 5 — 创建个 PHP 测试文件

Step 1 — 在CentOS 7 安装 Nginx

Nginx没有内置在默认的CentOS repositories, 我们先安装EPEL repository,输入如下命令:

yum install epel-release -y

下面,开始安装 Nginx

yum install nginx -y

安装完成后,启动nginx和让nginx在linux启动时自动运行:

systemctl start nginx
systemctl enable nginx

Step 2 — 安装 MariaDB

当页面代理服务器安装完成后,我们开始安装MariaDB, MariaDB 是著名的 MySQL 的社区版的一个分支. 由于 MariaDB 内置在 CentOS repositories 里面,我们直接使用 Yum 就能安装(原版 MySQL Community 安装请参考 Centos 7安装MySQL Server5.7 这篇文章):

yum install mariadb-server mariadb -y

安装完成后,我们同样启动服务和配置服务自启动:

systemctl start mariadb
systemctl enable mariadb

然后, 运行初始化脚本,清除一些默认配置:

mysql_secure_installation

这时,需要你提供 MariaDB 的密码, 但这是第一次登陆,没有密码,因此直接按回车;进入下个设置密码的窗口,按 Y 设置,该密码为 root 密码:


Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorization.
New password: password
Re-enter new password: password
Password updated successfully!
Reloading privilege tables..
... Success!

其他问题可以直接按回车 (ENTER)使用默认配置. 完成 MariaDB 的配置后,将进行PHP的配置。

Step 3 — 安装 PHP v7.2

首先,安装额外包括 PHP v7.2 的 CentOS repo :

wget http://rpms.remirepo.net/enterprise/remi-release-7.rpm
rpm -Uvh remi-release-7.rpm

使 php72 repository 生效(默认不生效):

yum install yum-utils -y
yum-config-manager --enable remi-php72

然后安装 PHP package:

yum --enablerepo=remi,remi-php72 install php-fpm php-common

安装通用模块:

yum --enablerepo=remi,remi-php72 install php-opcache php-pecl-apcu php-cli php-pear php-pdo php-mysqlnd php-pgsql php-pecl-mongodb php-pecl-redis php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml

Step 4 — 配置 Nginx 和 PHP 7.2 匹配

安装 nano 文件编辑器:

yum install -y nano

使用 nano 修改 /etc/nginx/conf.d/default.conf 这个文件:

nano /etc/nginx/conf.d/default.conf

进行如下配置:

server {
    listen   80;
    server_name  your_server_ip;
    # note that these lines are originally from the "location /" block
    root   /usr/share/nginx/html;
    index index.php index.html index.htm;
    location / {
        try_files $uri $uri/ =404;
    }
    error_page 404 /404.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_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

IMPORTANT! 替换 your_server_ip 为你的真实 server IP.

CTRL + O 保存修改的文件, CTRL + X (CMD + X Mac 用户) 退出编辑器.

配置完重启 Nginx 使修改生效:

systemctl restart nginx

接着进行 PHP-FPM 的配置:

nano /etc/php-fpm.d/www.conf

找到并替换如下几行:

user = apache to user = nginx

group = apache to group = nginx

listen.owner = nobody to listen.owner = nginx

listen.group = nobody to listen.group = nginx

最后在 ;listen = 127.0.0.1:9000 下面增加:

listen = /var/run/php-fpm/php-fpm.sock

保存修改和退出编辑器. 最后, 启动 php-fpm 并让它自启动:

systemctl start php-fpm.service
systemctl enable php-fpm.service

Step 5 — 创建个 PHP 测试文件

rm /usr/share/nginx/html/index.html
echo "<?php phpinfo(); ?>" >> /usr/share/nginx/html/index.php

访问你的 your_server_ip 查看是否配置成功,若成功,则显示如下页面: alt

若需要安装低版本的5.x版本的PHP,可以参考: https://www.jianshu.com/p/bc14ff0ab1c7

文章来源:

Author:大官人
link:https://www.daguanren.cc/post/php_nginx_mysql_installation_guide.html