Install Nginx under Ubuntu (Nginx + php5-fpm)

Source: Internet
Author: User
Nginx Introduction I will not wordy, first say the installation environment of the system.


0. Installation Environment

System: Ubuntu 14.04

PHP version: 5.5.9

Existing Server Software: Apache (basic not affected, will be mentioned later)


1. Installing Nginx

sudo apt-get install Nginx
If you have Apache installed, and Apache is running at this time, then please modify the configuration file (no students skip directly):
#打开配置文件sudo Vim/etc/nginx/sites-available/default

Then follow the two changes (in order not to clash with Apache):

server {        #修改这里 I change 80 to 88listen Default_server;        #还有这里 also change to the listening port you want listen [::]:88 default_server ipv6only=on;        .....

At this point, we test the Nginx installation success, did not make the above modification directly in the browser input localhost, changed the memory plus port, my is localhost:88, if the following interface is installed successfully:


2. Installing PHP5-FPM

Also use Apt-get:

sudo apt-get install PHP5-FPM


3. Modify the configuration file to support PHP

Also open the configuration file:

#打开配置文件sudo Vim/etc/nginx/sites-available/default
First, add the index.php after index:
server {Listen Default_server;listen [::]:88 default_server ipv6only=on;root/usr/share/nginx/html;        #修改这里, add Index.phpindex index.php index.html index.htm;# make site accessible from Http://localhost/server_name l after index Ocalhost;        ....
Then, you can remove the following comments, see clearly 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 has "cgi.fix_pathinfo = 0;" 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;}
Then reboot:

4. Testing

#修改权限sudo chmod 777/usr/share/nginx/html/#新建测试文件vim/usr/share/nginx/html/index.php
It's good to enter the following in the file:

 
  
Open the browser, enter the address again, localhost or localhost: port (I am localhost:88), see the following interface is successful:



Issue: "502 Bad Gateway" appears when you open a webpage after installation

Some students may follow some online tutorials installed, the test shows "502 Bad Gateway", oh, don't be nervous, because the tutorial is wrong only ....

Many tutorials are relatively long ago, so when you modify a configuration file, you will be told that the comments get these 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, for PHP 5.3 and below, after PHP 5.4, PHP5-FPM is not listening to 9000 port, you can look at its configuration file:

#php5 configuration file for-fpm sudo vim/etc/php5/fpm/pool.d/www.conf
There is a paragraph below, note the last line of Listen, the original version is listen = 127.0.0.1:9000, but the latest version is the following:

; The address on which to accept FastCGI requests.; Valid syntaxes is:;   ' Ip.add.re.ss:port '    -to listen in a TCP socket to a specific address on;                            a specific port;;   ' Port '                 -to listen in 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, so 502 .....

Workaround:

Open Nginx configuration file:

#打开配置文件sudo Vim/etc/nginx/sites-available/default
Then we annotate the sentence of 9000 and replace it 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;}
Should be able to work normally, do not believe to try?
  • 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.