Install LEMP on Ubuntu 18 - LEMP stands for Linux, Nginx (Engine-X), MariaDB / MySQL and PHP to create a Web server environment to help deploy websites on the Internet.
In this article HOSTVN will guide you to install LEMP on Ubuntu 18.
Instructions for installing LEMP on Ubuntu 18
1. Request
To follow this guide, you need to have a VPS using the OS Ubuntu 18.04. If you do not have a VPS, immediately refer to the packages Cloud VPS of the HOSTVN.
2. Install LEMP on Ubuntu 18
2.1. Step 1: Install Nginx
First you need to install some necessary libraries by running the following command
sudo apt install curl gnupg2 shift-certificates lsb-release -y |
In this tutorial HOSTVN will guide you to install Nginx mainline latest version at the time of writing v17.10. To install Nginx You need to create source.list with the following command:
echo "deb http://nginx.org/packages/mainline/ubuntu` lsb_release -cs` nginx " | sudo tee /etc/apt/sources.list.d/nginx.list |
Next, enter nginx signing key so apt can verify the authenticity of the packages:
curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo apt-key add - |
Then proceed to key verification
sudo apt-key fingerprint ABF5BD827BD9BF62 |
To install Nginx, run the following commands:
sudo apt update sudo apt install nginx -y |
To check the Nginx version, use the following command
Run Nginx with the following two commands
sudo systemctl start nginx sudo systemctl enable nginx |
To check if Nginx is working, use the command
2.2. Step 2: Install MariaDB
To install MariaDB 10.4 on Ubuntu 18.04, you will need to add MariaDB repository to the system. First you need to install software-properties-common with the following command:
sudo apt-get install software-properties-common -y |
Then you run the following command to add the Repository Key to the system
sudo apt-key adv -recv-keys -keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8 |
Next add repository with the following command
sudo add-apt-repository "deb (arch = amd64, arm64, ppc64el) http://mariadb.mirror.liquidtelecom.com/repo/10.4/ubuntu $ (lsb_release -cs) main" |
To install Mariadb, run the following commands:
sudo apt update sudo apt -y install mariadb-server mariadb-client |
Next you proceed to place root password for Mariadb by running the following commands one after the other
sudo mysql -u root use mysql; ALTER USER 'root'@'localhost' IDENTIFIED BY 'Password-You-Want-Set'; flush privileges; exit |
Instead Password-You-Want-Set with your password
After setting the root password, run the following commands to start Mariadb
sudo systemctl start mariadb sudo systemctl enable mariadb |
Finally proceed to security MariaDB with the following command
mysql_secure_installation |
Enter current password for root (enter for none): Enter the root password you have set
Switch to unix_socket authentication (Y / n): n
Change the root password? (Y / n): n
Remove anonymous users? (Y / n): Y
Disallow root login hack? (Y / n): Y
Remove test database and access to it? (Y / n): Y
Reload privilege tables now? (Y / n): Y
To check if MariaDB is running, use the command
2.3. Step 3: Install PHP-FPM
a. Install PHP-FPM
To install PHP you should use ppa: ondrej / php PPA to be able to install the latest PHP-FPM version
sudo add-apt-repository ppa:ondrej/php |
Next you install PHP with the following command. In this article HOSTVN will install PHP 7.4
sudo apt update sudo apt -y install php7.4 php7.4-fpm php7.4-ldap php7.4-zip php7.4-cli php7.4-mysql php7.4-gd php7.4-xml php7.4-mbstring php7.4-common php7.4-soap php7.4-json php7.4-curl php7.4-bcmath php7.4-snmp php7.4-pspell php7.4-gmp php7.4-intl php7.4-imap php7.4-enchant php7.4-xmlrpc php7.4-tidy php7.4-opcache php7.4-cli unzip gcc |
b. Configure PHP-FPM
First, back up the default configuration file
sudo cp /etc/php/7.4/fpm/pool.d/www.conf /etc/php/7.4/fpm/pool.d/www.conf.bak |
Next you open the file /etc/php/7.4/fpm/pool.d/www.conf
user = www-data group = www-data |
Edited into
user = nginx group = nginx |
listen.owner = www-data listen.group = www-data |
Edited into
listen.owner = nginx listen.group = nginx |
Edited into
;security.limit_extensions = .php .php3 .php4 .php5 .php7 |
Edited into
security.limit_extensions = .php .php3 .php4 .php5 .php7 |
c. Configure php.ini
All necessary php parameter configuration will be in the file /etc/php/7.4/fpm/php.ini. Before editing, you should backup the file php.ini with the following command
sudo cp /etc/php/7.4/fpm/php.ini /etc/php/7.4/fpm/php.ini.bak |
Some basic parameters you can edit as follows
;date.timezone = expose_php = On short_open_tag = Off ;max_input_vars = 1000 max_execution_time max_input_time post_max_size upload_max_filesize |
You revise it as follows:
date.timezone = Asia/Ho_Chi_Minh expose_php = Off short_open_tag = On max_input_vars = 3000 max_execution_time = 90 max_input_time = 90 post_max_size = 128M upload_max_filesize = 128M |
d. Start PHP-FPM
After the installation process has completed, run the following two commands to start PHP-FPM
sudo systemctl start php7.4-fpm sudo systemctl enable php7.4-fpm |
To check if php-fpm is already running, use the following command
systemctl status php7.4-fpm |
When you use PHP-FPM. All PHP configuration files are located in the directory /etc/php/7.4/fpm
2.4. Step 4: Install PhpMyAdmin
a. Install PhpMyAdmin
To install PhpMyAdmin you run the following commands:
CD /usr/share sudo wget https://files.phpmyadmin.net/phpMyAdmin/5.0.2/phpMyAdmin-5.0.2-all-languages.zip sudo unzip phpMyAdmin-5.0.2-all-languages.zip sudo mv phpMyAdmin-5.0.2-all-languages phpMyAdmin sudo rm -rf phpMyAdmin-5.0.2-all-languages.zip sudo rm -rf /usr/share/phpMyAdmin/setup sudo ln -S /usr/share/phpMyAdmin /usr/share/nginx/html |
- Note: Access phpmyadmin.net To get the link down to the latest version
b. Configure PhpMyAdmin
Next you proceed to configure PhpMyadmin:
sudo mv /usr/share/phpMyAdmin/config.sample.inc.php /usr/share/phpMyAdmin/config.inc.php |
Open the file /usr/share/phpMyAdmin/config.inc.php and edit the following parameters
- Find
$cfg('blowfish_secret') = ''; |
Add any paragraph of characters between the single quotes. For example:
$cfg('blowfish_secret') = 'dsa123e12rwDSADs1few12tr3ewg3s2df3sAD'; |
- Next add the following code
$cfg('TempDir') = '/ usr / share / phpMyAdmin / tmp /'; |
Then you need to create folders tmp for PhpMyAdmin
sudo mkdir -p /usr/share/phpMyAdmin/tmp sudo chown -R nginx:nginx /usr/share/phpMyAdmin/tmp |
c. Security configuration for PhpMyAdmin
You open the file /etc/nginx/conf.d/default.conf delete all content and replace with the following:
first 2 3 4 5 6 7 8 9 ten 11 twelfth 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 thirty first 32 33 34 35 36 37 38 39 40 41 | server { listen 80; server_name localhost; access_log /var/log/nginx/localhost.log; error_log /var/log/nginx/localhost.error.log; root /usr/share/nginx/html; index index.html index.htm index.php; location ~ .php$ { try_files $uri $uri/ =404; fastcgi_split_path_info ^(.+.php)(/.+)$; fastcgi_pass unix:/run/php/php7.4-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; fastcgi_intercept_errors on; if (-f $request_filename) { fastcgi_pass unix:/run/php/php7.4-fpm.sock; } } location ~ ^/phpmyadmin { rewrite ^/* /phpMyAdmin last; } location ~ ^/pma { rewrite ^/* /phpMyAdmin last; } location ^~ /phpMyAdmin/log/ { deny all; } location ^~ /phpMyAdmin/libraries/ { deny all; } location ^~ /phpMyAdmin/templates/ { deny all; } location ^~ /phpMyAdmin/tmp/ { deny all; } } |
Next you run the following command:
sudo mv /etc/nginx/conf.d/default.conf /etc/nginx/default.conf |
Open the file /etc/nginx/nginx.conf and add above the include line /etc/nginx/conf.d/*.conf; the following rule
include /etc/nginx/default.conf; |
Restart Nginx to reload the configuration
sudo service nginx restart |
Then you can access: address-vps / pma and login with user as root and password is MariaDB root password
2.5. Step 5: Create a Virtual host
Virtual Host A configuration file that allows multiple domains to run on the same server. All vhost files will be in the directory /etc/nginx/conf.d/. For convenience, each website should have a separate vhost, for example: hostvn.net.conf
In this example, the website will be created hostvn.net with the corresponding vhost is /etc/nginx/conf.d/hostvn.net.conf with the following content:
first 2 3 4 5 6 7 8 9 ten 11 twelfth 13 14 15 16 17 18 19 20 21 | server { listen 80; server_name www.hostvn.net hostvn.net; access_log /home/hostvn.net/logs/access.log; error_log /home/hostvn.net/logs/error.log; root /home/hostvn.net/public_html; index index.html index.htm index.php; location ~ .php$ { try_files $uri $uri/ =404; fastcgi_split_path_info ^(.+.php)(/.+)$; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; fastcgi_intercept_errors on; if (-f $request_filename) { fastcgi_pass unix:/run/php/php7.4-fpm.sock; } } } |
Next, you need to create the directory containing the website source code and the directory containing the log file with the following commands
sudo mkdir -p /home/hostvn.net/public_html sudo mkdir -p /home/hostvn.net/logs sudo chown -R nginx:nginx /home/hostvn.net |
Restart Nginx to load the configuration
sudo systemctl restart nginx |
After configuration is complete you point the domain to vps and then create the file /home/hostvn.net/public_html/index.php With the following content and type your domain name in the address bar of the browser to check
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>HOSTVN.NET - Huuuung duhn cahi putt LEMP trHeyn Ubuntu 18</title> </head> <body> <p><center> "HOSTVN.NET - Instructions for installing LEMP on Ubuntu 18" ?></center></p> </body> </html> |
3. Tài liệu tham khảo
4. Kết luận
Qua bài viết này HOSTVN đã hướng dẫn các bạn cách cài đặt LEMP trên Ubuntu 18. Nếu có bất kỳ ý kiến đóng góp nào các bạn có thể để lại bình luận ở bên dưới. Ngoài ra các bạn có thể xem thêm Hướng dẫn cách cài đặt LEMP trên Centos 7.
0 Comments