Slime: Learn Ubuntu's quick build LNMP environment

Source: Internet
Author: User
Tags fpm install php

This article by show according to Lin Feng to provide friendship sponsorship, starting in the mud row of the 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

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

Dpkg-s Nginx

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

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

or access via the Curl command, as follows:

Curl http://192.168.1.9

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

View the PHP version as follows:

Php5-v

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; }

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;

Modify the default home page for Nginx, as follows:

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

<?php phpinfo ();?>

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

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

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

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

Now let's visit Nginx, as follows:

http://192.168.1.9/index.php

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

Netstat-tunlp

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; }

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

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

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

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

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";

}

?>

Now to access MySQL via PHP, as follows:

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.