Slime: Learn Ubuntu's quick build LNMP environment

Source: Internet
Author: User
Tags fpm install php

this article starting out in the slime world.

Now the company is using Ubuntu system, these days due to individual projects need to build a LNMP environment. In order to build this environment quickly, I use the apt-get way to install. For specific steps, see the following article.

PS: Under the CentOS system Nginx and PHP-FPM integration is not feasible, because under the CentOS configuration, Nginx access to the homepage is blank.

First, install Nginx

Let's install Nginx first, using the following command:

sudo apt-get-y install Nginx

650) this.width=650; "title=" clip_image001 "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" clip_image001 "src=" http://s3.51cto.com/wyfs02/M00/6F/74/ Wkiol1wddq7sxkcgaahfpyxrgqg937.jpg "width=" 523 "height=" 266 "/>

After installation of Nginx, we will see what files are installed in Nginx. Use the following command to view the following:

Dpkg-s Nginx

650) this.width=650; "title=" clip_image002 "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; margin:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" clip_image002 "src=" http://s3.51cto.com/wyfs02/M01/6F/74/ Wkiol1wddraz0gzsaarn-nl0zau055.jpg "width=" 367 "height=" 517 "/>

Through, we can see Nginx default installation location is the/etc/nginx directory, and nginx configuration file nginx.conf is also in this directory.

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

Now let's start the Nginx by using the following command:

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 this is directly through the Nginx startup script to start.

Netstat–tunlp

sudo lsof-i: 80

650) this.width=650; "title=" clip_image003 "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; margin:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" clip_image003 "src=" http://s3.51cto.com/wyfs02/M02/6F/74/ Wkiol1wddruykwvqaanm2s5kom0798.jpg "width=" 530 "height=" 345 "/>

Visit the Web page to see the actual results, as follows:

650) this.width=650; "title=" clip_image004 "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; margin:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" clip_image004 "src=" http://s3.51cto.com/wyfs02/M00/6F/74/wKioL1WdDR_ Tsvwpaahvr-ut1nq566.jpg "width=" 640 "height=" 290 "/>

or access via the Curl command, as follows:

Curl http://192.168.1.9

650) this.width=650; "title=" clip_image005 "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; margin:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" clip_image005 "src=" http://s3.51cto.com/wyfs02/M00/6F/74/ Wkiol1wddsyxllnraajk6_3jfok342.jpg "width=" 435 "height=" 419 "/>

Second, install PHP and PHP-FPM

After Nginx installation, we will now install PHP and PHP-FPM, using the following command, as follows:

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

650) this.width=650; "title=" clip_image006 "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; margin:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" clip_image006 "src=" http://s3.51cto.com/wyfs02/M01/6F/77/ Wkiom1wdc2dhbvqjaadhpm8jpde803.jpg "width=" 463 "height=" 104 "/>

View the PHP version as follows:

Php5-v

650) this.width=650; "title=" clip_image007 "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; margin:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" clip_image007 "src=" http://s3.51cto.com/wyfs02/M02/6F/77/ Wkiom1wdc2otw-l6aad993vo9kc393.jpg "width=" 560 "height=" 104 "/>

Three, nginx and PHP-FPM integration

After the installation of Nginx and PHP, we now begin to integrate Nginx with PHP. In fact, nginx and PHP integration is achieved through fastcgi, and fastcgi we generally use is php-fpm.

There are two ways of PHP-FPM and Nginx communication, one is TCP and one is UNIX socket.

TCP is used to connect using TCP ports, which is generally 127.0.0.1:9000.

Sockets are socket/dev/shm/php-cgi.sock using UNIX domain sockets (many tutorials use the path/tmp, and Path/dev/shm is a tmpfs, much faster than disk), and when the server is under a lot of pressure, TCP and socket are not very different, but when the pressure is full, the use of sockets, the effect is really better.

3.1 TCP Way

First of all, modify the default Web file of Nginx defaults, 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; }

650) this.width=650; "title=" clip_image008 "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; margin:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" clip_image008 "src=" http://s3.51cto.com/wyfs02/M01/6F/74/ Wkiol1wddtbqfkmiaafp3ybbgns114.jpg "width=" 519 "height=" 199 "/>

Now to modify the Nginx fastcgi_params file, add the following command.

Note: This command must be added, otherwise nginx and PHP integration, the page will show blank.

sudo vi/etc/nginx/fastcgi_params

Fastcgi_param script_filename $document _root$fastcgi_script_name;

650) this.width=650; "title=" clip_image009 "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; margin:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" clip_image009 "src=" http://s3.51cto.com/wyfs02/M00/6F/77/ Wkiom1wdc23t3gmjaaphbhkwcni253.jpg "width=" 499 "height=" 475 "/>

Modify the default home page for Nginx, as follows:

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

<?php phpinfo ();?>

650) this.width=650; "title=" clip_image010 "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; margin:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" clip_image010 "src=" http://s3.51cto.com/wyfs02/M00/6F/74/ Wkiol1wddugael8caac9acr30zo259.jpg "width=" 493 "height=" 94 "/>

Now let's change the PHP config file php.ini, as follows:

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

650) this.width=650; "title=" clip_image011 "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; margin:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" clip_image011 "src=" http://s3.51cto.com/wyfs02/M01/6F/74/ Wkiol1wdduss6lzzaaiigdl_c1k951.jpg "width=" 604 "height=" 247 "/>

In addition, we also need to modify the PHP-FPM configuration file www.conf, as follows:

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

Listen = 127.0.0.1:9000

650) this.width=650; "title=" clip_image012 "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; margin:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" clip_image012 "src=" http://s3.51cto.com/wyfs02/M02/6F/74/ Wkiol1wddubsalu5aafukq99azw480.jpg "width=" 496 "height=" 172 "/>

After the above modification, we will now restart Nginx and PHP-FPM, as follows:

Sudo/etc/init.d/nginx restart

SUDO/ETC/INIT.D/PHP5-FPM restart

650) this.width=650; "title=" clip_image013 "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; margin:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" clip_image013 "src=" http://s3.51cto.com/wyfs02/M01/6F/74/ Wkiol1wdduqw4ruaaaeprcjcakq078.jpg "width=" 435 "height=" 145 "/>

Now let's visit Nginx, as follows:

http://192.168.1.9/index.php

650) this.width=650; "title=" clip_image014 "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; margin:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" clip_image014 "src=" http://s3.51cto.com/wyfs02/M00/6F/77/ Wkiom1wdc33hd6eraah-tjkhmzg294.jpg "width=" 549 "height=" 384 "/>

Now look at the port of the system, as follows:

Netstat-tunlp

650) this.width=650; "title=" clip_image015 "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; margin:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" clip_image015 "src=" http://s3.51cto.com/wyfs02/M01/6F/77/ Wkiom1wdc4ctuykdaafp8rysakc389.jpg "width=" 422 "height=" 168 "/>

The above is the PHP-FPM and Nginx TCP communication mode.

3.2 Socket Way

Modify nginx Default Web site file defaults, 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; }

650) this.width=650; "title=" clip_image016 "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; margin:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" clip_image016 "src=" http://s3.51cto.com/wyfs02/M02/6F/77/ Wkiom1wdc4owqyr9aaeqdwevws0807.jpg "width=" 398 "height=" 197 "/>

Now again to modify the PHP-FPM configuration file www.conf, as follows:

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

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

650) this.width=650; "title=" clip_image017 "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; margin:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" clip_image017 "src=" http://s3.51cto.com/wyfs02/M00/6F/77/ Wkiom1wdc4bbooeeaad5jzhkqbq902.jpg "width=" 422 "height=" 134 "/>

The other modifications are the same as the TCP connection, and now we'll restart Nginx and php-fpm. As follows:

Sudo/etc/init.d/nginx restart

SUDO/ETC/INIT.D/PHP5-FPM restart

650) this.width=650; "title=" clip_image013[1] "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; margin:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" clip_image013[1] "src=" http://s3.51cto.com/wyfs02/M01/6F/77/ Wkiom1wdc4iakzbpaaeprcjcakq652.jpg "width=" 435 "height=" 145 "/>

Now let's take a look at whether the PHP-FPM occupies the system port. As follows:

650) this.width=650; "title=" clip_image018 "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; margin:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" clip_image018 "src=" http://s3.51cto.com/wyfs02/M02/6F/74/wKioL1WdDVry7V_ Daafbfxuqr1g109.jpg "width=" 472 "height=" 153 "/>

You can see that PHP-FPM does not occupy the system port at this time.

Iv. installation of MySQL

Now to start installing MySQL, as follows:

sudo apt-get–y install mysql-server mysql-client php5-mysql

650) this.width=650; "title=" clip_image019 "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; margin:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" clip_image019 "src=" http://s3.51cto.com/wyfs02/M02/6F/77/wKiom1WdC4_ Zhe3waagutv4inme069.jpg "width=" 622 "height=" 149 "/>

650) this.width=650; "title=" clip_image020 "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; margin:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" clip_image020 "src=" http://s3.51cto.com/wyfs02/M02/6F/77/ Wkiom1wdc5kgph4uaagap9sk1qu359.jpg "width=" 838 "height=" 197 "/>

650) this.width=650; "title=" clip_image021 "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; margin:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" clip_image021 "src=" http://s3.51cto.com/wyfs02/M00/6F/74/ Wkiol1wddwtjqfv6aadhnnnh2va023.jpg "width=" 379 "height=" 174 "/>

After the MySQL installation is complete, we will now use PHP to connect to MySQL. Create a ilanni.php file with the following content:

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

<?php

$host = ' 127.0.0.1 ';

$root = ' root ';

$pwd = ' 123456 ';

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

if ($con = = False) {

echo "Connect false";

} else{

echo "Connect true";

}

?>

650) this.width=650; "title=" clip_image022 "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; margin:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" clip_image022 "src=" http://s3.51cto.com/wyfs02/M01/6F/74/ Wkiol1wddwet55dvaaedtey7kny332.jpg "width=" 559 "height=" 247 "/>

Now to access MySQL via PHP, as follows:

650) this.width=650; "title=" clip_image023 "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" clip_image023 "src=" http://s3.51cto.com/wyfs02/M01/6F/77/ Wkiom1wdc5rwp-0raadbqhmz3mw476.jpg "width=" 388 "height=" 143 "/>

Through, we can see that PHP has been connected to MySQL database, which also shows that MySQL has been installed successfully.

Slime: Learn Ubuntu's quick build LNMP environment

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.