Install LEMP in Ubuntu Server

You are currently viewing Install LEMP in Ubuntu Server

To install a LEMP (Linux, Nginx, MySQL, PHP) stack on your VPS (Virtual Private Server), you can follow these general steps. Keep in mind that the specific commands might vary slightly depending on the Linux distribution you’re using.

Step 1: Update Package List

Update your package list to make sure you have the latest information about available packages:

sudo apt update

Step 2: Install Nginx

Install Nginx, a web server:

sudo apt install nginx

Step 3: Start Nginx

Start the Nginx service and enable it to start on boot:

sudo systemctl start nginx
sudo systemctl enable nginx

Step 4: Install MySQL (MariaDB)

Install the MySQL (or MariaDB) database server:

sudo apt install mariadb-server

Secure your MySQL installation:

sudo mysql_secure_installation

Follow the on-screen prompts to set a root password and answer security-related questions.

Step 5: Install PHP

Install PHP and the required modules:

sudo apt install php-fpm php-mysql

Step 6: Configure Nginx to Use PHP

Edit the default Nginx server block configuration file:

sudo nano /etc/nginx/sites-available/default

Find the location block that starts with location ~ \.php$ { and uncomment and modify the lines to look like this:

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;

Save and close the file.

Step 7: Restart Nginx and PHP-FPM

Restart Nginx and PHP-FPM to apply the changes:

sudo systemctl restart nginx
sudo systemctl restart php7.4-fpm

Step 8: Test the Installation

Create a simple PHP info file to test the setup:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Visit your server’s IP address in a web browser with /info.php appended (e.g., http://your_server_ip/info.php). You should see the PHP information page.

Step 9: (Optional) Configure Firewall

If you’re using a firewall, you may need to open the necessary ports. For example, for UFW:

sudo ufw allow 'Nginx Full'
sudo ufw allow 'OpenSSH'
sudo ufw enable

These steps provide a basic LEMP stack setup. Depending on your specific requirements, you may need to customize configurations further. Always refer to the official documentation for each component for more detailed information.

Leave a Reply