Nginx
在 Ubuntu 14.04 LTS 上安装带有 PHP5(和 PHP-FPM)和 MySQL 支持(LEMP)的 Nginx
版本 1.0
作者:Falko Timme,由 Srijan Kishore 更新
最后编辑 07/ 2014 年 5 月
Nginx(发音为“engine x”)是一个免费、开源、高性能的 HTTP 服务器。 Nginx 以其稳定性、丰富的功能集、简单的配置和低资源消耗而闻名。本教程展示了如何在支持 PHP5(通过 PHP-FPM)和 MySQL 支持的 Ubuntu 14.04 服务器上安装 Nginx(LEMP =Linux nginx(发音为“engine x”) MySQL PHP) .
我不保证这对您有用!
在本教程中,我使用主机名server1.example.com,IP 地址为192.168.0.100。这些设置可能与您不同,因此您必须在适当的情况下替换它们。
我使用 root 权限运行本教程中的所有步骤,因此请确保您以 root 身份登录:
sudo su
为了安装 MySQL,我们运行
apt-get install mysql-server mysql-client
您将被要求提供 MySQL root 用户的密码 - 该密码对用户 root@localhost 以及 root@server1.example.com 有效,因此我们稍后不必手动指定 MySQL root 密码:
MySQL“root”用户的新密码:MySQL“root”用户的重复密码:
Nginx 作为 Ubuntu 14.04 的软件包提供,我们可以安装它,由于 Apache2 是默认安装的,所以我们首先将其删除,然后安装 nginx:
service apache2 stop
update-rc。 d -f apache2 remove
apt-get remove apache2
apt-get install nginx
之后启动 nginx:
service nginx start
类型在浏览器中输入您的网络服务器的 IP 地址或主机名(例如 http://192.168.0.100),您应该看到以下页面:
默认Ubuntu 14.04 上的 nginx 文档根目录是/usr/share/nginx/html。
我们可以通过 PHP-FPM(PHP-FPM (FastCGI Process Manager))让 PHP5 在 nginx 中工作是另一种 PHP FastCGI 实现,具有一些对任何规模的网站(尤其是繁忙的网站)有用的附加功能,我们安装如下:
apt-get install php5-fpm
PHP-FPM 是一个守护进程(带有初始化脚本php5-fpm),在socket/var/run/php5-fpm.sock上运行FastCGI服务器。
nginx 配置位于我们现在打开/etc/nginx/nginx.conf:
vi /etc/nginx/nginx.conf
配置很容易理解(你可以在这里了解更多:http: //wiki.nginx.org/NginxFullExample 和这里:http://wiki.nginx.org/NginxFullExample2)
首先(这是可选的)调整工作进程的数量并将keepalive_timeout 设置为合理的值:
[...]worker_processes4;[...]keepalive_timeout 2;[...] |
虚拟主机在服务器容器中定义。默认虚拟主机在文件 /etc/nginx/sites-available/default 中定义 - 让我们修改它如下:
vi /etc/nginx/sites-available/default
[...]server { listen 80; listen [::]:80 default_server ipv6only=on; root /usr/share/nginx/html; index index.php index.html index.htm; # Make site accessible from http://localhost/ server_name _; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ /index.html; # Uncomment to enable naxsi on this location # include /etc/nginx/naxsi.rules } location /doc/ { alias /usr/share/doc/; autoindex on; allow 127.0.0.1; allow ::1; deny all; } # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests #location /RequestDenied { # proxy_pass http://127.0.0.1:8080; #} #error_page 404 /404.html; # redirect server error pages to the static page /50x.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 127.0.0.1:9000 # location ~ /.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+/.php)(/.+)$; # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini # With php5-cgi alone: #fastcgi_pass 127.0.0.1:9000; # With php5-fpm: fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # location ~ //.ht { deny all; }}[...] |
取消注释这两个listen,使nginx监听端口80 IPv4和IPv6。
server_name _;使其成为默认的包罗万象的虚拟主机(当然,您也可以在这里指定一个主机名,例如www.example .com)。
我在indexline.root /usr/share/nginx/html中添加了index.php;表示文档根目录是/usr/share/nginx/html目录。
PHP 的重要部分是位置 ~ /.php$ {} 节。取消注释以启用它。请注意,我添加了 linetry_files $uri =404; 以防止零日漏洞(请参阅http://wiki.nginx.org/Pitfalls#Passing_Uncontrol_Requests_to_PHP 和 http://forum.nginx.org/read.php?2,88845 ,page=3).
现在保存文件并重新加载 nginx:
服务 nginx 重新加载
接下来打开 /etc/php5/fpm/php.ini...
vi /etc/php5/fpm/php.ini
... 和 setcgi.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-pathinfocgi.fix_pathinfo=0[...] |
Reload PHP-FPM:
service php5-fpm reload
Now create the following PHP file in the document root/usr/share/nginx/html:
vi /usr/share/nginx/html/info.php
<?phpphpinfo();?> |
Now we call that file in a browser (e.g.http://192.168.0.100/info.php):
As you see, PHP5 is working, and it's working through FPM/FastCGI, as shown in theServer APIline. If you scroll further down, you will see all modules that are already enabled in PHP5. MySQL is not listed there which means we don't have MySQL support in PHP5 yet.
To get MySQL support in PHP, we can install thephp5-mysqlpackage. It's a good idea to install some other PHP5 modules as well as you might need them for your applications. You can search for available PHP5 modules like this:
apt-cache search php5
Pick the ones you need and install them like this:
apt-get install php5-mysql 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-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl
APC is a free and open PHP opcode cacher for caching and optimizing PHP intermediate code. It's similar to other PHP opcode cachers, such as eAccelerator and Xcache. It is strongly recommended to have one of these installed to speed up your PHP page.
APC can be installed as follows:
apt-get install php-apc
Now reload PHP-FPM:
service php5-fpm reload
Now reloadhttp://192.168.0.100/info.phpin your browser and scroll down to the modules section again. You should now find lots of new modules there, including the MySQL module:
By default PHP-FPM is listening on the socket/var/run/php5-fpm.sock. It is also possible to make PHP-FPM use a TCP connection. To do this, open/etc/php5/fpm/pool.d/www.conf...
vi /etc/php5/fpm/pool.d/www.conf
... and make thelistenline look as follows:
[...];listen = /var/run/php5-fpm.socklisten = 127.0.0.1:9000[...] |
This will make PHP-FPM listen on port9000on the IP127.0.0.1(localhost). Make sure you use a port that is not in use on your system.
Then reload PHP-FPM:
php5-fpm reload
Next go through your nginx configuration and all your vhosts and change the linefastcgi_pass unix:/var/run/php5-fpm.sock;tofastcgi_pass 127.0.0.1:9000;, e.g. like this:
vi /etc/nginx/sites-available/default
[...]location ~ /.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+/.php)(/.+)$; # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini # With php5-cgi alone: fastcgi_pass 127.0.0.1:9000; # With php5-fpm: #fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params;}[...] |
Finally reload nginx:
service nginx reload
If you want to serve CGI/Perl scripts with nginx, please read this tutorial:Serving CGI Scripts With Nginx On Debian Squeeze/Ubuntu 11.04
The recommended way is to usefcgiwrap(chapter 4).