This article is based on how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-20-04 learning, you can refer to the original text.
The LEMP software stack is a set of software used to provide services for dynamic web pages and web applications written in PHP. This is an acronym used to describe the Linux operating system and Nginx (pronounced "Engine-X") web server. The back-end data is stored in a MySQL database, and the dynamic processing is handled by PHP. This article describes how to install LEMP stack on Ubuntu 20.04 server.
Prerequisites:
Have a non-root user with sudo privileges. If not, create one under root
adduser sammy
usermod -aG sudo sammy
This creates a user with sudo privileges
Install Nginx
Execute the following command to install:
sudo apt update
sudo apt install nginx
View firewall
sudo ufw app list
can be seen
Available applications:
Nginx Full
Nginx HTTP
Nginx HTTPS
OpenSSH
Enable firewall
sudo ufw allow 'Nginx HTTP'
Check status:
sudo ufw status
can be seen:
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx HTTP ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Nginx HTTP (v6) ALLOW Anywhere (v6)
View the local ip address
ip addr show eth0 | grep inet | awk'{ print $2; }'| sed's/\/.*$//'
can also be like this:
curl -4 icanhazip.com
Get the ip address, mainly to verify whether it is installed.
Enter the ip address in the browser, you should see:
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
This means that Nginx is installed
Install MySQL
Execute the following command:
sudo apt install mysql-server
Perform database security procedures
sudo mysql_secure_installation
If you answer Y, the installation is started, then you will be prompted for the security level,
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1
In fact, the restriction password is a bit more complicated.
test:
sudo mysql
enters mysql, because sudo does not require a password. exit Exit.
Install PHP
Execute the following command:
sudo apt install php-fpm php-mysql
Configure Nginx to use PHP
On Ubuntu 20.04, Nginx has a server block enabled by default and configured to serve documents in the /var/www/html directory. Although this is very effective for a single site, it can become difficult to manage if you host multiple sites. We will create a directory structure for your_domain website within /var/www instead of modifying /var/www/html. If the customer request is not received, we will leave /var/www/html as the default directory to match any other website.
The original text explains that multiple sites are configured. I will make it simpler here and use the default one. To see the configuration of multiple sites, you can refer to the original text, or you can see the introduction to nginx configuration . The so-called simplicity is that no domain name is used.
Go to the working directory /etc/nginx/sites-available to edit the default under it
sudo nano default
I found the following paragraph. It was all commented at the beginning. I have uncommented 4 lines here. The last line is:}
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
There is a file here is /var/run/php/php7.4-fpm.sock, I found this directory, there is this file, the file name in the previous tutorial is different, it may be 7.2, inconsistent with my system, the result will be Report an error.
Save the file, then execute
sudo nginx -t
check for errors. Reload nginx without error
sudo systemctl reload nginx
The configuration is changed.
test
Create a file index.html in the /var/www/html directory,
sudo nano index.html, the content is as follows:
<html>
<head>
<title>your_domain website</title>
</head>
<body>
<h1>Hello World!</h1>
<p>This is the landing page of <strong>your_domain</strong>.</p>
</body>
</html>
Enter the URL in the browser to see the page
Hello World!
This is the landing page of your_domain.
Then create an info.php under it
sudo nano /var/www/html/info.php
The content is as follows:
<?php
phpinfo();
Enter the ip address/php.info in the browser
Mine is http://138.197.144.170/info.php
A lot of php information pages will be displayed.
Test database connection from php (optional)
I won’t introduce it here, you can refer to the original text if necessary, or
That's it for the introduction.