LAMP (Linux, Apache, MySQL/MariaDB, PHP) is a popular open-source stack for hosting dynamic websites and web applications. In this guide, we’ll walk you through the steps to install and configure a LAMP stack on a CentOS 8 server.
Prerequisites:
- A server running CentOS 8
- Root or sudo access to the server
Step 1: Update the System
Before starting, make sure your system is up to date:
sudo dnf update -y
This ensures that you have the latest software and security patches installed.
Step 2: Install Apache Web Server
Apache is the web server component of the LAMP stack. To install Apache, use the following command:
sudo dnf install httpd -y
Once the installation is complete, start and enable Apache to run on system startup:
sudo systemctl start httpd
sudo systemctl enable httpd
You can verify that Apache is running by checking its status:
sudo systemctl status httpd
Step 3: Adjust Firewall Settings
If your server uses firewalld
, you’ll need to adjust the firewall to allow web traffic. Run the following commands to allow HTTP and HTTPS traffic:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
Step 4: Install MariaDB (MySQL Alternative)
MariaDB is a popular database management system and a drop-in replacement for MySQL. To install MariaDB, run:
sudo dnf install mariadb-server -y
Start and enable the MariaDB service:
sudo systemctl start mariadb
sudo systemctl enable mariadb
To secure your MariaDB installation and set a root password, run the security script:
sudo mysql_secure_installation
Follow the prompts to configure the security options, such as removing anonymous users and disallowing remote root login.
Step 5: Install PHP
PHP is the programming language used to generate dynamic content on your website. To install PHP along with some common PHP extensions, run:
sudo dnf install php php-mysqlnd php-fpm php-json php-gd -y
Once PHP is installed, restart Apache to make sure PHP works with your web server:
sudo systemctl restart httpd
Step 6: Test PHP
To ensure PHP is properly installed and working, create a test PHP file in Apache’s default document root:
sudo nano /var/www/html/info.php
Add the following PHP code to the file:
<?php
phpinfo();
?>
Save and close the file. Now, open a web browser and navigate to your server’s IP address, followed by /info.php
. For example:
http://your_server_ip/info.php
If PHP is working correctly, you will see a page displaying PHP version information and various configuration details.
Step 7: Secure Apache and MariaDB (Optional)
- Set File Permissions for Apache: Set appropriate file and directory permissions to avoid unauthorized access.
- SSL Encryption: Consider securing your Apache server with SSL by installing a free SSL certificate from Let’s Encrypt.
- Database Security: Regularly update your database, use strong passwords, and ensure that only necessary users have access to the database.
Conclusion
You have successfully installed and configured a LAMP stack on your CentOS 8 server. Your server is now ready to host dynamic web applications using Apache, MariaDB, and PHP. Make sure to regularly update your system and applications to maintain security and performance.