The process of configuring nginx+php+fastcgi on Ubuntu

Source: Internet
Author: User
These two days need to build nginx and PHP environment on the Ubuntu14.04, the process of configuration in the fastcgi of the problem spent a lot of time, the online data mostly in disrepair, so decided to record the whole process, share to everyone.


Get ready:

Apt-get Update


1. Install Nginx and start

Apt-get Install Nginxsudo/etc/init.d/nginx Start

2. Installing PHP and related

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

3. FastCGI

Before running the PHP site, you need to understand that the next fastcgi,fastcgi is a scalable, high-speed interface between the HTTP server and the Dynamic scripting language, it uses the C/s structure, you can separate the HTTP server and the script resolution server, When the HTTP server encounters a dynamic request, the request is forwarded to the fastcgi process, the fastcgi process executes the dynamic script, and the result is returned to the HTTP server, and the HTTP server finally returns the result to the browser, which greatly improves the response speed of the request.


There are several ways to implement FastCGI, php-cgi, PHP-FPM, spawn-fcgi

PHP-CGI is PHP's own fastcgi manager, starting php-cgi can use the following command:

Php-cgi-b 127.0.0.1:9000

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


PHP-FPM is the newly added CGI manager from PHP 5.3.3, does not require a reboot after changing the PHP configuration, and can be restarted quickly even after being killed, due to the addition of daemons.


SPAWN-FCGI is a generic fastcgi manager, not just a scripting language for PHP. But it's not as php-fpm as efficiency and CPU usage.


4. php-fpm

Since PHP-FPM is the best way to go, let's configure it now. Since PHP-FPM is a separate process, it is necessary to communicate with Nginx. There are currently two modes of communication, TCP and socket. The differences and configuration of the two methods can be found in these two articles:

Nginx and PHP-FPM communication using UNIX sockets or TCP, and their configuration

PHP-FPM Tcpsocket vs Unixsocket


Here is a brief description of the configuration, both of which are mainly modified Nginx configuration files (/etc/nginx/sites-available/default) and FPM configuration files (/etc/php5/fpm/pool.d/www.conf).


The TCP configuration requires the Fastcgi_pass parameter of the corresponding reverse proxy in the Nginx configuration file to be changed to 127.0.0.1:9000, and the Listen parameter in the FPM configuration file is modified to 127.0.0.1:9000.


The socket configuration method needs to change the Fastcgi_pass parameter of the corresponding reverse proxy in the Nginx configuration file to Unix:/dev/shm/fpm-cgi.sock, modifying the Listen parameter in the FPM configuration file to/dev/shm/ Fpm-cgi.sock, if you consider high concurrency you can change the Listen.backlog parameter to-1, the memory backlog is infinitely large. In addition, you can increase the number of concurrent connections at the kernel level and modify the/etc/sysctl.conf:

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

After the two configurations are modified, you need to restart PHP-FPM and Nginx.

sudo service php5-fpm Restartsudo/etc/init.d/nginx Reload


5. Run the PHP site

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

Create a Site Directory:

sudo mkdir/var/www/

In the Nginx configuration file, modify the root directory to root/var/www;

To add a test page:

sudo vi/var/www/test.php

Add the following code:

 
  

At this point in the browser input address http://localhost/test.php, you can see the PHP information.


Common commands:

1. Check for Nginx startup error

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


If you feel that you can help, but also hope to help the top, thank you:)
Personal blog: http://blog.csdn.net/zhaoxy2850
Address of this article:
Reprint please indicate the source, 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.