1. 程式人生 > 實用技巧 >CentOS7部署LNMP環境

CentOS7部署LNMP環境

使用限制

本教程示例步驟適用於以下軟體版本:
作業系統:公共映象CentOS 7.2 64位
Nginx版本:Nginx 1.16.1
MySQL版本:MySQL 5.7.28
PHP版本:PHP 7.0.33

操作流程

步驟一:準備編譯環境

關閉防火牆。
執行systemctl status firewalld命令檢視當前防火牆的狀態。

如果防火牆的狀態引數是inactive,則防火牆為關閉狀態。
如果防火牆的狀態引數是active,則防火牆為開啟狀態。本示例中防火牆為開啟狀態,因此需要關閉防火牆。
關閉防火牆。如果防火牆為關閉狀態可以忽略此步驟。
如果您想臨時關閉防火牆,執行命令systemctl stop firewalld。

說明 這只是暫時關閉防火牆,下次重啟Linux後,防火牆還會開啟。
如果您想永久關閉防火牆,執行命令systemctl disable firewalld。

說明 如果您想重新開啟防火牆,請參見firewalld官網資訊。
關閉SELinux。
執行getenforce命令檢視SELinux的當前狀態。

如果SELinux狀態引數是Disabled,則SELinux為關閉狀態。
如果SELinux狀態引數是Enforcing,則SELinux為開啟狀態。本示例中SELinux為開啟狀態,因此需要關閉SELinux。
關閉SELinux。如果SELinux為關閉狀態可以忽略此步驟。
如果您想臨時關閉SELinux,執行命令setenforce 0。

說明 這只是暫時關閉SELinux,下次重啟Linux後,SELinux還會開啟。
如果您想永久關閉SELinux,執行命令vim /etc/selinux/config編輯SELinux配置檔案。回車後,把游標移動到SELINUX=enforcing這一行,按i鍵進入編輯模式,修改為SELINUX=disabled,按Esc鍵,然後輸入:wq並按Enter鍵以儲存並關閉SELinux配置檔案。

說明 如果您想重新開啟SELinux,請參見開啟或關閉SELinux。
重啟系統使設定生效。

步驟二:安裝Nginx

執行以下命令安裝Nginx。

yum -y install nginx
執行以下命令檢視Nginx版本。

nginx -v
返回結果如下所示,表示Nginx安裝成功。

nginx version: nginx/1.16.1

步驟三:安裝MySQL

執行以下命令更新YUM源。

rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
執行以下命令安裝MySQL。

說明 如果您使用的作業系統核心版本為el8,可能會提示報錯資訊No match for argument。您需要先執行命令yum module disable mysql禁用預設的mysql模組,再安裝MySQL。

yum -y install mysql-community-server
執行以下命令檢視MySQL版本號。

mysql -V
返回結果如下所示,表示MySQL安裝成功。

mysql Ver 14.14 Distrib 5.7.28, for Linux (x86_64) using EditLine wrapper
執行以下命令啟動MySQL。

systemctl start mysqld
執行以下命令設定開機啟動MySQL。

systemctl enable mysqld
systemctl daemon-reload

步驟四:安裝PHP

更新YUM源。
執行以下命令新增epel源。

yum install \
https://repo.ius.io/ius-release-el7.rpm \
https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

執行以下命令新增Webtatic源。

rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
執行以下命令安裝PHP。

yum -y install php70w-devel php70w.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64 php70w-pdo.x86_64 php70w-mysqlnd php70w-fpm php70w-opcache php70w-pecl-redis php70w-pecl-mongodb
執行以下命令檢視PHP版本。

php -v
返回結果如下所示,表示安裝成功。

PHP 7.0.33 (cli) (built: Dec  6 2018 22:30:44) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.33, Copyright (c) 1999-2017, by Zend Technologies  

步驟五:配置Nginx

執行以下命令備份Nginx配置檔案。

cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
修改Nginx配置檔案,新增Nginx對PHP的支援。

說明 若不新增此配置資訊,後續您使用瀏覽器訪問PHP頁面時,頁面將無法顯示。
執行以下命令開啟Nginx配置檔案。

vim /etc/nginx/nginx.conf
按i進入編輯模式。
在server大括號內,新增下列配置資訊。

    #除下面提及的需要新增的配置資訊外,其他配置保持預設值即可。
    location / {
        #在location大括號內新增以下資訊,配置網站被訪問時的預設首頁
        index index.php index.html index.htm;
    }
    #新增下列資訊,配置Nginx通過fastcgi方式處理您的PHP請求
    location ~ .php$ {
        root /usr/share/nginx/html;    #將/usr/share/nginx/html替換為您的網站根目錄,本教程使用/usr/share/nginx/html作為網站根目錄
        fastcgi_pass 127.0.0.1:9000;   #Nginx通過本機的9000埠將PHP請求轉發給PHP-FPM進行處理
        fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include fastcgi_params;   #Nginx呼叫fastcgi介面處理PHP請求
    }                

新增配置資訊後,如下圖所示。

按下Esc鍵後,輸入:wq並回車以儲存並關閉配置檔案。
執行以下命令啟動Nginx服務。

systemctl start nginx
執行以下命令設定Nginx服務開機自啟動。

systemctl enable nginx

步驟六:配置MySQL

執行以下命令檢視/var/log/mysqld.log檔案,獲取並記錄root使用者的初始密碼。

grep 'temporary password' /var/log/mysqld.log
返回結果如下:

2016-12-13T14:57:47.535748Z 1 [Note] A temporary password is generated for root@localhost: p0/G28g>lsHD

說明 下一步重置root使用者密碼時,會使用該初始密碼。
執行以下命令配置MySQL的安全性。

mysql_secure_installation
安全性的配置包含以下五個方面:
重置root賬號密碼。

Enter password for user root: #輸入上一步獲取的root使用者初始密碼
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration of the plugin.
Using existing password for root.
Estimated strength of the password: 100 
Change the password for root ? (Press y|Y for Yes, any other key for No) : Y #是否更改root使用者密碼,輸入Y
New password: #輸入新密碼,長度為8至30個字元,必須同時包含大小寫英文字母、數字和特殊符號。特殊符號可以是()` ~!@#$%^&*-+=|{}[]:;‘<>,.?/
Re-enter new password: #再次輸入新密碼
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
輸入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  #是否刪除匿名使用者,輸入Y
Success.
輸入Y禁止root賬號遠端登入。 


Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y #禁止root遠端登入,輸入Y
Success.
輸入Y刪除test庫以及對test庫的訪問許可權。 


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y #是否刪除test庫和對它的訪問許可權,輸入Y
- Dropping test database...
Success.
輸入Y重新載入授權表。 


Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y #是否重新載入授權表,輸入Y
Success.
All done!

更多詳情,請參見MySQL文件。

步驟七:配置PHP

新建phpinfo.php檔案,用於展示PHP資訊。
執行以下命令新建檔案。

vim <網站根目錄>/phpinfo.php #將<網站根目錄>替換為您配置的網站根目錄。
網站根目錄是您在nginx.conf檔案中location ~ .php$大括號內配置的root值,如下圖所示。

本教程配置的網站根目錄為/usr/share/nginx/html,因此命令為:

vim /usr/share/nginx/html/phpinfo.php
按i進入編輯模式。
輸入下列內容,函式phpinfo()​會展示PHP的所有配置資訊。

按Esc鍵後,輸入:wq並回車以儲存並關閉配置檔案。
執行以下命令啟動PHP-FPM。

systemctl start php-fpm
執行以下命令設定PHP-FPM開機自啟動。

systemctl enable php-fpm

步驟八:測試訪問LNMP平臺

開啟瀏覽器。
在位址列輸入http://<ECS例項公網IP地址>/phpinfo.php。
返回結果如下圖所示,表示LNMP環境部署成功。
後續步驟
測試訪問LNMP平臺成功後,建議您執行以下命令將phpinfo.php檔案刪除,消除安全隱患。

rm -rf <網站根目錄>/phpinfo.php #將<網站根目錄>替換為您在nginx.conf中配置的網站根目錄
本教程配置的網站根目錄為/usr/share/nginx/html,因此命令為:

rm -rf /usr/share/nginx/html/phpinfo.php

原文件連結:https://help.aliyun.com/document_detail/97251.html#title-685-ygs-m1z
源自:阿里雲文件