Quickly build LNMP environment in ubuntu [graphic tutorial]

Source: Internet
Author: User
Tags curl fpm install php php file php and socket unix domain socket

Note: During my testing, nginx is integrated with php-fpm in the centos system, and nginx access to the home page is blank.


1. Install nginx

Run the following command to install nginx:

Sudo apt-get-y install nginx


After nginx is installed, let's check what files are installed in nginx. Run the following command to view details:

Dpkg-S nginx


As shown in the figure above, the default installation location of nginx is the/etc/nginx directory, and the nginx configuration file nginx. conf is also in this directory.

In addition, the default nginx website directory is under/usr/share/nginx/html, and the default nginx website configuration file is the default file under the/etc/nginx/sites-available/directory.

Run the following command to start nginx:

Sudo/etc/init. d/nginx start

Sudo service nginx start

The above two commands can start nginx. I personally prefer to use the first command because it is directly started through the nginx startup script.

Netstat? Tunlp

Sudo lsof-I: 80


Visit the Web page to see the actual effect, as shown below:


Or use the curl Command to access the service, as follows:

Curl http: // 192.168.1.9


II. Install php and php-fpm

After nginx is installed, run the following command to install php and php-fpm:

Sudo apt-get-y install php5 php5-fpm php5-cli


View the php version as follows:

Php5-v


III. Integration of nginx and php-fpm

After nginx and php are installed, we are now integrating nginx with php. In fact, nginx and php are integrated through fastcgi, while fastcgi is generally used for php-fpm.

There are two communication methods for php-fpm and nginx: TCP and unix socket.

The TCP mode is to use the TCP port connection, which is generally 127.0.0.1: 9000.

Socket is connected to socket/dev/shm/php-cgi.sock using unix domain Socket (many tutorials use path/tmp, while path/dev/shm is tmpfs, which is much faster than disk ), in the case of low server pressure, the difference between tcp and socket is not big, but when the pressure is full, the socket method is used, the effect is indeed better.

3.1 TCP mode

Modify the default website file default of nginx as follows:

Sudo vi/etc/nginx/sites-available/default

Location ~ \. Php $ {

Fastcgi_split_path_info ^ (. + \. php) (/. +) $;

Fastcgi_pass 127.0.0.1: 9000;

Fastcgi_index index. php;

Include fastcgi_params ;}


Modify the fastcgi_params file of nginx and add the following command.

Note: this command must be added. Otherwise, the web page will be blank after nginx is integrated with php.

Sudo vi/etc/nginx/fastcgi_params

Fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;


Modify the default homepage of nginx as follows:

Sudo vi/usr/share/nginx/html/index. php


Now let's modify the php configuration file php. ini as follows:

Sudo vi/etc/php5/fpm/php. ini + 758


In addition, we also need to modify the configuration file www. conf of php-fpm, as follows:

Sudo vi/etc/php5/fpm/pool. d/www. conf

Listen = 127.0.0.1: 9000


After the preceding modification, restart nginx and php-fpm as follows:

Sudo/etc/init. d/nginx restart

Sudo/etc/init. d/php5-fpm restart


Access nginx as follows:

Http: // 192.168.1.9/index. php


View the system Port as follows:

Netstat-tunlp


The above is the TCP communication method between php-fpm and nginx.

3.2 socket mode

Modify the default website file default of nginx as follows:

Sudo vi/etc/nginx/sites-available/default

Location ~ \. Php $ {

Fastcgi_split_path_info ^ (. + \. php) (/. +) $;

Fastcgi_pass unix:/var/run/php5-fpm.sock;

Fastcgi_index index. php;

Include fastcgi_params ;}


Modify the configuration file www. conf of php-fpm as follows:

Sudo vi/etc/php5/fpm/pool. d/www. conf

Listen =/var/run/php5-fpm.sock


Other modifications are the same as those for TCP connections. Now let's restart nginx and php-fpm. As follows:

Sudo/etc/init. d/nginx restart

Sudo/etc/init. d/php5-fpm restart


Now let's take a look at whether php-fpm occupies the system Port. As follows:


We can see that php-fpm does not occupy the system Port at this time.

4. Install mysql

Install mysql now as follows:

Sudo apt-get? Y install mysql-server mysql-client php5-mysql




After mysql is installed, we will use php to connect to mysql. Create the ilanni. Php file with the following content:

Sudo vi/usr/share/nginx/html/ilanni. php


$ Host = '1970. 0.0.1 ';

$ Root = 'root ';

$ Pwd = '000000 ';

$ Con = mysql_connect ($ host, $ root, $ pwd );

If ($ con = false ){

Echo "connect false ";

} Else {

Echo "connect true ";

}

?>


Access mysql through php as follows:


As shown in the figure above, we can see that php has been connected to the mysql database, which also indicates that mysql has been installed successfully.


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.