How to Install LEMP (Linux, Nginx, MySQL, PHP) on Ubuntu for High-Performance Websites?
Are you looking to set up a robust web server that can handle high-traffic websites with lightning-fast performance? Look no further! In this tutorial, we’ll guide you through the process of installing LEMP (Linux, Nginx, MySQL, PHP) stack on Ubuntu. LEMP is a powerful combination of open-source software that ensures exceptional performance, stability, and security for your website. Let’s dive into the step-by-step installation process.
Step 1: Preparing Your Ubuntu Server
Before installing the LEMP stack, make sure your Ubuntu server is up-to-date by running the following commands:
Step 2: Installing Nginx
Nginx is a high-performance web server that will serve as the backbone of your LEMP stack. Execute the following commands to install Nginx:
After the installation is complete, start the Nginx service:
–or–
Allow nginx in firewall using these commands:
To verify that Nginx is running, open your web browser and enter your server’s IP address (e.g. http://127.0.0.1/). If everything is set up correctly, you should see the default Nginx welcome page.
Step 3: Installing MySQL
MySQL is a widely used relational database management system. Let’s install MySQL using the following command:
During the installation process, you’ll be prompted to set a password for the MySQL root user. Make sure to choose a strong and secure password. Once the installation is complete, start the MySQL service:
To enhance the security of your MySQL installation, run the MySQL secure installation script:
Follow the prompts to remove the anonymous user, disallow remote root login, remove test databases, and reload the privilege tables.
Note:
If you are not prompted to set password in the above method, you can use these steps to set password for your user (e.g. root):
You will be logged into MySQL server, now run this query to set the password:
Verify your MySQL installation and password with this command, you will be asked to enter your password afterwards.
Step 4: Installing PHP
PHP is a popular server-side scripting language that enables dynamic content generation. Install PHP and its required dependencies using the following commands:
If you need to install any specific version of PHP, you can use this command instead:
If you need to install additional PHP extensions for your framework, like Magento, you can install them like this:
Once installation is done, you can start it like this:
Step 5: Configuring Nginx to Use PHP
To configure Nginx to work with PHP, open the default Nginx configuration file using your preferred text editor:
Inside the server block, locate the location ~ \.php$
line and uncomment it by removing the ‘#’ character. It should look like this:
Save the file and exit the text editor.
Step 6: Testing the LEMP Setup and Verifying PHP Configuration
To ensure that Nginx can handle PHP files properly, create a new PHP test file:
Add the following code to the file:
Save the file and exit the text editor. Open your web browser and navigate to http://your_server_ip/info.php
. You should see the PHP information page, which confirms that PHP is working correctly with Nginx.
Step 7: Restarting Services
After making configuration changes, you may restart the Nginx, MySQL and PHP-FPM services:
Conclusion
Congratulations! You’ve successfully installed the LEMP stack on your Ubuntu server. By combining Linux, Nginx, MySQL, and PHP, you have set up a powerful web server that can handle high-performance websites with ease. Take your time to explore the various configuration options for each component, and enjoy the benefits of a robust LEMP stack for your website.
Remember, maintaining the security of your server and keeping your software up-to-date are essential practices for a safe and efficient web hosting environment. Happy coding!
Liked the post, share with others: