Before beginning the installation, make sure you have:
- Access to a CentOS 7 server.
- A user with sudo privileges for performing administrative tasks.
Step 1: Update System
Before installing, it's important to update system packages and dependencies. Open the terminal and run the following command:
sudo yum update
Step 2: Install Nginx
Nginx is a popular web server known for its efficiency and easy configuration. To install Nginx on CentOS 7, use the command:
sudo yum install nginx
After installation, start the Nginx service and set it to start automatically on system boot:
sudo systemctl start nginx
sudo systemctl enable nginx
To verify that Nginx is running, open your web browser and enter your server's IP address. You should see the Nginx welcome page.
Step 3: Install MySQL (MariaDB)
MySQL is a popular relational database system. CentOS 7 uses MariaDB, a compatible replacement for MySQL, by default. To install MariaDB, run:
sudo yum install mariadb-server mariadb
After installation, start the MariaDB service and enable it to start on boot:
sudo systemctl start mariadb
sudo systemctl enable mariadb
Next, run the security script to perform basic MariaDB security setup, including setting a password for the root user:
sudo mysql_secure_installation
Step 4: Install PHP v7
PHP is a server-side scripting language used for web application development. To install PHP v7 on CentOS 7, you need to add the EPEL repository and then install PHP:
sudo yum install epel-release
sudo yum install php php-mysql
After installation, restart Nginx to load the new configuration:
sudo systemctl restart nginx
Step 5: Test PHP
To verify that PHP is working correctly with Nginx, create a PHP test file in the web server's root directory:
echo "<?php phpinfo(); ?>" | sudo tee /usr/share/nginx/HTML/phpinfo.php
Open http://server_domain_or_IP/phpinfo.php
in your web browser. You should see a page displaying PHP configuration information.
You have successfully installed and configured the LEMP stack on your CentOS 7 server. This stack provides a solid foundation for hosting web applications and sites. With Nginx as the web server, MariaDB for database management, and PHP for dynamic web content, you are ready for developing and deploying modern web applications.