Install Linux, Apache, MariaDB, PHP (LAMP Stack) on CentOS 8 / RHEL 8
LAMP Stack stands for Linux, Apache, MariaDB, and PHP stack. It is most widely used to host websites, blogs, etc..
Here is the small guide about installing and configuring web server stack with the latest release of CentOS 8 / RHEL 8.
Install Linux
Install the CentOS 8 or Red Hat Enterprise Linux 8 using the below link:
READ: Step by Step Guide To Install CentOS 8 (with Screenshots)
READ: Step by Step Guide To Install RHEL 8 (with Screenshots)
By this time, you should have a Linux machine ready with you. Now, we will install Apache, MariaDB, and PHP on top of it.
Switch to the root user.
$ su -
Install Apache Web Server
Let us start installing the Apache webserver. The package name of the Apache server is httpd. Install the httpd rpm package using the YUM command.
In RHEL 8, ensure rhel-8-for-x86_64-baseos-rpms and rhel-8-for-x86_64-appstream-rpms enabled on your system.
yum install -y httpd
Start the Apache web service by using the systemctl command.
systemctl start httpd
Make the Apache server to start automatically. Type the following command on terminal and press enter.
systemctl enable httpd
Check the Apache Web server status using the below command.
systemctl status httpd
Output:
Firewall
By default, the system firewall blocks the HTTP connections coming from external machines. So, to test or use the Apache web server, we need to configure the firewall to allow HTTP requests from external machines.
firewall-cmd --permanent --add-port=80/tcp firewall-cmd --reload
Test Apache
Open up a web browser and then enter the following URL into the web address.
http://localhost
OR
http://your.ip.adr.ess
You will get the below Apache test web page. This test page confirms that the Apache is working fine.
Apache’s default document root is /var/www/html on RHEL, and the main configuration file is /etc/httpd/conf/httpd.conf. Additional configurations for Apache Web server are stored in /etc/httpd/conf.d/ directory.
Install MariaDB
Next, we will install the MariaDB database server. Type the following command in the terminal and press enter to install MariaDB from base OS repository.
You can also install MariaDB from the MariaDB official community repository instead of OS repository.
READ: How To Install MariaDB on CentOS 8 / RHEL 8
In RHEL 8, ensure rhel-8-for-x86_64-appstream-rpms repository enabled on your system.
yum install -y mariadb mariadb-server
Start the MariaDB server service using the systemctl command.
systemctl start mariadb
Enable the MariaDB server to start during every boot.
systemctl enable mariadb
Check the MariaDB server status using the below command.
systemctl status mariadb
Output:
Secure MariaDB
Nex is to make the MariaDB server secure by using the mysql_secure_installation command.
This command enables you to improve the security of your MariaDB installation in the following ways:
- Set a password for root accounts.
- Remove root accounts that are accessible from outside the localhost.
- Delete anonymous-user accounts.
- Remove the test database (which by default can be accessed by all users, even anonymous users), and privileges that permit anyone to access databases with names that start with test_.
mysql_secure_installation
Output:
Install PHP
By default, the Apache webserver supports the HTML language only, not PHP. To have PHP support, we will need to install the PHP rpm package along with support for the MariaDB database.
CentOS 8 / RHEL 8 comes with PHP v7.2. In case you want to install PHP 7.3, you can use steps in the below link.
READ: How To Install PHP 7.3 On CentOS 8 / RHEL 8
In RHEL 8, ensure rhel-8-for-x86_64-appstream-rpms repository enabled on your system.
yum install -y php php-mysqlnd
Restart the Apache Web server after the installation of the PHP to take an effect of PHP installation.
systemctl restart httpd
Test LAMP Stack
To test PHP, we will place a .php file on to the default directory of the Apache.
echo "" > /var/www/html/info.php
Now open your web browser and type the following URL into the web address.
http://localhost/info.php
OR
http://you.ip.addr.ess/info.php
The page will look like below.
You will see lots of useful information about our PHP installation, such as the installed PHP version, PHP extension details, etc.
Scroll down the browser to check the support for the MariaDB. You will get the screen like below.
Conclusion
I hope this post helped you setup LAMP stack on CentOS 8 / RHEL 8. Additionally, install Let’s Encrypt SSL certificate for secure communication and improved security. Also, install phpMyAdmin for managing the database server via the web interface.
Please share your feedback in the comments section.