Table of Contents
Setting up a LEMP (Linux, Nginx, MySQL, PHP) server on Ubuntu is a fundamental task for hosting web applications and websites. In this comprehensive guide, we’ll walk you through each step, ensuring that your server is secure, optimized, and ready to handle your web traffic. So, let’s get started!
Before we begin, make sure you have SSH access to your Ubuntu server. This allows you to manage your server remotely and execute commands from your terminal.
The first step is to update the package index on your Ubuntu server. This ensures that you have the latest information about available packages and their versions.
sudo apt update
Nginx is a powerful and efficient web server that will serve as the backbone of our LEMP stack.
sudo apt install nginx
To enable Nginx to accept incoming connections, we need to allow it through the firewall.
sudo ufw app list sudo ufw allow 'Nginx Full'
Now, if you enter your server’s IP address in a web browser, you should see the default Nginx landing page, confirming that Nginx is successfully installed.
MySQL is a popular and robust database management system that will store and manage your application’s data.
sudo apt install mysql-server
During the installation process, you will be prompted to set a password for the MySQL root user. Make sure to choose a strong and secure password.
sudo mysql
PHP is a server-side scripting language used for dynamic web content and is essential for running PHP-based applications.
sudo apt install php-fpm php-mysql
If you need a particular PHP version, like php8.2-fpm
, you can mention it in the package name when you’re installing it.
To make Nginx work seamlessly with PHP, we need to make some configurations. Open the Nginx configuration file:
sudo nano /etc/nginx/sites-available/default
Within the file, locate the index directive and make sure to include index.php in the list. After that, insert the following lines inside the location ~ .php$ block. You can also remove the ‘#’ symbol in front of ‘fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
‘ and make sure to update the PHP version accordingly.
include fastcgi_params; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
Save and close the file by pressing Ctrl+X, Y, and Enter.
Now restart your server using this command:
systemctl restart nginx
To verify that PHP is working correctly, create a test PHP file in your web server’s root directory. For example:
sudo nano /var/www/html/index.php
Add the following PHP code to the file:
<?php phpinfo(); ?>
Save and close the file.
Now, open your web browser and navigate to http://your_server_ip
. You should see a comprehensive page detailing PHP information, confirming that PHP is correctly installed and operational.
Congratulations! You’ve successfully set up a powerful LEMP server on your Ubuntu system. From this point, you can begin deploying and hosting your web applications with ease. Remember to follow security best practices and keep your server updated to ensure optimal performance and safety. Happy hosting!
1 comment