示例步驟将使用以下軟體版本:
- CentOS 7
- Nginx版本:Nginx 1.16.1
- MySQL版本:MySQL 5.7.28
- PHP版本:PHP 7.0.33
部署前工作
- 幹淨的CentOS 7
- 關閉防火牆
- 運作systemctl status firewalld指令檢視目前防火牆的狀态。
systemctl status firewalld
如果防火牆的狀态參數是inactive,則防火牆為關閉狀态。
如果防火牆的狀态參數是active,則防火牆為開啟狀态。本示例中防火牆為開啟狀态,是以需要關閉防火牆。
- 關閉防火牆。如果防火牆為關閉狀态可以忽略此步驟。
#如果您想臨時關閉防火牆,運作指令systemctl stop firewalld systemctl stop firewalld #如果您想永久關閉防火牆,運作指令systemctl disable firewalld systemctl disable firewalld
- 運作systemctl status firewalld指令檢視目前防火牆的狀态。
安裝Nginx
- 運作以下指令安裝Nginx
yum -y install nginx
- 運作以下指令檢視Nginx版本
傳回結果如下表示安裝成功nginx -v
nginx version: nginx/1.16.1
安裝MySQL
- 運作以下指令更新YUM源
rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
- 運作以下指令安裝MySQL
yum -y install mysql-community-server
- 運作以下指令檢視MySQL版本号
傳回結果如下表示安裝成功mysql -V
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源
運作以下指令添加Webtatic源yum install \ https://repo.ius.io/ius-release-el7.rpm \ https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
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的支援
運作以下指令打開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并回車以儲存并關閉配置檔案。server { listen 80 default_server; listen [::]:80 default_server; server_name _; root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; 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請求。 } error_page 404 /404.html; location = /404.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { }
- 運作以下指令啟動Nginx服務
systemctl start nginx
- 運作以下指令設定Nginx服務開機自啟動
systemctl enable nginx
配置MySQL
- 運作以下指令檢視/var/log/mysqld.log檔案,擷取并記錄root使用者的初始密碼。
傳回結果如下表示安裝成功grep 'temporary password' /var/log/mysqld.log
- 運作以下指令配置MySQL的安全性
mysql_secure_installation
Securing the MySQL server deployment. Enter password for user root: #輸入上一步擷取的root使用者初始密碼 The existing password for the user account root has expired. Please set a new password. New password: #輸入新密碼,長度為8至30個字元,必須同時包含大小寫英文字母、數字和特殊符号。特殊符号可以是()` ~!@#$%^&*-+=|{}[]:;‘<>,.?/ Re-enter new password: #再次輸入新密碼 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 #是否删除匿名使用者,輸入Y Success. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y #禁止root遠端登入,輸入Y Success. Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y #是否删除test庫和對它的通路權限,輸入Y Dropping test database... Success. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y #是否重新加載授權表,輸入Y Success. All done!
配置PHP
-
建立phpinfo.php檔案,用于展示PHP資訊
運作以下指令建立檔案
vim /usr/share/nginx/html/phpinfo.php
按i進入編輯模式。
輸入下列内容,函數phpinfo()會展示PHP的所有配置資訊。
按Esc鍵後,輸入:wq并回車以儲存并關閉配置檔案。<?php echo phpinfo(); ?>
- 運作以下指令啟動PHP-FPM
systemctl start php-fpm
- 運作以下指令設定PHP-FPM開機自啟動
systemctl enable php-fpm
測試通路LNMP
- 打開浏覽器。
-
在位址欄輸入http://<ECS執行個體公網IP位址>/phpinfo.php。
傳回結果如下圖所示,表示LNMP環境部署成功。