How to Install Nginx, MySQL & PHP (LEMP) on Ubuntu 20.04
https://tecadmin.net/install-lemp-stack-on-ubuntu-20-04/
CHú ý nếu gặp lỗi không cài được mysql thì đọc bài này :)
How to Install Nginx, MySQL & PHP (LEMP) on Ubuntu 20.04
Written by Rahul, Updated on June 30, 2020
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:
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.
Also install additional PHP modules required for your application.
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:
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.
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.
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.
Use the below basic Nginx Virtual host configuration with php fpm settings. Update the configuration as followings.
123456789101112131415 | 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.
Then restart Nginx service to reload the changes.
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:
To start Nginx and MySQL services, type:
To stop Nginx and MySQL services, type:
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.
Step 7 – Verify the Setup
After completing all setup. Let’s create a info.php file website document root with following content.
Now access this file in web browser. You will see the screen like below with all details of PHP on server.
Congratulation’s! You have successfully configured LEMP server on your Ubuntu 20.04 LTS system.
Last updated