How to setup a Web Server with Apache, MySQL, and PHP on CentOS 8

Author: Saksham

Basically it will be a LAMP setup. LAMP is an acronym for Linux, Apache, MySQL, and PHP/Perl that makes it possible to host dynamic web applications on a server.

To install the LAMP Stack on a CentOS 8 cloud server. You need SSH access to your server.

Prerequisites for this LAMP installation –

  • A working CentOS 8 server
  • A non-root user account with sudo privileges

Install Apache on CentOS 8

For installing Apache web server, we need to update the system and install httpd on CentOS 8

First update the server using below command.

$ sudo dnf update

Now install Apache, which is identified by the httpd package.

$ sudo dnf install httpd -y

Once installation is complete then start the Apache daemon and enable it to start during system boot using below commands.

$ sudo systemctl enable httpd
$ sudo systemctl start httpd

In CentOS 8 the firewall is enabled by default, so we have to allow connections for Apache by opening http and https ports for the web server to function.

$ sudo firewall-cmd --permanent --zone=public --add-service=http
$ sudo firewall-cmd --permanent --zone=public --add-service=https

Now verify that the firewall rule was applied properly using this command.

$ sudo firewall-cmd --permanent --list-all

This command will show status of the firewall rules and allowed services through firewall:

  target: default
  icmp-block-inversion: no
  services: cockpit dhcpv6-client http https ssh
  masquerade: no
  rich rules:

After checking http and https in services list reload firewall for the new rules to take effect using below command.

$ sudo firewall-cmd --reload

Now, test the web server installation by visiting your server’s public IP Address in a browser. You should see the Apache default test page. Now move on to next step.

Install MySQL on CentOS 8

When we have our web server up and running, we need to install a database server like MySQL to allow storage and management of data for dynamic websites such as WordPress. But since the base MySQL is not available in CentOS repositories, we can substitute it with MariaDB, which uses the same commands and table structures.

For installing MySQL run the command below:

$ sudo dnf install mariadb-server

After installation is complete, enable and start MySQL to run during system boot.

$ sudo systemctl enable mariadb

$ sudo systemctl start mariadb

Now, we have to secure the database server by removing insecure default settings and configurations and setting a new root password through the secure installation script.

$ sudo mysql_secure_installation

Now enter a new root password different from your server’s password and verify the installation by logging in to MySQL.

$ mysql

Output of this command should be similar to:

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

This output shows that the MariaDB server is working perfectly well, and now you can create databases and tables through the console. Now move on to next step.

Install PHP on CentOS 8

For installing PHP andMySQL module, run the command below:

$ sudo dnf install php php-mysqlnd

Now once the installation is complete, we need to restart Apache to enable PHP and the MySQL module for your server andto make it run during system boot.

$ sudo systemctl restart httpd

Testing PHP Functionality in CentOS 8

To verify PHP functionality with Apache, we need to create a sample PHP file to confirm if the server can run the script.

$ sudo nano /var/www/html/demo.php

Now add these line of codes in this file.



Save and close this file.

Now, when you go to your web browser and visit the server’s public IP address followed by /demo.php. You will be able to see the PHP configurations and details in the browser in a tabular form.

When you see this PHP page properly in browser then it mean, you have successfully installed LAMP on your CentOS 8 server.

Now your server is a fully working Web Server and ready for use for any web application or CMS like WordPress.

Happy coding.

blog author bio

Author: Saksham

Saksham is a driven Computer Science student with a passion for programming and exploring new technologies. In his articles, he provides valuable insights of the topics, showcasing his ability to communicate complex ideas in a clear and concise manner. With strong analytical and problem-solving skills, Saksham is poised to make a significant impact in the world of Computer Science.

Share on: Share Poonam yogi blogs on twitter Share Poonam yogi blogs on facebook Share Poonam yogi blogs on WhatsApp Create Pin in Pinterest for this post

Related Posts


Please give us your valuable feedback

Your email address will not be published. Required fields are marked *

Google core updates November 2021 are rolling now
Google core updates November 2021 are rolling now

What websites are being affected by google core update November 2021?
Google’s guidance regarding broad core algorithm update are as follows:
Expect widely noticeable effects, such as spikes or drops in search rankings.

Read full article
60+ SSC GK question with answer pdf: general awareness- GK for SSC
60+ SSC GK question with answer pdf: general awareness- GK for SSC

SSC GK Question with Answers: General Awareness (GA) and public information (GK) Section is considered among the high-scoring segments of the SSC Tier-1 Exam.

Read full article
Load balancing vs failover
Load balancing vs failover

Load balancing vs failover – Load balancing is a technique of distributing your requests over a network when your server is facing heavy traffic load and putting load on the CPU or disk or database IO rate.

Read full article

Some important study notes