At Monovm we provide you with the underlying facts, assumptions and rules which a computer system has.

18

Dec, 18

How to install LEMP on Ubuntu18.04

  • by linda Y
  • in Linux
  • View 135

 

LEMP is an acronym for Linux, Nginx, MySQL, and PHP. LEMP stack just like LAMP stack (with Apache instead of Nginx) is used for development and deployment of web applications. Nginx in LEMP provides a modular event-driven architecture handling requests using asynchronous events. This feature enables it to have high performance under high loads. The MySQL is used to store the website’s data whereas PHP is for processing the dynamic content of the sites.

Prerequisites

  • A VPS account 
  • Username and password for your VPS account

 

Step1: Connect to your server via SSH

All VPS plans allow SSH access. You should therefore connect to your Ubuntu server with your public IP address that was allocated to you. If you are running Windows as your local computer, consider downloading PuTTY SSH client. You can also initiate an SSH connection from Mac or Linux using the inbuilt SSH client Terminal.

 

Step2: Install Nginx server

First update your software packages and then install Nginx.

$ sudo apt update$ sudo apt install nginx

Once you have installed it, the Nginx service should start automatically and will be enabled to start at boot time, you can check if it’s up and running.

$ sudo systemctl status nginx

If you have a firewall enabled and running on your system, you should open the ports 80 (HTTP) and 443(HTTPS) to allow client requests to Nginx web server, and reload the firewall rules.

$ sudo ufw allow 80/tcp$ sudo ufw allow 443/tcp$ sudo ufw reload

Next, test if the Nginx package was successfully installed and is working fine, type your server's IP address in your web browser.


If you see the Nginx default web page, it means your installation is working fine.

 

 

 

Step 3: Installing MySQL to Manage Site Data

Now that you have a web server, you need to install MySQL (a database management system) to store and manage the data for your site. Below we have two commands separated by &&. The first command will update the package lists to ensure you get the latest version and dependencies for MySQL. The second command will then download and install MySQL.

$ sudo apt update && sudo apt install mysql-server

Press “y” and “Enter” when prompted to install the MySQL package.

Once the package installer has finished, we can check to see if the MySQL service is running.

$ sudo service mysql status

If running, you will see a green Active status like below.

mysql.service - MySQL Community ServerLoaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled (Active: active (running) since Wed 2018-05-09 21:10:24 UTC; 16s agoMain PID: 30545 (mysqld)Tasks: 27 (limit: 1153)CGroup: /system.slice/mysql.service30545 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid 

You may need to press “q” to exit the service status.

 

Step4: Configure MySQL Security

You should now run mysql_secure_installation to configure security for your MySQL server.

$ sudo mysql_secure_installation

If you have a root password, you may be prompted to enter it here. Otherwise you will be asked to create one.

You will be asked if you want to set up the Validate Password Plugin. It’s not really necessary unless you want to enforce strict password policies for some reason.

Securing the MySQL server deploymentConnecting to MySQL using a blank passwordVALIDATE PASSWORD PLUGIN can be used to test passwordsand improve security. It checks the strength of passwordand allows the users to set only those passwords which aresecure enough. Would you like to setup VALIDATE PASSWORD plugin?Press y|Y for Yes, any other key for No:

Press “n” and “Enter” here if you don’t want to set up the validate password plugin.

Please set the password for root hereNew password:Re-enter new password:

If you didn’t have a root password, now you must create one here.

Generate a strong password and enter it. Note that when you enter passwords in Linux, nothing will show as you are typing (no stars or dots).

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 fortesting, and to make the installation go a bit smoother.You should remove them before moving into a production environmentRemove anonymous users? (Press y|Y for Yes, any other key for No):

Press “y” and “Enter” to remove anonymous users.

Normally, root should only be allowed to connect from'localhost'. This ensures that someone cannot guess atthe root password from the network.Disallow root login remotely? (Press y|Y for Yes, any other key for No):

Press “y” and “Enter” to disallow root login remotely. This will prevent bots and hackers from trying to guess the root password.

By default, MySQL comes with a database named 'test' thatanyone can access. This is also intended only for testing,and should be removed before moving into a productionenvironment.Remove test database and access to it? (Press y|Y for Yes, any other key for No):

Press “y” and “Enter” to remove the test database.

Reloading the privilege tables will ensure that all changesmade so far will take effect immediately.Reload privilege tables now? (Press y|Y for Yes, any other key for No):

Press “y” and “Enter” to reload the privilege tables.

All done!

As a test, you can log into the MySQL server and run the version command.

$ sudo mysqladmin –p –u root version

Enter the MySQL root password you created earlier and you should see the following:

$ sudo mysqladmin –p –u root versionmysqladmin  Ver 8.42 Distrib 5.7.22, for Linux on x86_64Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Server version          5.7.22-0ubuntu18.04.1Protocol version        10Connection              Localhost via UNIX socketUNIX socket             /var/run/mysqld/mysqld.sockUptime:                 4 min 28 secThreads: 1  Questions: 15  Slow queries: 0  Opens: 113  Flush tables: 1  Open tables: 106  Queries per second avg: 0.055

You have now successfully installed and configured MySQL for Ubuntu 18.04! Continue to the next step to install PHP.

Step 5: Install PHP on Ubuntu 18.04

PHP is a popular server side scripting language used to generate dynamic content on websites. You can install PHPPHP-FPM and other modules for web development using following command (the default version in the Ubuntu repos is PHP 7.2).

$ sudo apt install php php-fpm php-common php-mysql php-gd php-cli

After PHP installation, the PHP7.2-FPM service should also start automatically, you can verify the service using the following command.

$ sudo systemctl status php7.2-fpm

Next, configure PHP-FPM properly to serve PHP based web applications or sites, in the configuration file /etc/php/7.2/fpm/php.ini.

$ sudo vim /etc/php/7.2/fpm/php.ini

Search for the cgi.fix_pathinfo=1 and change it to the following.

cgi.fix_pathinfo=0

Then configure PHP-FPM to process PHP scripts in Nginx default server block configuration file (/etc/nginx/sites-available/default).

$ sudo vim /etc/nginx/sites-available/default

Uncomment the configuration section below to pass PHP scripts to FastCGI server.

location ~ \.php$ {            include snippets/fastcgi-php.conf;               fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;}

After making changes, restart the php7.2-fpm and nginx services to apply the recent changes.

$ sudo systemctl restart php7.2-fpm$ sudo systemctl restart nginx

Now you can test your PHP setup by creating a simple info.php page in your web server document root, with this single command.

$ echo "" | sudo tee /var/www/html/info.php

Next, open a web browser, and enter any of the following URL to view the php information page.

http://domain_name/info.phpORhttp://SERVER_IP/info.php

 

 

 

linda Y

My name is Linda, I have Master degree in Information Technology Engineering. I have some experiences in working with Windows and Linux VPS and I have been working for 2 years on Virtualization and Hosting.