# Set up LEMP (Linux, Nginx, MySQL, PHP) in Ubuntu Linux

## LEMP Stack Overview

The **LEMP stack** is a popular open-source software stack for hosting dynamic web applications. It is an acronym that outlines the primary components that make up this powerful combination:

* **Linux**: The operating system that serves as the foundation for the stack.
* **Nginx**: Pronounced "Engine-X," it acts as the web server, efficiently handling requests and responses.
* **MySQL**: The relational database management system used for storing and managing the application's data.
* **PHP**: A widely-used scripting language that enables dynamic content by processing code on the server side.

These components provide a robust platform for developing and deploying web applications.

### **Prerequisite:**&#x20;

It works on all Linux distributions.

* Operating system used: [Ubuntu 22.0](https://docs.neevcloud.com/neevcloud-products/computes/getting-started-launch-vms)4
* [Install Nginx](https://docs.neevcloud.com/neevcloud-guide/neevcloud-knowledgebase/how-to-install-nginx-on-ubuntu-22.04)
* [Install MySQL](https://docs.neevcloud.com/neevcloud-guide/neevcloud-knowledgebase/how-to-install-mysql-on-ubuntu-22.04)
* [Install PHP](https://docs.neevcloud.com/neevcloud-guide/neevcloud-knowledgebase/set-up-lemp-linux-nginx-mysql-php-in-ubuntu-linux#step-3-installing-php-and-configuring-nginx-to-use-the-php-processor)

### Update && Upgrade the server <a href="#step-1-installing-the-nginx-web-server" id="step-1-installing-the-nginx-web-server"></a>

```
sudo apt-get update -y && apt-get upgrade -y
```

### Install Nginx web server <a href="#step-1-installing-the-nginx-web-server" id="step-1-installing-the-nginx-web-server"></a>

```
sudo apt-get install nginx -y
```

Once the Nginx is installed, start the Nginx service

```
sudo systemctl start nginx
```

&#x20;Enable it to start when the system rebooted

```
sudo systemctl enable nginx
```

To verify the current status of the service, follow these steps:

```
sudo systemctl status nginx
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FAJW5GtBUUg92ieMup9za%2Fimage.png?alt=media&#x26;token=bf8be356-8954-47b6-b894-34a13379249d" alt=""><figcaption></figcaption></figure>

You can also verify the installed version of **Nginx** with the following command:

```
sudo nginx -v
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FqVAgczLW8Wn6mcngblav%2Fimage.png?alt=media&#x26;token=951d279f-8a7a-44bf-a54b-18ead47fcb7c" alt=""><figcaption></figcaption></figure>

#### Check nginx default port

To verify that **Nginx** is running on its default **port (80)**, execute the command below:

```
ss -antpl | grep nginx
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FDvjdFawaBUgP9ZiEnDS9%2Fimage.png?alt=media&#x26;token=612c3591-243f-43a6-ac3c-a3b06a145f9e" alt=""><figcaption></figcaption></figure>

* Adjusting The Firewall

To configure `ufw` to allow Nginx connections, it's important to find a balance between security and traffic requirements. If your server isn't set up with SSL, you must allow HTTP traffic. To do this, enable connections on port

```
sudo ufw allow 'Nginx HTTP'
sudo ufw status 
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2F8Bdjkabqa4H80t2YEEpa%2Fimage.png?alt=media&#x26;token=01da1311-d81c-462f-b9ff-9c1cd20a362c" alt=""><figcaption></figcaption></figure>

To verify your Nginx installation, navigate to `http://your-server-ip` your web browser. If the installation is successful, you will be greeted by the Nginx test page.

{% embed url="http\://your\_ip\_address" %}

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2Fm5dBuNmn3iYWybCgLUQF%2Fimage.png?alt=media&#x26;token=9c65b472-5d51-4960-955a-6052d26fc802" alt=""><figcaption></figcaption></figure>

### **Install MySQL**

Install MySQL by typing the following command:

```
sudo apt-get install mysql-server -y
```

To secure the installation, MySQL comes with a script that will ask whether you want to modify some insecure defaults

```
mysql_secure_installation
```

Answer `Y` for yes, or anything else to continue without enabling.

To change the root password, type `Y`. To keep the current password, type N

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FhOVKPvURozfn5Z4dO5mS%2Fimage.png?alt=media&#x26;token=1de2c5ab-154a-48bc-804d-9750e6225ca6" alt=""><figcaption></figcaption></figure>

If you agree, please follow these steps:

If you’ve enabled validation, the script will also ask you to select a level of password validation.

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FNdertGhbqmmxxzKlhnlm%2Fimage.png?alt=media&#x26;token=0f65f7db-b7c2-4a60-b3ed-3e02a13b8823" alt=""><figcaption></figcaption></figure>

Next, you’ll be asked to submit and confirm a root password:

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FYv4zuQMFnrHygrBPtHxP%2Fimage.png?alt=media&#x26;token=ad3566b3-6f4c-4fb7-85b9-ab15634d400e" alt=""><figcaption></figcaption></figure>

* Logging Into MySQL

To access your MySQL database, use the following command:

```
sudo mysql
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FGx1emS9Hqs0dpC5B2PWy%2Fimage.png?alt=media&#x26;token=e9769d31-7e69-4d2a-b0cb-02f6a1db0b35" alt=""><figcaption></figcaption></figure>

To follow a command as a user, please execute the following steps:

```
mysql -u your_user -p
```

Replace `your_username` with your actual MySQL username. Upon entering this command, you will be prompted to input your password. After providing the correct password, you'll gain access to the MySQL shell, ready for you to execute SQL commands.

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2F869bife4kHTqfKAyVk1H%2Fimage.png?alt=media&#x26;token=035e7927-aef3-44ca-a689-86eb2cd40f65" alt=""><figcaption></figcaption></figure>

### **Install PHP and Configure Nginx to use the PHP**  <a href="#step-3-installing-php-and-configuring-nginx-to-use-the-php-processor" id="step-3-installing-php-and-configuring-nginx-to-use-the-php-processor"></a>

Nginx is now installed to serve your pages and MySQL is installed to store and manage your data. However, you still don’t have anything that can generate dynamic content. This is where PHP comes into play.

```
sudo add-apt-repository universe  
sudo apt-get install php-fpm php-mysql -y
```

This is done on the server block level (server blocks are similar to Apache’s virtual hosts). To do this, create a new server block configuration file using your preferred text editor within the `/etc/nginx/sites-available/` directory. In this example, we will be using `nano/vi` and the new server block configuration file will say, so you can replace it with your information:

sudo vi /etc/nginx/sites-available/your\_domain\_name

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FaWhqNvCqEhY7N6atVJuh%2Fimage.png?alt=media&#x26;token=99a80947-b585-420c-b7e7-b038b7011760" alt=""><figcaption></figcaption></figure>

```
server {
        listen 80;
        root /var/www/html;
        index index.php index.html index.htm index.ngnix-debian.html;
        server_name <SERVER_IP>;
        
        location/ {
                try_files snippets/fastcgi-php.conf;
        }
        
        location ~ \.php$ {
                include snippents/fastcgi-oho.conf;
                fastcgi_pass unix:/var/run/php/php<PHP_VERSION_INSTALLED>-fpm.sock;
        }
        
        location ~ /\.ht {
                deny all;
        }
}
```

\<SERVER\_IP>: Serve's public IP of the server

\<PHP\_VERSION\_INSTALLED>:  Please mention the PHP version as installed on the server, this can be checked by using the command *`$ php --version`* as used in the example below.

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FfR9rklfdJ2PEyW4Iu0DO%2Fimage.png?alt=media&#x26;token=eceb0709-cc18-46a9-b3b3-ea7a744e6655" alt=""><figcaption></figcaption></figure>

After adding the content, save and exit the file. If you're using `nano/vi`, save by pressing `CTRL + X`, then press `Y` followed by `ENTER`. Next, activate your new server block by creating a symbolic link from your server block configuration file in `/etc/nginx/sites-available/` to the `/etc/nginx/sites-enabled/` directory:

```

sudo ln -s /etc/nginx/sites-available/example.conf /etc/nginx/sites-enabled/example.conf

```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FcivFuAG1jrbq9PEDqInf%2Fimage.png?alt=media&#x26;token=a1439d94-e487-415d-b9db-9c4ee0533369" alt=""><figcaption></figcaption></figure>

Test your new configuration file for syntax errors:

```
sudo nginx -t
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FhNhOxNiO5qaCWDnIcX1p%2Fimage.png?alt=media&#x26;token=449f6717-fdf0-46a2-a26b-8f86b070dea4" alt=""><figcaption></figcaption></figure>

If any errors are reported, go back and recheck your file before continuing.

When you are ready, reload Nginx to make the necessary changes:

```
sudo systemctl reload nginx  
```

This concludes the installation and configuration of your LEMP stack. However, it’s prudent to confirm that all of the components can communicate with one another.

### **Creating PHP file to test configuration**&#x20;

To do this, use your preferred text editor to create a test PHP file called `info.php` in your document root:

```
sudo vi /var/www/html/info.php
```

```
<? php
phpinfo();
?>
```

When you are finished, save and close the file.

To view this page, navigate to your server's domain name or public IP address in your web browser, and append `/info.php` to the URL.

**<http://your\\_Server> or IP\_address/info.php**

Your browser will load a web page like the following that has been generated by PHP with information about your server:

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FVf9k4cZVnHw0gUIywXDn%2Fimage.png?alt=media&#x26;token=7889c823-8954-4886-a1a3-1b9b366a3dac" alt=""><figcaption></figcaption></figure>
