這裡介紹在 Ubuntu Linux 中安裝與設定 LEMP 網頁伺服器的詳細步驟,以及安全性相關的注意事項。

LEMP(Linux、nginx、MySQL、PHP)是現在很熱門的網頁伺服器組合,它是將 LAMP 中的 Apache 以 nginx 取代,以提升伺服器的的負載能力,以下介紹在 Ubuntu Linux 中安裝 LEMP 的步驟。

這裡我是使用 Linode VPS 上面的 Ubuntu Linux 14.04 為測試環境,不過這些安裝方式應該適用於各種 Ubuntu Linux 版本,Ubuntu Linux 本身系統的設定請參考 Linode VPS 安裝 Ubuntu Linux 與基本安全性設定

nginx

使用 apt 安裝 nginx 套件:

apt-get install nginx

編輯 /etc/nginx/nginx.conf,調整 worker_processes 的數量:

worker_processes 1;

如果是 1 顆 CPU 的主機,就直接將 worker_processes 設定為 1 就好了,如果是多顆 CPU 的話,可以設定為 auto,詳細說明請參考 nginx 的文件

調整 events 設定,將 worker_connections 設定成適當的數值:

events {
  worker_connections 1024;
  use epoll;
  multi_accept on;
}

Linux 2.6 以後的核心可以使用 epoll 這種比較有效率的方式。

server_tokens 關掉,這樣網頁上就不會顯示 nginx 的版本資訊,減低被攻擊的機率:

http {
  # ...
  server_tokens off;
  # ...
}

對各種文字檔案啟用 gzip 壓縮,設定啟用壓縮的最小資料長度:

http {
  # ...

  gzip on;
  gzip_disable "msie6";

  # gzip_vary on;
  # gzip_proxied any;
  # gzip_comp_level 6;
  # gzip_buffers 16 8k;
  # gzip_http_version 1.1;
  gzip_min_length 1000;
  gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

  # ...
}

關於 nginx 的 gzip 的壓縮設定,可以參考 nginx 的文件

接著是設定 virtual host,編輯 /etc/nginx/sites-available/default,不過這個部分就要根據自己的網站來調整了,下面這個是一個簡單的範例:

server {
  listen   80;

  root /usr/share/nginx/html;
  index index.php index.html index.htm;

  server_name gtwang.org;

  location / {
          try_files $uri $uri/ /index.html;
  }

  error_page 404 /404.html;

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

  # pass the PHP scripts to FastCGI server listening on the php-fpm socket
  location ~ \.php$ {
          try_files $uri =404;
          fastcgi_pass unix:/var/run/php5-fpm.sock;
          fastcgi_index index.php;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          include fastcgi_params;
  }

  # Directives to send expires headers and turn off 404 error logging.
  location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires 7d;
    log_not_found off;
  }
}

最後重新啟動 nginx 服務:

sudo service nginx start

PHP

安裝 PHP 相關的套件:

sudo apt-get install php5-fpm php5-mysql php5-gd php5-imagick php5-apcu php5-cli

基本的 LEMP 只要 php5-fpmphp5-mysql 就可以運作了,其餘的部分可以自行選擇要不要裝。

php5-gdphp5-imagick 可以讓 PHP 可以自動產生圖片的縮圖,WordPress 這類的網站通常都會需要,以 WordPress 而言如果沒有裝的話,您會發現網頁上的圖都是原圖,這會影響網頁的載入速度,所以建議是一起裝起來。

php5-apcu 是 PHP 的 APC 套件,它利用快取技術縮短 PHP 程式碼的解譯時間,可有效改善 PHP 執行效能,詳細說明請參考 Alternative PHP Cache

最後的 php5-cli 則是在指令列除錯用的,這個是讓您可以在命令列執行 PHP 程式,如果只是單純架站,不會用到這樣的功能的話,就可以自己拿掉。

編輯 /etc/php5/fpm/php.ini,將 cgi.fix_pathinfo 功能關閉:

cgi.fix_pathinfo=

並將 expose_php 關閉,不要讓 PHP 顯示自己的版本資訊:

expose_php = Off

然後重新啟動 PHP 服務:

sudo service php5-fpm restart

MySQL

安裝 MySQL 伺服器:

sudo apt-get install mysql-server

接著將一些不安全的預設設定移除,執行

sudo mysql_secure_installation

這時候需要回答一系列的問題,不想細看的話,全部都回答 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? [Y/n] 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.

Disallow root login remotely? [Y/n] y
... 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? [Y/n] 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? [Y/n] y
 ... Success!

Cleaning up...

這樣 MySQL 的資料庫就安裝完成了。後來如果要重設 MySQL 資料庫的管理者密碼,可以執行

sudo dpkg-reconfigure mysql-server-5.0

Exim

通常網頁伺服器也會有發信的需求,像 WordPress 就會需要發一些通知信給管理者,所以就算不作為郵件伺服器,也會需要安裝一個簡單的 MTA,對於這種指發信不收信的主機,使用 Exim 會是一個不錯的做法:

apt-get install exim4-daemon-light mailutils

設定 Exim:

dpkg-reconfigure exim4-config

以上是整個 LEMP 網頁伺服器安裝的大約安裝與設定步驟,因為有許多設定都會受到網站的性質、流量與主機硬體規格影響,所以只能大約列出一些重點,實際在安裝時,都還是需要自行調整一些細部的設定。

參考資料