Configure LNMP environment

Time:2021-10-20

The virtual machine environment is

  • Oracle VM VirtualBox

  • ubuntu-16.04.2-desktop-amd64.iso

To ensure that you update to the latest package, usesudo apt-get updateCommand update source list

Basic dependency

Autoconf: a tool for generating configuration scripts (for example, the. / configure file needs to be generated)

sudo apt-get install autoconf

phpizeUsers can easily add various extensions

Zlib: compressed function call library

sudo apt-get install zlib1g-dev

nginx

Installing nginx

sudo apt-get install nginx

View downloaded directory

dpkg -S nginx

Nginx default installation address:/etc/nginx

Configure LNMP environment

apt-get installFile directory after:

Bin file path: / usr / bin 
Library file path: / usr / Lib/  
Other paths: / usr / share 
Configuration file path: / etc/

Default location of installation package:/var/cache/apt/archives

View the directory where the command exists

which make

Configure LNMP environment

start nginx

sudo service nginx start
sudo /etc/init.d/nginx start

Restart nginx

sudo service nginx reload

To check whether port 80 has been in listen status, you can use:sudo lsof -i :80
Then enter in the browser:127.0.0.1, the default welcome interface of nginx appears, and nginx starts successfully

php

Php7. X is installed
The version of php7. X is not in the Ubuntu software library, so PPA should be usedppa:ondrej/phplibrary

sudo apt-get repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install php7.1 php7.1-fpm

You can view the PHP version

php -v

Nginx and PHP integration

Modify the nginx configuration file to make nginx support PHP

vi /etc/nginx/sites-available/default

Nginx and fastcgi can communicate in two ways: TCP and UNIX socket
The default is socket mode

  • Modify the nginx home directory. The default is/var/www/htmlmodify/var/www

Configure LNMP environment

  • Nginx supports PHP configuration

Configure LNMP environment

After modifying the nginx configuration, reload the nginx configuration filesudo service nginx reload

Modify php7 FPM configuration file

Because the communication mode between nginx and PHP in the nginx configuration file istcp, so it also needs to be modifiedphp7-fpmConfiguration file for

vi /etc/php/7.1/fpm/pool.d/www.conf

Put the defaultSocket modechange intoTCP mode

Configure LNMP environment

listen =127.0.0.1:9000Indicates that php7-fpm listens for connection requests on port 9000, which is the default port.

Next startphp7-fpm

sudo service php7.1-fpm start
sudo /etc/init.d/php7.1-fpm start

Verify that nginx supports PHP

stay/var/www/Create a file in the directorytest.php

<?php
    echo phpinfo();
?>    

Output PHP related information in the browser, indicating that nginx can run PHP successfully.

error

If the default file of PHP is not modified, or the modification is not successful, a502 bad gateway error

mysql

Install MySQL

sudo apt-get install mysql-server mysql-client

The output root password appears during installation.

You can use commandsnetstat -anpCheck it out3306Is the port listening

Test whether PHP can connect to MySQL database.
stay/var/wwwCreate a file in the directorydb.php

<?php

//Connect to MySQL
$con = mysql_connect('127.0.0.1', 'root', '');

if (!$con) {
    echo 'not connect' . mysql_error();
    die();
}

//Create a test database db
if (mysql_query('create database db', $con)) {
    echo 'database created';
} else {
    echo 'database created error' . mysql_error();
}

//Close data connection
mysql_close($con);

At this point, run in the browserdb.phpIf no output is found, check the console information,Server returns 500

Configure LNMP environment

The reason is that the MySQL extension of PHP is not installed

Search the MySQL extension installation package

sudo apt-chche search php7.1-mysql

Configure LNMP environment

Then install the MySQL extension

sudo apt-get install php7.1-mysql

After installation, restartphp7-fpm

sudo /etc/init.d/php7.1-fpm restart

Configure LNMP environment

Run in browserdb.phpfile
Log in to the database on the command line to see whether the creation is successful.