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 Mode
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
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 Mode
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
$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.
Original: https://yq.aliyun.com/ziliao/29524
Fast setup of LNMP Environment "graphic Tutorial" under Ubuntu