使用 PHP 和 MariaDB 安装 Nginx(作为 MySQL 的替代品)- LEMP -on Ubuntu 15.10 (Wiley Werewolf)
本教程适用于这些操作系统版本
- Ubuntu 14.04 LTS (Trusty Tahr)
在此页
- 1 条初步说明
- 2 安装 MariaDB
- 3 安装 Nginx
- 4 安装 PHP5
- 5 配置 Nginx
- 6 在 PHP 中获得 MariaDB/MySQL 支持
- 7 让 PHP-FPM 使用 TCP 连接
- 8本教程虚拟机镜像下载
- 9 个链接
Nginx(发音为“engine x”)是一个免费、开源、高性能的 HTTP 服务器。 Nginx 以其稳定性、丰富的功能集、简单的配置和低资源消耗而闻名。本教程展示了如何在具有 PHP 支持(通过 PHP-FPM)和 MariaDB(作为 MySQL 替代品)的 Ubuntu 15.10 服务器上安装 Nginx。术语 LEMP 代表:Linux + Nginx + MySQL + PHP。
1 初步说明
在本教程中,我将使用 IP 地址为 192.168.1.100 的主机名 server1.example.com。这些设置可能因您而异,因此您必须在适当的地方替换它们。
2 安装 MariaDB
我将安装 MariaDB 而不是 MySQL,MariaDB 是由原始 MySQL 创始人 Monty Widenius 维护的 MySQL 分支,与 MySQL 相比,它在速度和功能方面有一些改进。为了安装 MariaDB,运行:
sudo apt-get -y install mariadb-server mariadb-client
MariaDB 目前没有为 root 用户设置密码。运行mysql_secure_installation命令配置密码并移除test数据库。
sudo mysql_secure_installation
你会被问到这些问题:
Enter current password for root (enter for none): <-- press enter
Set root password? [Y/n] <-- y
New password: <-- Enter the new MariaDB root password here
Re-enter new password: <-- Repeat the password
Remove anonymous users? [Y/n] <-- y
Disallow root login remotely? [Y/n] <-- y
Reload privilege tables now? [Y/n] <-- y
3 安装 Nginx
Nginx 可作为 Ubuntu 15.10 的软件包使用。可能是您的服务器上安装了 apache 网络服务器,这可能会导致冲突。检查 apache 是否未运行:
ps aux | grep apache2
如果此命令没有返回任何结果,请继续安装 Nginx。
删除 apache,以防上述命令使用这些命令显示一些 apache 进程:
sudo systemctl stop apache2.service
sudo systemctl disable apache2.service
sudo apt-get remove apache2
现在我们可以安装nginx了。
sudo apt-get install nginx
之后启动 Nginx:
sudo systemctl start nginx.service
sudo systemctl enable nginx.service
在浏览器中输入您的 Web 服务器 IP 地址或主机名(例如 http://192.168.1.100),您应该会看到以下页面:

为什么在该页面中显示“Apache2”而不是 Nginx?页面的文字并不反映实际运行的Web服务器,上面的页面只是Apache和Nginx使用的文件根/var/www/html/中Ubuntu上的默认页面。要验证 Nginx 是否真的在为该页面提供服务,您可以查看 Firefox Network 分析控制台中的 HTTP 标头:

或者你检查 nginx 运行的 shell:
ps aux | grep nginx
结果应该像这样显示 nginx 进程:

4 安装PHP5
我们可以通过PHP-FPM让PHP5在Nginx中运行。 PHP-FPM(FastCGI 进程管理器)是一种替代的 PHP FastCGI 实现,具有一些对任何规模的站点(尤其是繁忙的站点)有用的附加功能,我们按如下方式安装:
sudo apt-get install php5-fpm
PHP-FPM 是一个守护进程(带有初始化脚本 php5-fpm),它在套接字 /var/run/php5-fpm.sock 上运行 FastCGI 服务器。
5 配置 Nginx
Nginx 配置位于我们现在在 nano 编辑器中打开的文件 /etc/nginx/nginx.conf 中:
sudo nano /etc/nginx/nginx.conf
配置简单易懂(你可以在这里了解更多信息:http://wiki.nginx.org/NginxFullExample2)
首先(这是可选的)将 keepalive_timeout 设置为一个合理的值:
[...]
keepalive_timeout 2;
[...]
虚拟主机在服务器 {} 容器中定义。默认虚拟主机在文件 /etc/nginx/sites-available/default 中定义 - 让我们按如下方式修改它:
sudo nano /etc/nginx/sites-available/default
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}
服务器名称 _;使它成为默认的通用虚拟主机(当然,您也可以在此处指定一个主机名,例如 www.example.com)。
我将 index.php 添加到索引行。根/usr/share/nginx/html;表示文档根目录是目录/usr/share/nginx/html。
PHP 的重要部分是 location ~ \.php${} 节。取消注释以启用它。请注意,在 PHP 位置节中启用行 \include snippets/fastcgi-php.conf;\ 以防止零日攻击很重要(请参阅 http://forum.nginx.org/read.php? 2,88845,页数=3)。
现在保存文件并重新加载 nginx:
sudo service nginx reload
接下来打开/etc/php5/fpm/php.ini...
sudo nano /etc/php5/fpm/php.ini
...并设置 cgi.fix_pathinfo=0:
[...]
; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is. For more information on PATH_INFO, see the cgi specs. Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec. A setting
; of zero causes PHP to behave as before. Default is 1. You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=0
[...]
重新加载 PHP-FPM:
sudo service php5-fpm reload
现在在文档根目录 /var/www/html 中创建以下 PHP 文件:
sudo nano /var/www/html/info.php
<?php
phpinfo();
?>
现在我们在浏览器中调用该文件(例如 http://192.168.1.100/info.php):

如您所见,PHP5 正在运行,并且它通过 FPM/FastCGI 运行,如服务器 API 行所示。如果进一步向下滚动,您将看到所有已在 PHP5 中启用的模块。 MySQL 没有在那里列出,这意味着我们在 PHP5 中还没有 MySQL 支持。
6 在 PHP 中获得 MariaDB/MySQL 支持
要在 PHP 中获得 MySQL 支持,我们可以安装 php5-mysqlnd 包。安装一些其他 PHP5 模块是个好主意,因为您的应用程序可能需要它们。您可以像这样搜索可用的 PHP5 模块:
apt-cache search php5
选择你需要的并像这样安装它们:
sudo apt-get install php5-mysqlnd php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-sqlite php5-tidy php5-xmlrpc php5-xsl
APCu 是一个免费的 PHP 操作码缓存器,用于缓存和优化 PHP 中间代码。它类似于其他 PHP 操作码缓存器,例如 eAccelerator 和 Xcache。强烈建议安装其中之一以加速您的 PHP 页面。
APC 可以按如下方式安装:
sudo apt-get install php5-apcu
现在重新加载 PHP-FPM:
sudo service php5-fpm reload
现在在浏览器中重新加载 http://192.168.1.100/info.php 并再次向下滚动到模块部分。你现在应该在那里找到很多新模块,包括 MySQL 模块:

7 让 PHP-FPM 使用 TCP 连接
默认情况下,PHP-FPM 正在监听套接字 /var/run/php5-fpm.sock。也可以使 PHP-FPM 使用 TCP 连接。为此,打开 /etc/php5/fpm/pool.d/www.conf...
sudo nano /etc/php5/fpm/pool.d/www.conf
...并使 listen 行如下所示:
[...]
;listen = /var/run/php5-fpm.sock
listen = 127.0.0.1:9000
[...]
这将使 PHP-FPM 侦听 IP 127.0.0.1(本地主机)上的端口 9000。确保您使用的端口未在您的系统上使用。
然后重新加载 PHP-FPM:
sudo php5-fpm reload
接下来检查您的 nginx 配置和所有虚拟主机并更改行 fastcgi_pass unix:/var/run/php5-fpm.sock;到 fastcgi_pass 127.0.0.1:9000;,例如像这样:
sudo nano /etc/nginx/sites-available/default
[...]
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php5-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
# fastcgi_pass unix:/var/run/php5-fpm.sock;
}
[...]
最后重新加载nginx:
sudo service nginx reload
8 本教程虚拟机镜像下载
本教程可随时使用 ovf/ova 格式的虚拟机映像,与 VMWare 和 Virtualbox 兼容。虚拟机映像使用以下登录详细信息:
SSH/外壳登录
用户名:管理员
密码:howtoforge
此用户具有 sudo 权限。
登录
用户名:root
密码:howtoforge
VM 的 IP 是 192.168.1.100,可以在文件 /etc/network/interfaces 中更改。请更改以上所有密码以保护虚拟机。
9个链接
- nginx:http://nginx.net/
- nginx 维基:http://wiki.codemongers.com/Main
- PHP:http://www.php.net/
- PHP-FPM:http://php-fpm.org/
- MySQL:http://www.mysql.com/
- Ubuntu:http://www.ubuntu.com/