LEMP 意思是 Linux + Nginx, + MySQL + PHP。Nginx (发音能同 engine x) 是一款以性能取向的网页服务器, 较 Apache 及 Lighttpd 使用较少内存, 而配置 Nginx 及 PHP 的方法与 Apache 有点不同, 因为 Nginx 是透过 php-fpm (FastCGI Process Manager) 运作的, 以下是在 Ubuntu 16.04 安装 Nginx, MariaDB 及 PHP 的方法。
安装 Nginx
Ubuntu 16.04 安装 Nginx 比以前简单,因为 Nginx 已经在 Ubuntu 16.04 的默认 Repositories 内, 用 apt-get 指令安装:
$ sudo apt-get install nginx
Nginx 默认的 Document Root 并不存在, 手动建立及设定权限:
$ sudo chown www-data:www-data /var/www
安装好 Nginx 网页服务器后, 便可以用浏览器检查 Nginx 是否可以连接, 例如:
http://server-ip/
如果看到 “Welcome to Nginx!” 页面便安装成功了。
安装 MySQL Server
安装 MySQL 同样可以用 apt-get 完成, 执行以下指令安装 MySQL Server:
安装时 MySQL 会询问要设置的 root 新密码, 或者可以执行 mysql_secure_installation 修改:
安装 PHP
Nginx 要使用 PHP 需要连同 php-fpm (Fast CGI Process Manager) 一同安装:
安装好 PHP 后, 开启档案 /etc/php/7.0/fpm/php.ini, 找到 cgi.fix_pathinfo=1, 将 1 改成 0, 即这样:
cgi.fix_pathinfo=0
储存盘案后, 可以用以下指令启动 php-fpm
现在设定 Nginx 使用 PHP 处理 PHP 网页, 在 Nginx 透过 Server Block (即 Apache 的 Virtual Hosts) 设定。开启 Nginx 默认 Server Block 设定档:
内容像以下:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; index index.html index.htm index.nginx-debian.html; server_name _; location / { try_files $uri $uri/ =404; } } |
将档案内容改成以下这样:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; index index.php index.html index.htm index.nginx-debian.html; server_name your-IP; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.0-fpm.sock; } location ~ /\.ht { deny all; } } |
储存盘案后离开编辑器, 现在可以重新启动 Nginx:
要测试 PHP, 可以放一个简单的 PHP Script 到 /var/www/html/ 目录, 这里:
输入以下几行程式码:
|
1 2 3 |
<?php phpinfo(); ?> |
然后在浏览器看看 http://localhost/info.php, 如果看到 PHP 的设定资料便表示安装完成了。