Ubuntu Server 16.04.x (Xenial Xerus) 安裝 LEMP / LNMP 教程

獸獸發表於2017-05-08

LEMP 指的是 Linux + Nginx (發音 engine x 所以這裡是 E 而不是 N) + MySQL + PHP 的簡稱,國內有些地方叫做 LNMP (因為 LNMP 沒法讀出來,而 LEMP 可以直接發音,所以今後本站教程一律都會寫 LEMP)

以下操作推薦在 root 使用者下完成,請使用 sudo -i 切換到 root 使用者進行操作

一、安裝 Nginx 1.10.x 或 1.12.x

由於 Nginx 更新頻繁,而 Ubuntu Server 長久以來一直更新緩慢沒法支援新功能,所以我們有兩種方案。

截止 2017 年 5 月 6 日,目前最新的穩定版是 1.12.x 當然次版 1.10.x 也是很穩定的。

1、用 Nginx 官方的 PPA 代替預設的源安裝

首先,加入 Nginx 的 PPA並安裝一些必要的軟體

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:nginx/stable
sudo apt-get update
sudo apt-get install curl vim wget unzip

如果喜歡體驗 Nginx 的新功能,您可以使用 add-apt-repository ppa:nginx/development 安裝 Nginx 的 Mainline 版代替預設的 Stable 版

接著安裝 Nginx

sudo apt-get install nginx

如果您需要 Nginx 更多的功能,您可以使用 sudo apt-get install nginx-extras 代替預設的 Nginx

PPA 方式更新也未必很及時,如果您希望追求更新的版本,我們可以使用第二種方法

2、使用 Nginx 官方源

首先,下載並匯入 Nginx 官方的 Key:

wget http://nginx.org/keys/nginx_signing.key
sudo apt-key add nginx_signing.key

修改 /etc/apt/sources.list 檔案或新建一個 /etc/apt/sources.list.d/nginx.list 檔案,內容如下:

deb http://nginx.org/packages/ubuntu/ xenial nginx
deb-src http://nginx.org/packages/ubuntu/ xenial nginx

接著就可以更新系統並安裝和官網同步的最新版本 Nginx:

sudo apt-get update
sudo apt-get install nginx

需要注意的是,官網的這個版本,安裝的外掛和 Nginx 配置檔案寫法和 Ubuntu Server 預設安裝以及 PPA 安裝的 Nginx 稍有區別,不過足夠日常使用。

二、安裝 PHP 7.0.x 或 PHP 7.1.x 或 PHP 5.6.x

目前 PHP 官方開發只對這三個版本進行維護。

雖然一些老舊的程式、外掛不支援 PHP 7.0.x 或 PHP 7.1.x,但是這貨速度確實快不少啊,強烈呼籲開發者漸漸的轉移到 PHP 7.0.x 或 PHP 7.1.x 的開發中,至於某些國產程式,就只能呵呵噠。

同樣,由於 Ubuntu Server 官方更新實在太慢,導致很多時候沒法打上最新的安全補丁,我們這裡推薦 Ondřej SurýPPA

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update

由於 PHP 7.1.x 實在太新,很多程式沒辦法跟上步伐,但是大多數正常的程式的對 PHP 7.0.x 的支援是很好的,所以本文推薦安裝 PHP 7.0.x

如有 PHP 7.1.x 或 PHP 5.6.x 需求的朋友,可以把下面的安裝包檔名類似 php7.0-fpm 改成 php7.1-fpmphp5.6-fpm

執行完成後,安裝一些常見的軟體以及 PHP 7.0.x, 如果是 WordPress ,那麼下面的 PHP 包足夠滿足大部分你需求:

sudo apt-get install php7.0-fpm php7.0-mysql php7.0-curl php7.0-gd php7.0-mbstring php7.0-mcrypt php7.0-xml php7.0-xmlrpc php7.0-zip

以上只安裝了大部分 WordPress 必須的 PHP 元件,如果您的程式需要額外的 PHP 元件,可以通過 apt-cache search php7.0 命令來查詢。

showfom@ubuntu:~$ sudo apt-cache search php7.0
libapache2-mod-php7.0 - server-side, HTML-embedded scripting language (Apache 2 module)
php-all-dev - package depending on all supported PHP development packages
php7.0 - server-side, HTML-embedded scripting language (metapackage)
php7.0-cgi - server-side, HTML-embedded scripting language (CGI binary)
php7.0-cli - command-line interpreter for the PHP scripting language
php7.0-common - documentation, examples and common module for PHP
php7.0-curl - CURL module for PHP
php7.0-dev - Files for PHP7.0 module development
php7.0-gd - GD module for PHP
php7.0-gmp - GMP module for PHP
php7.0-json - JSON module for PHP
php7.0-ldap - LDAP module for PHP
php7.0-mysql - MySQL module for PHP
php7.0-odbc - ODBC module for PHP
php7.0-opcache - Zend OpCache module for PHP
php7.0-pgsql - PostgreSQL module for PHP
php7.0-pspell - pspell module for PHP
php7.0-readline - readline module for PHP
php7.0-recode - recode module for PHP
php7.0-snmp - SNMP module for PHP
php7.0-sqlite3 - SQLite3 module for PHP
php7.0-tidy - tidy module for PHP
php7.0-xml - DOM, SimpleXML, WDDX, XML, and XSL module for PHP
php7.0-xmlrpc - XMLRPC-EPI module for PHP
libphp7.0-embed - HTML-embedded scripting language (Embedded SAPI library)
php7.0-bcmath - Bcmath module for PHP
php7.0-bz2 - bzip2 module for PHP
php7.0-enchant - Enchant module for PHP
php7.0-fpm - server-side, HTML-embedded scripting language (FPM-CGI binary)
php7.0-imap - IMAP module for PHP
php7.0-interbase - Interbase module for PHP
php7.0-intl - Internationalisation module for PHP
php7.0-mbstring - MBSTRING module for PHP
php7.0-mcrypt - libmcrypt module for PHP
php7.0-phpdbg - server-side, HTML-embedded scripting language (PHPDBG binary)
php7.0-soap - SOAP module for PHP
php7.0-sybase - Sybase module for PHP
php7.0-xsl - XSL module for PHP (dummy)
php7.0-zip - Zip module for PHP
php7.0-dba - DBA module for PHP

安裝完成後,編輯 /etc/php/7.0/fpm/php.ini 並替換 ;cgi.fix_pathinfo=1cgi.fix_pathinfo=0

sed -i `s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/` /etc/php/7.0/fpm/php.ini

然後重啟 PHP7.0-fpm

systemctl restart php7.0-fpm

三、更新 Nginx 配置

下面開始,我們假設您的域名是 example.com 您的伺服器 IP 是 192.0.2.2 (RFC 5737),並且您已經解析 example.com 的 A 記錄到您的伺服器 IP 192.0.2.2

無論用哪種方法安裝 Nginx 他們預設的 nginx.conf 都指定了載入 /etc/nginx/conf.d/ 目錄下的 *.conf 檔案,所以我們直接新建立一個 /etc/nginx/conf.d/example.conf 並增加一些基本的配置

直接把下面一整行命令複製貼上到你的終端

sudo cat >> /etc/nginx/conf.d/example.conf << EOF
server {
        listen 80;
        listen [::]:80;

# 指定網站目錄,可根據自己情況更換,建議放在 /var/www 目錄下
        root /var/www/example.com;
        index index.php index.html index.htm;

# 預設第一個域名,替換 example.com 為您的域名
        server_name example.com;

        location / {
            try_files $uri $uri/ =404;
        }

# 開啟 PHP7.0-fpm 模式
        location ~ .php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
}
EOF

然後重啟 Nginx

sudo nginx -t && sudo nginx -s reload

我們的目錄在 /var/www/example.com, 建立一個 phpinfo.php 並輸入 phpinfo() 函式

sudo cat >> /var/www/example.com/phpinfo.php << EOF
<?php phpinfo(); ?>
EOF

好了,此時在瀏覽器輸入 http://example.com/phpinfo.php,如果看到經典的 phpinfo 頁面則說明安裝成功,如果不成功,請仔細對比步驟查詢哪裡出錯

四、安裝 MySQL 5.7.x

經過多年生產環境的測試,我們推薦使用 Percona Server 代替原生的 MySQL

按照官方教程 下載最新版的 .deb 檔案

wget https://repo.percona.com/apt/percona-release_0.1-4.$(lsb_release -sc)_all.deb
sudo dpkg -i percona-release_0.1-4.$(lsb_release -sc)_all.deb

此時會在 /etc/apt/sources.list.d/percona-release.list 檔案加入官方源,在安裝之前可以先檢查是否存在

更新系統並安裝 Percona Server 5.7

sudo apt-get update
sudo apt-get install percona-server-server-5.7

安裝成功後系統會讓您輸入兩次 MySQL 的 root 密碼,請切記一定要使用隨機的、不可被人猜測的密碼

我親眼目測過很多新手第一次安裝用了弱密碼,後來伺服器就被人日,所以這裡強烈推薦安裝完 MySQL 後,執行一次安全設定,很簡單的一條命令

mysql_secure_installation

執行後會讓您選擇密碼強度,一般情況下選擇 1 或者 2

root@demo:~# mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root: 

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 請輸入 y 進行初始安全設定

There are three levels of password validation policy:

LOW    Length &gt;= 8
MEDIUM Length &gt;= 8, numeric, mixed case, and special characters
STRONG Length &gt;= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2 最強大的密碼當然要輸入 2

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) : n 如果之前設定了強密碼,則不需要重新更改 root 密碼,反之則按 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 移除匿名使用者,沒啥鳥用就直接移除吧

Normally, root should only be allowed to connect from
`localhost`. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y 關閉 root 遠端登入,不需要進行遠端登入的話就關了吧
Success.

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.

Remove test database and access to it? (Press y|Y for Yes, any other key for No) :y 移除 test 資料庫

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 重置資料庫許可權

再次提醒,密碼一定要隨機、不可猜測,使用弱密碼而導致伺服器被日的例子實在是數不清楚

做好初始安全設定後,我們就可以進行建立資料庫操作,首先使用 root 登入 MySQL

mysql -u root -p

會提示讓您輸入密碼,輸入密碼登陸後,建立一個名為 example 的資料庫

CREATE DATABASE example DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

由於手機端的流行,我們已經不再使用 utf-8 編碼,而改用 utf8mb4 這樣我們就可以在 MySQL 資料庫裡儲存 emoji 表情了,比如這樣 ??????

接著我們建立一個叫做 example_user 的使用者,使用強大的密碼並且賦予 example_database 資料庫許可權

GRANT ALL ON example.* TO `example_user`@`localhost` IDENTIFIED BY `這裡改成你要設定的強大的沒人能猜出來的隨機的密碼`;

終端會提示 Query OK, 0 rows affected, 1 warning (0.00 sec) 不用去管它

然後我們重新整理許可權

FLUSH PRIVILEGES;

沒問題就退出

EXIT;

然後我們測試一下資料庫,在 /var/www/example.com 目錄下建立一個 mysql-test.php

sudo cat >> /var/www/example.com/mysql-test.php << EOF  
<?php  
$dbname = `example`;    //MySQL 資料庫名
$dbuser = `example_user`;   //MySQL 使用者名稱
$dbpass = `你的強大的沒人可以猜出來的密碼`;
$dbhost = `localhost`;  //安裝在本地就用 localhost
$link = mysqli_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to `$dbhost`");
mysqli_select_db($link, $dbname) or die("Could not open the db `$dbname`");  
$test_query = "SHOW TABLES FROM $dbname";
$result = mysqli_query($link, $test_query);
$tblCnt = 0;
while($tbl = mysqli_fetch_array($result)) {  
  $tblCnt++;
  #echo $tbl[0]."&lt;br /&gt;
";
}
if (!$tblCnt) {  
  echo "MySQL is working fine. There are no tables.";
} else {
  echo "MySQL is working fine. There are $tblCnt tables.";
}
?>
EOF

建立完畢後訪問 http://example.com/mysql-test.php 如果出現 MySQL is working fine. There are no tables. 則說明 MySQL 工作正常。

好了,以上就是基本的 Ubuntu Server 16.04 安裝 LEMP 的教程,如有問題可以隨時發評論留言討論。

原文連結

Ubuntu Server 16.04.x (Xenial Xerus) 安裝 LEMP / LNMP 教程

相關文章