Configure nginx + php + fastcgi on Ubuntu, nginxfastcgi

Source: Internet
Author: User

Configure nginx + php + fastcgi on Ubuntu, nginxfastcgi
In the past two days, we need to build the nginx and php environments on Ubuntu14.04. During the configuration process, we spent a lot of time on fastcgi issues. 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 needs to understand FastCGI before running the php site. FastCGI is a scalable and high-speed interface for communication between HTTP server and dynamic scripting languages. It adopts the 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.
There are several ways to implement FastCGI, PHP-CGI, PHP-FPM, Spawn-FCGIPHP-CGI is PHP built-in FastCGI manager, start the PHP-CGI can use the following command: php-cgi-B 127.0.0.1: there are two problems with 9000PHP-CGI. One is to change php. after ini, You need to restart php-cgi to take effect. Second, after the php-cgi process crashes or is killed, php cannot 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. Since 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. See the two articles for differences and Configuration Methods: Use unix socket or TCP for nginx and php-fpm communications, and configure 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. After running the php site and above configuration, 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:
<?php phpinfo();?>

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: http://blog.csdn.net/zhaoxy_thu/article/details/41851389
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.