Centos+php+nginx+mysql环境部署

Centos+php+nginx+mysql环境部署

Centos+php+nginx+mysql环境部署

Centos环境下部署php环境经常会遇到一些问题,每次历尽千辛万苦解决后又没有做记录,再次遇到部署的时候又会重来头疼不已
在这里我写一个非常详细的文章记录最近一次部署过程

1 部署环境

服务器:CentOS 8
PHP版本:7.3.20
mysql:5.7
web服务器:nginx 1.14.1

2 安装PHP环境

我们使用 dnf 来安装 PHP环境,不推荐使用 yum 直接来按,因为很有可能会遇到各种冲突

使用 dnf 很有可能会遇到

  1. Failed to Download Metadata for Repo ‘AppStream’ 问题
  2. yum 命令直接提示找不到软件包源

我们拿到空白服务器后首先配置好各种软件包源,确保安装顺畅

设置CentOS软件包源为华为云镜像 https://mirrors.huaweicloud.com/home

# 备份原有软件仓库文件
cp -a /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
# 下载华为云镜像仓库文件
wget -O /etc/yum.repos.d/CentOS-Base.repo https://repo.huaweicloud.com/repository/conf/CentOS-8-reg.repo
# 清除原有yum缓存
yum clean all
# 刷新缓存
yum makecache
# 执行软件包更新
yum update

增加 dnf

sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*
sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-Linux-*
sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.epel.cloud|g' /etc/yum.repos.d/CentOS-Linux-*

接下来可以继续安装php环境了

yum install epel-release
dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-8.rpm
# 重置php模块,非常重要的一步,这里不重置的话,很有可能和yum的源发生冲突
dnf module reset php
dnf module list php

可以看到如下信息

Last metadata expiration check: 2:35:57 ago on Sat 20 Aug 2022 07:04:46 PM CST.
CentOS-8.5.2111 - AppStream - mirrors.aliyun.com
Name                                Stream                                Profiles                                                 Summary
php                                 7.2 [d]                               common [d], devel, minimal                               PHP scripting language
php                                 7.3 [e]                               common [d], devel, minimal                               PHP scripting language
php                                 7.4                                   common [d], devel, minimal                               PHP scripting language
Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

选择我们需要安装的版本,执行安装

# 指定使用 php 7.3 版
dnf module enable php:7.3
dnf module install php

继续使用 yum 安装剩余的依赖包

yum -y install php php-mysqlnd php-gd php-xml php-mbstring php-ldap php-pear php-xmlrpc php-zip php-json

查看php版本

php -v
PHP 7.3.20 (cli) (built: Jul  7 2020 07:53:49) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.20, Copyright (c) 1998-2018 Zend Technologies

启动php-fpm服务,并设置为开机自动启动

systemctl enable --now php-fpm

由于我们使用 nginx 作为web服务器,所以需要修改 php-fpm.conf 相关配置

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

user = apache 改为 user = nginxgroup = apache 改为 group = nginnx

重启 php-fpm 服务,systemctl restart php-fpm

查看下服务运行状态

systemctl status php-fpm

如果为 Active: active (running) since Fri 2022-07-29 11:40:10 CST; 3 weeks 1 days ago

则表示服务运行正常,php环境安装成功

3 安装配置nginx环境

直接 yum 安装

yum install nginx

修改配置文件 vim /etc/nginx/nginx.conf

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/
# 需要将 user 改为 root
user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    client_max_body_size 10m;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    server {
        listen       80;
	    server_name  127.0.0.1;
    	root /data/web/cms;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        location / {
	        root   /data/web/cms;
            index  index.html index.php;
        }
        error_page 404 /404.html;
        location = /40x.html {
	        root html;
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
	        root html;
        }
	    location ~ \.php$ {
    	    root           /data/web/cms;
    	    fastcgi_pass   unix:/run/php-fpm/www.sock;
    	    fastcgi_index  index.php;
    	    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
	        include        fastcgi_params;
	    }	
    }
}

参考上面配置进行修改调整,需要注意如下几个问题:

  1. user 需要改为 root
  2. 增加 client_max_body_size 10m;
  3. 由于用到了 $document_root,因此所有的 root 变量都需要设置,不能删除也不能为空
  4. 注意 fastcgi_pass unix:/run/php-fpm/www.sock;,不能使用默认的配置 fastcgi_pass 127.0.0.1:9000
    原因是php7已经直接走www.sock模式了,不用http代理了,这里是最头疼的地方,网上的帖子十有八九都没说到这个关键点。
    分析 /etc/php-fpm.d/www.conf ,可以看到 listen = /run/php-fpm/www.sock ,这里与nginx.conf保持一致

改为后对根目录进行授权

chmod 777 -R /data/web/cms

然后重启 nginx

systemctl restart nginx
# 或者直接设置为自动启动
systemctl enable --now nginx

可以创建 test 页进行php验证

test页示例

<?php
    phpinfo();
?>

使用 curl 命令进行验证

curl 127.0.0.1:8090

看到正确的页面内容展示则表示nginx+php环境集成成功

4 安装mysql5.7

删除可能已有的mysql

sudo dnf remove @mysql
sudo dnf module reset mysql
sudo dnf module disable mysql

增加mysql社区版源

sudo vim /etc/yum.repos.d/mysql-community.repo
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=0
[mysql-connectors-community]
name=MySQL Connectors Community
baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/7/$basearch/
enabled=1
gpgcheck=0
[mysql-tools-community]
name=MySQL Tools Community
baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/7/$basearch/
enabled=1
gpgcheck=0

关闭mysql8的源,开启mysql57源,并安装

sudo dnf config-manager --disable mysql80-community
sudo dnf config-manager --enable mysql57-community
sudo dnf install mysql-community-server

设置mysql服务器自动启动,并立即执行

sudo systemctl enable --now mysqld.service

获取mysql临时密码

sudo grep 'A temporary password' /var/log/mysqld.log |tail -1
2020-01-06T18:06:19.947403Z 1 [Note] A temporary password is generated for root@localhost: AS*5Rx%YY5+c

登录mysql,修改临时密码

sudo mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:

登录mysql

mysql -u root -p
Enter password: <Enter Root Password>

进入mysql控制台,创建数据库,创建新用户并授权

> CREATE DATABASE IF NOT EXISTS `pbootcms` DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci;
> CREATE USER 'cms'@'localhost' IDENTIFIED BY 'Swa!n@E802';
> grant all privileges on 'pbootcms'.* to 'cms'@'localhost';
> FLUSH PRIVILEGES;

修改mysql配置文件,文件路径 /etc/my.cnf

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/data/web/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
character-set-server=utf8mb4
bind-address=127.0.0.1
port=3306
collation-server=utf8mb4_general_ci

复制原有mysql文件到 datadir 指定的路径,并授予读写权限

重启mysql服务,验证是否能正常登录使用

systemctl restart mysqld

至此,mysql环境搭建完毕

参考mysql57官方安装步骤

5 总结

在服务器搭建 php+nginx+mysql环境 ,重难点在于各个软件包源的配置,很有可能一不小心就出现了冲突。
比如,一上来就直接 yum install php,这样服务器就会在默认的源拉取php包,很有可能版本不对,然后反复删除重装改配置,最后导致陷入死循环。
然后 php-fpm 的配置一定要修改用户、组为nginx,它的默认配置是 apache 需要改为 nginx
最后,nginx配置关键点在于 fastcgi_pass unix:/run/php-fpm/www.sock; 和用户 user 参数配置。
不熟悉环境配置,那么需要非常小心,每做一步操作做好记录,这样做错了还可以回退,删除文件一定要记得做好备份

                       

点击阅读全文

上一篇 2023年 5月 27日 am10:13
下一篇 2023年 5月 27日 am10:15