Configure nginx + php + fastcgi on Ubuntu

Source: Internet
Author: User
The nginx + php + fastcgi configuration process on Ubuntu needs to be set up on Ubuntu14.04 for the nginx and php environments over the past two days. during the configuration process, fastcgi issues took a lot of time, most of the online materials were out of service for years, so I decided to record the entire process and share it with you.


Preparation:

apt-get update


1. install nginx and start

apt-get install nginxsudo /etc/init.d/nginx start

2. install php and related

sudo apt-get install php5-cli php5-cgi php5-fpm php5-mcrypt php5-mysql

3. FastCGI

Before running a php site, you must first understand FastCGI. FastCGI is a scalable and high-speed interface for communication between HTTP server and dynamic scripting languages. it adopts a C/S structure, the HTTP server and the script parsing server can be separated. when the HTTP server encounters a dynamic request, the request is forwarded to the FastCGI process. The FastCGI process executes the dynamic script and then returns the result to the HTTP server, the HTTP server finally returns the result to the browser, which greatly improves the request response speed.


FastCGI can be implemented in several ways: PHP-CGI, PHP-FPM, and Spawn-FCGI.

The PHP-CGI is the FastCGI manager that comes with PHP, and the following command can be used to start the PHP-CGI:

Php-cgi-B 127.0.0.1: 9000

PHP-CGI has two problems, one is to change php. ini after the restart of php-cgi to take effect, the second is the php-cgi process crashes or is killed after php can not run.


The PHP-FPM is a new CGI manager from PHP 5.3.3 and does not need to be restarted after changing the PHP configuration, and since it is added to the Daemon, it can be restarted quickly even after being killed.


Spawn-FCGI is a common FastCGI manager, not just for PHP. However, it is inferior to PHP-FPM in terms of efficiency and CPU usage.


4. PHP-FPM

Now that the PHP-FPM is the best way, let's configure it now. Since the PHP-FPM is an independent process, it needs to communicate with Nginx. Currently, there are two communication modes: tcp and socket. For the differences between the two methods and the configuration method, see the two articles:

Use unix socket or TCP for communication between nginx and php-fpm and its configuration

Php-fpm TcpSocket vs UnixSocket


The configuration method is briefly described here. both methods mainly modify the nginx configuration file (/etc/nginx/sites-available/default) and fpm configuration files (/etc/php5/fpm/pool. d/www. conf ).


In TCP configuration mode, you need to change the fastcgi_pass parameter of the reverse proxy in the nginx configuration file to 127.0.0.1: 9000, and change the listen parameter in the fpm configuration file to 127.0.0.1: 9000.


The Socket configuration method needs to change the fastcgi_pass parameter of the reverse proxy in the nginx configuration file to unix:/dev/shm/fpm-cgi.sock, modify the listen parameter in the fpm configuration file to/dev/shm/fpm-cgi.sock. if you consider high concurrency, you can set the listen. the backlog parameter is changed to-1, and the memory backlog is infinite. In addition, you can increase the number of concurrent connections at the kernel level and modify/etc/sysctl. conf:

sudo echo 'net.core.somaxconn = 2048' >> /etc/sysctl.confsudo sysctl -p

After the two configuration methods are modified, you must restart php-fpm and nginx.

sudo service php5-fpm restartsudo /etc/init.d/nginx reload


5. run the php Site

After the above configuration is complete, we can write a simple php page to test it.

Create a website directory:

sudo mkdir /var/www/

In the nginx configuration file, change the root directory to root/var/www;

Add a test page:

sudo vi /var/www/test.php

Add the following code:

 

Enter http: // localhost/test. php in the browser to view the php information.


Common commands:

1. check for nginx startup errors

sudo nginx -c /etc/nginx/nginx.conf -t


If you feel helpful to yourself, I hope you can help me with it. thank you :)
Blog: http://blog.csdn.net/zhaoxy2850
Address:
Please indicate the source for reprinting. thank you!

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.