Install Nginx under Ubuntu (Nginx + Php5-fpm)

Source: Internet
Author: User
Install Nginx under Ubuntu (Nginx + Php5-fpm) nginx introduction I will not be arrogant, first talk about the system installation environment.


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:

 
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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.