The benefits of installing the LEMP stack on CentOS 7 include high performance and low memory usage provided by Nginx, optimized performance and security provided by MySQL and CentOS, the flexibility of PHP for building a variety of web applications, cost-effectiveness due to the free and open-source software, and ease of management and configuration with a wealth of documentation and tutorials available online.
Here is a step-by-step guide on how to install the LEMP stack on CentOS 7.
Before you start, make sure you have the following:
Step 1: Update the system
- A server running CentOS 7.
- A user account with sudo privileges.
- A terminal application (such as PuTTY for Windows users) to access the server.
First, update the system to ensure you have the latest packages and security updates:
Step 2: Install Nginx
Nginx is a lightweight web server that is used to serve static content, reverse proxy, and load balance HTTP traffic. To install Nginx on CentOS 7, run the following command:
After the installation is complete, start the Nginx service and enable it to start automatically at boot:
sudo systemctl start nginx
sudo systemctl enable nginx
To check if Nginx is running, open your web browser and enter your server's IP address in the address bar. You should see the default Nginx web page.
Step 3: Install MySQL
MySQL is a popular open-source database management system. To install MySQL on CentOS 7, run the following command:
sudo yum install mysql-server
After the installation is complete, start the MySQL service and enable it to start automatically at boot:
sudo systemctl start mysqld
sudo systemctl enable mysqld
Run the following command to secure your MySQL installation:
This will prompt you to set a root password, remove anonymous users, disallow remote root login, and remove the test database. Follow the prompts to complete the process.
Step 4: Install PHP
PHP is a popular programming language used to build dynamic web applications. To install PHP on CentOS 7, run the following command:
sudo yum install php php-mysql
After the installation is complete, restart the Nginx service to load the PHP module:
Step 5: Test the LEMP stack
sudo systemctl restart nginx
To test the LEMP stack, create a PHP file in the Nginx web root directory:
sudo nano /usr/share/nginx/html/info.php
Add the following code to the file:
Save and close the file. Then, open your web browser and enter the following URL:
You should see a page with information about your PHP installation.
Congratulations! You have successfully installed the LEMP stack on CentOS 7. You now have a powerful web server that can serve static content, run dynamic web applications, and manage databases.