How To Install Nginx, PHP and MySQL on Ubuntu Linux (LEMP)

Install LEMP stack on Ubuntu

Step 1: Update the Server’s Package Index

To install LEMP stack on Ubuntu, first you need to update your server’s package index. This can be done by executing the following command:

sudo apt update

Step 2: Install Nginx

Next, you can install Nginx, a high-performance web server, using the following command:

sudo apt install nginx

After the installation, Nginx should be active and running on your Ubuntu server.

Step 3: Allow Nginx Through the Firewall

If you have the UFW firewall enabled, you need to allow connections to Nginx, You can do this by executing the following command:

sudo ufw allow 'Nginx HTTP'

for https:

sudo ufw allow 'Nginx HTTPS'

You can verify the change by checking the status:

sudo ufw status

The output of this command should show that HTTP (or HTTPS) traffic is allowed

Step 4: Install MySQL

Now, let’s install MySQL for managing backend data. You can do this by executing the following command:

sudo apt install mysql-server

Step 5: Install PHP

Finally, you need to install PHP for handling the dynamic processing. You can do this by executing the following command:

sudo apt install php-fpm php-mysql

Step 6: Testing Your LEMP Stack

After you’ve installed all the components of the LEMP stack, it’s a good idea to test everything to ensure they are working correctly.

Test Nginx

You can test Nginx by navigatin to your server’s IP address in your web brower. If Nginx is working correctly, you should see a welcome page:

http://your_server_ip

Test PHP Processing

To test PHP, you can create a simple PHP script in default document root. Create a new file named info.php:

sudo nano /var/www/html/info.php

In this file, add the following lines:

<?php
phpinfo();
?>

Save and close the file. Now, if you visit

http://your_server_ip/info.php

in you web browser, you should see a page displaying information about your server’s PHP configuration

Test MySQL

You can check the status of the MySQL service using the following commands:

sudo systemctl status mysql

If MySQL is running correctly, you should see an output indicating that the service is active.

That’s it! You have successfuly installed a LEMP stack on your Ubuntu server. You can now start building your web applications.

Please note that these instructions are for Ubuntu 20.04. If you’re using a different version of Ubuntu, the commands might vary slightly. Always refer to the official documentation for the most accurate information.

Resource:

Leave a Reply