Installing Apache and PHP on CentOS 8

This article explains How to install Apache and PHP on CentOS 8

Apache webserver

Apache httpd is an open-source web server especially for Unix platforms born in 1995. It is one of the most popular and used servers in the world. Almost half of the websites are served with Apache.

Install Apache and PHP on CentOS 8

Apache webserver can be downloaded and compiled from the project website. But this is not optimal so it is best to install it from the CentOS 8 repository.

So, open a terminal and install it using the following command:

:~$ sudo dnf install httpd

Install Apache on CentOS 8

In case you do not have sudo enabled just run the commands as root without the command sudo.

As you see in the image, many dependencies have to be satisfied. But this will be taken care of by DNF. So just accept the installation proposal and it will start.

Apache web server is installed but still needs to start the service. To do this, run:

:~$ sudo systemctl start httpd

However, you should set up Apache to start at boot time.

:~$ sudo systemctl enable httpd

Now you can check the status of the service to see if it is initiated or not.

:~$ sudo systemctl status httpd

Check the Apache service status to know if it is running

On CentOS, the Firewall is on by default, so if you do not set the rules for Apache it will not work.

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

And apply the changes by restarting the firewall:

:~$ sudo firewall-cmd --reload

Now, open a web browser and access your server. In the address bar type http://IP-address-of-the-server or http://Domain-name

If everything went well, you will have to see the Apache test page at CentOS 8.

Apache Default page. So, it is running

Now, Install PHP

PHP is a fast, easy to learn programming language available for Linux. It is in this language that many great web applications such as WordPress have been created. In this sense, having it is a guarantee of compatibility with many applications.

To install PHP and some of its modules, just run the following command:

:~$ sudo dnf install php php-common php-pecl-apcu php-cli php-mysqlnd

Install PHP on CentOS 8

Then, as a matter of course, you have to prove that everything is all right.

Create a PHP file in the Apache root directory.

:~$ sudo nano /var/www/html/test.php

And add this content:

<?php
phpinfo();
?>

Create a new PHP file to test it

Save the changes and close the file.

And since you have installed PHP after Apache, you have to restart the Apache service so that it can interpret it.

:~$ sudo systemctl restart httpd

Again, open your web browser and try to open the newly created PHP file.

If you see an image like this, then Apache and PHP work correctly on CentOS 8.

Apache and PHP are running on CentOS 8

So congratulations you’ve achieved the goal. Already the Apache web server and PHP are working properly.

Conclusion

As a conclusion to this post, we can affirm that Apache is a very useful web server used by many people around the world. Thanks to being open source we will be able to examine the source code and for this reason, it is quite safe and stable.

However, Apache needs to be coupled with a programming language for the interpretation of web sites. This is why PHP has been installed. Together they can run most of today’s web sites.

Now we want to hear from you, do you like Apache? Have you installed it? Let us know

Scroll to Top