How to Install Linux, Nginx, MySQL, PHP (LEMP) on Ubuntu
1. Prerequisites
Before you begin, ensure you have the following:
- A server running Ubuntu 18.04 or 20.04.
- Sudo privileges on the server.
- A terminal or SSH access to your server.
2. Update Your System
Open your terminal and run the following command to update your package index:
sudo apt update
3. Install Nginx
Step 1: Install Nginx
Run the following command to install Nginx:
sudo apt install nginx -y
Step 2: Start and Enable Nginx
After the installation, start the Nginx service and enable it to run on boot:
sudo systemctl start nginx sudo systemctl enable nginx
Step 3: Check Nginx Status
You can check the status of Nginx to ensure it’s running:
sudo systemctl status nginx
You can also open your web browser and navigate to your server’s IP address. You should see the Nginx welcome page.
4. Install MySQL
Step 1: Install MySQL Server
Run the following command to install MySQL:
sudo apt install mysql-server -y
Step 2: Secure MySQL Installation
After the installation, run the security script to enhance MySQL security:
sudo mysql_secure_installation
Follow the prompts to set a root password and configure other security settings.
5. Install PHP
Step 1: Install PHP and Required Extensions
To install PHP and the required extensions for Nginx and MySQL, run the following command:
sudo apt install php-fpm php-mysql -y
Step 2: Configure PHP Processor
Open the PHP configuration file for Nginx:
sudo nano /etc/php/7.4/fpm/php.ini
Ensure the following lines are set (uncomment if necessary):
cgi.fix_pathinfo=0
Step 3: Restart PHP-FPM Service
After making changes to the PHP configuration, restart the PHP-FPM service:
sudo systemctl restart php7.4-fpm
6. Configure Nginx to Use PHP
Step 1: Create a Server Block for Your Website
Create a new configuration file for your website:
sudo nano /etc/nginx/sites-available/your_domain
Add the following configuration:
server {
listen 80;
server_name your_domain;
# Replace with your domain or server IP
root /var/www/your_domain;
# The root directory where your website files are stored
index index.php index.html index.htm;
# Default files to load when accessing the root directory
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
# Adjust PHP version as necessary
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
Step 2: Enable the Server Block
Create a symbolic link to enable the server block configuration:
sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
Step 3: Test Nginx Configuration
Test the Nginx configuration for any syntax errors:
sudo nginx -t
Step 4: Restart Nginx
Restart the Nginx service to apply the changes:
sudo systemctl restart nginx
7. Create a PHP Info File
To test the PHP installation, create a PHP info file in your document root:
sudo nano /var/www/your_domain/info.php
Add the following content:
<?php phpinfo(); ?>
8. Accessing Your Application
Open your web browser and navigate to http://your_domain/info.php or your server’s IP address. You should see the PHP information page.
9. Conclusion
You have successfully installed the LEMP stack (Linux, Nginx, MySQL, PHP) on your Ubuntu server. This powerful combination provides a robust platform for hosting dynamic web applications. Be sure to secure your server, regularly update your software, and explore further configurations to optimize performance.