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組,命令如下:

1.groupadd www
2//-s參數是表示不允許登錄,-g參數是分配到www組,-M表示不創建用戶目錄。
3useradd -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安裝及配置

安裝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;

//實際創建的時候根據情況更改就好。
————————————————
版權聲明:本文為CSDN博主「香言香語」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/Jeff_YaoJie/article/details/108593370

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM