Install Nginx under Ubuntu (Nginx + Php5-fpm), nginxphp5-fpm
I won't bother with the introduction of nginx. Let's talk about the system installation environment first.
0. installation environment
System: Ubuntu 14.04
PHP version: 5.5.9
Existing server software: Apache (basically not affected, which will be mentioned later)
1. Install Nginx
sudo apt-get install nginx
If you have installed Apache and Apache is running at this time, modify the configuration file first (skip this step if you do not have it ):
# Open the configuration file sudo vim/etc/nginx/sites-available/default
Modify the settings as follows (in order not to conflict with Apache ):
Server {# change the value of "80" to "88 listen 88 default_server". # change the value of "80" to "88 listen 88 default_server". # change the value of "listen" to the desired listening port [:]. 88 default_server defaults 6only = on ;.....
At this point, we will test whether nginx is successfully installed. If the above modification is not done, enter localhost in the browser. Remember to add the port after the modification. My address is localhost: 88, if the following interface appears, the installation is successful:
2. Install php5-fpm
Also use apt-get:
sudo apt-get install php5-fpm
3. modify the configuration file to support PHP
Open the configuration file:
# Open the configuration file sudo vim/etc/nginx/sites-available/default
First, add index. php after index:
Server {listen 88 default_server; listen [:]: 88 default_server defaults 6only = on; root/usr/share/nginx/html; # modify here and add index after index. phpindex index. php index.html index.htm; # Make site accessible from http: // localhost/server_name localhost ;....
Then, you can remove the following comments to see where it is:
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000#location ~ \.php$ {#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;}
Restart:
# This sentence although not necessary but can give you check configuration file errors sudo nginx-s reload # restart php5-fpmsudo/etc/init. d/php5-fpm restart # restart nginxsudo/etc/init. d/nginx restart
4. Test
# Modify sudo chmod 777/usr/share/nginx/html/# create a test file vim/usr/share/nginx/html/index. php
Enter the following content in the file:
<?phpphpinfo();?>
Open your browser and enter the address localhost or localhost: Port (I am localhost: 88) again. If you see the following interface, the operation is successful:
Problem: "502 Bad Gateway" appears when you open the webpage after installation"
Some people may follow some online tutorials to install the service and the "502 Bad Gateway" is displayed during the test. Oh, don't be nervous, because the tutorial is wrong ....
Many tutorials have been used for a long time, so when you modify the configuration file, you will be notified to comment out the three lines:
location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params;}
Here the fastcgi_pass variable should be wrong, applicable to PHP 5.3 and below, after PHP 5.4, the php5-fpm is not listening to port 9000, you can take a look at its configuration file:
# Php5-fpm configuration file sudo vim/etc/php5/fpm/pool. d/www. conf
There is a paragraph as follows. Note that the listen in the last line is the original version of listen = 127.0.0.1: 9000, but the latest version is as follows:
; The address on which to accept FastCGI requests.; Valid syntaxes are:; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific address on; a specific port;; 'port' - to listen on a TCP socket to all addresses on a; specific port;; '/path/to/unix/socket' - to listen on a unix socket.; Note: This value is mandatory.listen = /var/run/php5-fpm.sock
So, 502 .....
Solution:
Open the nginx configuration file:
# Open the configuration file sudo vim/etc/nginx/sites-available/default
Then, the 9000 clause is commented out and replaced with the new fast_pass variable:
location ~ \.php$ {#fastcgi_pass 127.0.0.1:9000;fastcgi_pass unix:/var/run/php5-fpm.sock;fastcgi_index index.php;include fastcgi_params;}
It should work normally. Don't believe it?