CentOS 8 配置Nginx+PHP+MySQL+Git 详细安装设置

CentOS 8配置Nginx+PHP+MySQL+Git详细安装设置

作为服务器系统再加上一个PHP开发者,使用CentOS8系统运行LNMP技术栈是件很正常的事,再加上作为一个强迫症,有了这种新系统软件新版本,忍不住想要折腾下,这里把我在CentOS8上的配置Nginx+PHP+MySQL+Git 过程及遇到的坑记录一下,顺便备份下相关配置文件,方便有需要的朋友查找。
操作系统: CentOS 8.2 64位

为PHP-FPM及Nginx创建专门的用户

考虑到安全问题,我建议为PHP-FPM及Nginx专门创建一个只用来跑Web服务、没有home目录也不允许登录的用户,我这里是创建一个www用户组,然后创建一个www用户并分配到www组,命令如下:

groupadd www
//-s参数是表示不允许登录,-g参数是分配到www组,-M表示不创建用户目录。
useradd -s /sbin/nologin www -g www -M

后面我们统一用www用户组下面的www用户来运行Nginx及PHP-FPM,这样就能解决文件读写权限问题。
在这里插入图片描述

安装PHP

CentOS8自带了PHP7.2版本,相对来说比较新,暂时不需要借助第三方的源来安装,而且CentOS8自带的PHP的模块也相对来说够用,使用下面命令可以看到所有PHP相关的包:

//搜索php
yum search php

在这里插入图片描述

其中,php-cli是我们命令运行PHP所需要的,php-fpm是用来配合Nginx使用的,php-mysqlnd是连接MySQL使用的,php-pear在安装一些特定的模块比如说Redis、MongoDB、Swoole等需要用到,php-mbstring包含了一些加密解密函数,php-gd是图形库,生成图形验证码可能需要用到,php-opcache是用来加速PHP运行的,php-process在Swoole相关进行需要用到等等。

安装PHP相关包

PHP的模块我们不需要全部安装,仅需要安装一些必须的,以常用的WordPress及Laravel来说,我们只需要安装如下几个包就行:

yum install php-cli php-fpm php-gd php-mysqlnd php-mbstring php-opcache php-pdo

在这里插入图片描述

配置PHP-FPM

安装好了之后,我们还需要稍稍配置一下PHP-FPM,以让其更好的与Nginx进行配合工作。

编辑**/etc/php-fpm.d/www.conf **文件,这个文件有两个需要注意的地方,其中一个就是运行PHP-FPM的用户及对应的用户组:

//可以使用命令修改也可以使用下面的工具修改
vim /etc/php-fpm.d/www.conf
:%s/apache/www/g

在这里插入图片描述

修改上述文件,将其中的user及group字段由apache更改成上述我们创建的www。
在这里插入图片描述
在这里插入图片描述

另外一个就是**listen = /run/php-fpm/www.sock **这个配置置项,这一行配置了PHP-FPM运行模式及对应的文件目录,我们后面配置Nginx时会用到它。
在这里插入图片描述

//启动:
systemctl start php-fpm
//停止
systemctl stop httpd

在后面nginx配置完后再来启动

Nginx安装及配置

安装Nginx很简单,CentOS8系统源自带,只需要一行命令就能安装好:

// 安装:
yum install nginx
// 启动:
systemctl start nginx

在这里插入图片描述

基础配置也很简单,到这一步,我们只需要稍稍改动一下Nginx的配置文件就行,Nginx的配置文件在/etc/nginx/nginx.conf,这个配置文件能配置Nginx所有的功能,包括其他的配置文件也是通过这个文件进行加载。打开其配置文件,修改user字段,并增加几行可能会用上的配置:
在这里插入图片描述
在这里插入图片描述
主要是更改user哪一行,后面几个配置可以选择性增加,如果不知道其什么意思也可以暂时不加。

至于配置文件中默认的default_server那一部分,是自带的默认host,如果不需要的可以删除,或者直接返回404。

到目前为止,Nginx的本身配置已经完成。

在这里插入图片描述

MySQL 8安装

其实CentOS8系统自带了MySQL8和MariaDB 10.3.11,如果是从CentOS7升级上来的朋友,可能更习惯MariaDB,虽然说MySQL和MariaDB本身区别不大,但是出于兼容性的考虑,我还是更倾向于选择MySQL,毕竟使用MariaDB数据库有的时候出现一些和MySQL差别问题其实挺烦的,而且很多软件也是安装MySQL本身来适配的,所以我这里使用MySQL 8。

//安装MySQL8及对应工具
yum install mysql mysql-server
//启动MySQL服务
systemctl start mysqld

在这里插入图片描述
启动之后 初始化MySQL,命令如下:
在这里插入图片描述

//使用命令初始化
mysql_secure_installation 

在这里插入图片描述

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
// 这里提示选一个密码强度等级
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1
Please set the password for root here.
// 然后按照所选的密码强度要求设定密码
New password:
Re-enter new password:
// 密码要到100强度才可以
Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
// 这里删除默认无密码用户
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.

Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
// 禁止远程root登录,我选的是不禁止。因为我的mac上的数据库不会放到公网上,也不会存什么敏感数据
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : no

 ... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.

// 这里删除默认自带的test数据库
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
//是否立即重新加载权限表
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

mysql_secure_installation用来初始化MySQL信息,通过这个程序,我们可以进行如下操作:

  1. 设置root密码
  2. 更换root密码
  3. 移除匿名用户
  4. 禁用root用户远程登录
  5. 删除测试数据库
  6. 重新加载权限表

根本不用自己写SQL语句去更改MySQL的root密码,通过这个小程序就能直接设置/更改root密码,非常方便。

Git 安装

//直接使用安装
yum install  git

在这里插入图片描述

延伸联合配置使用

把Nginx+PHP+MySQL+Git联合配置起来使用

为网站添加单独的Nginx配置文件

其实放置网站的Nginx配置文件各个发行版都有自己的做法,有些是放在/etc/nginx/conf.d/,有些是放在/etc/nginx/site-avaliable,但是我为了统一,一般都是放在前者,这样就不用管什么发行版,更加方便去查找。
Markdown将文本转换为 HTML。

以域名www.runzi.cloud为例,假如网站文件放在/var/www/runzi,相关配置文件举例如下:

vim /etc/nginx/conf.d/runzi-ssl.conf

server{
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
        server_name  runzi.cloud;
        index index.html index.php;
        root  /var/www/runzi/public;
        ssl_certificate /var/www/ssl/runzi.crt;
        ssl_certificate_key /var/www/ssl/runzi.key;
        ssl_session_cache        shared:SSL:10m;
        ssl_session_timeout      10m;
        ssl_session_tickets      on;
        ssl_session_cache builtin:1000 shared:SSL:10m;
        ssl_dhparam /var/www/ssl/dhparam.pem;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;

        add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;

                include /etc/nginx/default.d/*.conf;

        location / {
                try_files $uri $uri/ /index.php?$args;
        }

        access_log /var/log/nginx/runzi.log;
        error_log /var/log/nginx/runzi-error.log;

        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/html;
        }
}

server
{
        listen          80;
        server_name runzi.com;
        location / {
                rewrite ^/(.*)$ https://runzi.cloud/$1 permanent;
        }
}

是的,上面没有包含location ~ .php$ {} 块,因为CentOS8源仓库里的Nginx默认提供了PHP部分相关配置。PHP相关部分配置文件位于/etc/nginx/default.d/php.conf,文件内容如下:
在这里插入图片描述

上面部分的Server块是用来配置对应网站的HTTPS部分功能,下面Server块是用来强制将HTTP请求原封不动的重定向到对应的HTTPS链接地址,比如说将http://nodeedge.com/about 重定向到 https://nodeedge.com/about 。

网站文件目录赋予可读写权限

了在Nginx配置文件中指定网站文件目录,还需要在赋予对应的用户可读写权限,上面是指定使用www用户组的www用户,指定的网站文件目录为/var/www/runzi/,对应的命令如下:

在这里插入图片描述

chown -R www.www /var/www/runzi

在这里插入图片描述

为网站创建单独的MySQL数据库

出于安全考虑,不建议使用root用户来运行网站使用的MySQL数据库,我更建议创建单独的数据库及对应的用户来运行对应的数据库,对应的命令如下:

//进入数据库
mysql -u root -p
//创建数据库表 runzi 设置为 utf8
create database if not exists runzi default character set utf8 collate utf8_unicode_ci;
//创建的用户名 runziuser
create user [15899859876@qq.com]'localhost' IDENTIFIED BY 'runziuser';
//创建的用户密码 runzipassword
grant all privileges on nodeedge.* to [15899859876@qq.com] identified by 'runzipassword';
//完成
flush privileges;

//实际创建的时候根据情况更改就好。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值