A combination of Linux, Nginx, MySQL, and PHP is known as LEMP stack is the popular web hosting environment for the PHP based application. Here Linux is an operating system, Nginx is the popular web server, MySQL is relational database management system used for storing data and PHP is the widely used programming language.
This article will help you to install Nginx, MySQL 8.0 and PHP 7.4 on Ubuntu Linux system. Let’s begin with the installation of LEMP stack your Ubuntu machine.
Prerequisites
Before beginning the LEMP installation on Ubuntu:
- A running Ubuntu 20.04 system
- Login as sudo proviledged account on your system. To create it follow initial server setup tutorial.
- A domain/subdomain name pointed to your server
Installing Nginx Web Server
Next, you need to install Nginx web server on your system. The Nginx packages are are available under the default apt repositories.
Run the following commands to install it:
sudo apt update sudo apt install nginx
Installing PHP with PHP-FPM
PHP 7.4 packages are available under the default repositories on Ubuntu 20.04 LTS. Use the following command to update apt cache and install PHP on your system.
sudo apt update sudo apt install -y php7.4 php7.4-fpm
Also install additional PHP modules required for your application.
sudo apt install php7.4-curl php7.4-gd php7.4-json php7.4-mbstring php7.4-xml
You have installed PHP 7.4 with PHP FPM package on your system. Let’s check the status of the PHP FPM service by running below command:
sudo systemctl status php7.4-fpm ● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2020-06-16 05:15:57 UTC; 34s ago Docs: man:php-fpm7.4(8) Process: 882716 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/7.4/fpm/pool.d/www.conf 74> Main PID: 882699 (php-fpm7.4) Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec" Tasks: 3 (limit: 2283) Memory: 10.3M CGroup: /system.slice/php7.4-fpm.service ├─882699 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf) ├─882714 php-fpm: pool www └─882715 php-fpm: pool www Jun 16 05:15:57 tecadmin systemd[1]: Starting The PHP 7.4 FastCGI Process Manager... Jun 16 05:15:57 tecadmin systemd[1]: Started The PHP 7.4 FastCGI Process Manager.
Installing MySQL
The default Ubuntu 20.04 apt repositories contains MySQL server 8.0. Finally, install mysql-server packages for the MySQL database. Also, install the php-mysql package to use MySQL support using PHP. Use the following command to install it.
sudo apt install mysql-server php7.4-mysql
The installer will prompt for the root password, This password will work for your MySQL root user. After installing MySQL execute the following command for initial settings of MySQL server. You will see that script will prompt for more settings than earlier MySQL versions like password validation policy etc.
sudo mysql_secure_installation
Securing the MySQL server deployment. Connecting to MySQL using a blank password. VALIDATE PASSWORD COMPONENT can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD component? Press y|Y for Yes, any other key for No: yThere 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: 2Please set the password for root here. New password: Re-enter new password:Estimated strength of the password: 100 Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? (Press y|Y for Yes, any other key for No) : ySuccess. Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : ySuccess. By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y- Dropping test database... Success. - Removing privileges on test database... Success. Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : ySuccess. All done!
Configure Nginx with PHP-FPM
Let’s create a Nginx virtual host to run with FPM/FastCGI. For this tutorial, we use default VirtualHost. Edit VirtualHost host configuration file in text editor. You can create new VirtualHost as per your requirements, so make sure to enable any new VirtualHost.
sudo vim /etc/nginx/sites-available/example.com
Use the below basic Nginx Virtual host configuration with php fpm settings. Update the configuration as followings.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | server { listen 80; root /var/www/html; index index.php index.html index.htm; server_name example.com; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; } } |
Save your changes to the configuration file and create a link to site enabled directory.
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com
Then restart Nginx service to reload the changes.
sudo systemctl restart nginx
Step 5 – Manage Services
We have done with the installation of LAMP stack on Ubuntu 20.04 LTS system. The below commands will help you to start/stop or restart Nginx and MySQL services running with systemd.
To restart Nginx and MySQL services, type:
sudo systemctl restart nginx sudo systemctl restart mysql
To start Nginx and MySQL services, type:
sudo systemctl start nginx sudo systemctl start mysql
To stop Nginx and MySQL services, type:
sudo systemctl stop nginx sudo systemctl stop mysql
Step 6 – Adjust Firewall Rules
You can directly provide a service name like “http” or “https” to allow. The firewalld uses /etc/services file to determine the corresponding port of the service.
sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload
Step 7 – Verify the Setup
After completing all setup. Let’s create a info.php file website document root with following content.
sudo echo "<?php phpinfo(); ?>" > /var/www/html/info.php
Now access this file in web browser. You will see the screen like below with all details of PHP on server.