How to Install Apache and PHP (LAMP stack) on Rocky Linux 8/Centos 8/Alma Linux 8

In this guide we are going to install Apache, PHP and set up a virtual host in Centos 8 Linux.

The Apache HTTP Server(Apache), is one of the most popular free and open-source cross-platform web server software, released under the terms of Apache License 2.0. Apache is popular as part of the LAMP setup, being the A in the Acronym. The apache server functionality can be extended with the many available modules.

PHP is a general-purpose scripting language geared towards web development. It is one of the popular programming languages for the web. Popular tools such as WordPress are coded using php. Big companies like Facebook also uses php heavily.

Related Content

Prerequisites

To follow along this guide, please ensure you have the following:

  • An Updated Rocky Linux 8 server
  • Root access or User with sudo access who can install packages and edit configurations
  • Internet access

Ensuring that the server is up to date

Before proceeding, lets make sure that we have the latest packages in our system. Run the following command to update the system:

1
2
3
4
5
$ sudo dnf -y update
Last metadata expiration check: 1:45:37 ago on Sun 29 Aug 2021 08:52:16 AM UTC.
Dependencies resolved.
Nothing to do.
Complete!

Installing PHP in Rocky Linux 8

Now that Apache is set up, we will need to install PHP. For php files to be served, php needs to be set up in the system. In this guide we will install php 7.4.

PHP 7.4 is not yet available in the default repositories. The Remi Repository is the perfect repo to install php7.4. Remi repo is a free and stable YUM repository mainly for the PHP stack. It contains packages for the latest versions of PHP.

To enable Remi Repository in our Centos Server:

1
sudo dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-8.rpm

Confirm that the remi repository has been installed and enabled with this command:

1
2
3
4
5
6
7
8
9
$ sudo dnf repolist
repo id                                 repo name
appstream                               CentOS Linux 8 - AppStream
baseos                                  CentOS Linux 8 - BaseOS
epel                                    Extra Packages for Enterprise Linux 8 - x86_64
epel-modular                            Extra Packages for Enterprise Linux Modular 8 - x86_64
extras                                  CentOS Linux 8 - Extras
remi-modular                            Remi's Modular repository for Enterprise Linux 8 - x86_64
remi-safe                               Safe Remi's RPM repository for Enterprise Linux 8 - x86_64

You should see the remi repos as part of the elist.

Now that the repository has been installed, lets search for php. Use this command:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
$ sudo dnf module list php

Last metadata expiration check: 2:14:53 ago on Sun 29 Aug 2021 08:52:16 AM UTC.
CentOS Linux 8 - AppStream
Name                Stream                      Profiles                                 Summary
php                 7.2 [d]                     common [d], devel, minimal               PHP scripting language
php                 7.3                         common [d], devel, minimal               PHP scripting language
php                 7.4                         common [d], devel, minimal               PHP scripting language

Remi's Modular repository for Enterprise Linux 8 - x86_64
Name                Stream                      Profiles                                 Summary
php                 remi-7.2                    common [d], devel, minimal               PHP scripting language
php                 remi-7.3                    common [d], devel, minimal               PHP scripting language
php                 remi-7.4                 common [d], devel, minimal               PHP scripting language
php                 remi-8.0                    common [d], devel, minimal               PHP scripting language

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

From the list. we can see that the default one is 7.2. Performing a dnf install php will install the 7.2 which we do not want. Let’s enable the 7.4 using this command:

1
sudo dnf module enable php:remi-7.4

If for some reason its failing, you can reset the existing module with this command:

1
sudo dnf module reset php

Now that the repo has been enabled, lets install php with this command:

1
sudo dnf install -y php php-common php-cli

Once successful. confirm the version installed with this command:

1
2
3
4
5
6
$ php -v

PHP 7.4.23 (cli) (built: Aug 24 2021 16:33:30) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.23, Copyright (c), by Zend Technologies

Now that Apache and php have been installed successfully, lets set it up to serve some traffic

Installing Apache on Rocky Linux 8

Now that we have updated our packages, Lets install Apache. Apache is available in the default repositories as the package httpd. You can check info about it using the following command:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
$ sudo dnf info httpd

Last metadata expiration check: 1:48:56 ago on Sun 29 Aug 2021 08:52:16 AM UTC.
Installed Packages
Name         : httpd
Version      : 2.4.37
Release      : 39.module_el8.4.0+778+c970deab
Architecture : x86_64
Size         : 4.3 M
Source       : httpd-2.4.37-39.module_el8.4.0+778+c970deab.src.rpm
Repository   : @System
From repo    : appstream
Summary      : Apache HTTP Server
URL          : https://httpd.apache.org/
License      : ASL 2.0
Description  : The Apache HTTP Server is a powerful, efficient, and extensible
             : web server.

Lets install it using dnf:

1
sudo dnf install -y httpd

Starting and enabling the service

By default the service is not started. Lets start it with this command:

1
sudo systemctl start httpd

To confiirm that its working as expected, use the status command and ensure that it indicates as active like shown below:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ sudo systemctl start httpd
$ sudo systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
  Drop-In: /usr/lib/systemd/system/httpd.service.d
           └─php-fpm.conf
   Active: active (running) since Sun 2021-08-29 10:43:52 UTC; 17s ago
     Docs: man:httpd.service(8)
 Main PID: 84446 (httpd)
   Status: "Running, listening on: port 80"
    Tasks: 213 (limit: 23800)
   Memory: 31.0M
   CGroup: /system.slice/httpd.service
           ├─84446 /usr/sbin/httpd -DFOREGROUND
           ├─84447 /usr/sbin/httpd -DFOREGROUND
           ├─84448 /usr/sbin/httpd -DFOREGROUND
           ├─84449 /usr/sbin/httpd -DFOREGROUND
           └─84450 /usr/sbin/httpd -DFOREGROUND

Aug 29 10:43:52 test-app-server systemd[1]: Starting The Apache HTTP Server...
Aug 29 10:43:52 test-app-server httpd[84446]: AH00558: httpd: Could not reliably determine the server's fully qualified doma>
Aug 29 10:43:52 test-app-server systemd[1]: Started The Apache HTTP Server.
Aug 29 10:43:52 test-app-server httpd[84446]: Server configured, listening on: port 80

If you have any firewall enabled, please ensure you enable http and https traffic to allow access to apache.

To confirm that php is installed and working fine, please visit the server’s IP address in the browser (http://server_ip). If you don’t know the server IP, get it using this command in your terminal:

1
curl -4 icanhazip.com

If all is well, you should see Apache Test page

Configuring virtual hosts

With Apache, it is possible to host multiple sites on the same server where the Apache is running. This can always be achieved using multiple domains each representing a site that the server is hosting.

Apache virtual hosts are similar to Nginx server blocks . Each virtual host will have configuration details for a single host. You can have as many virtual hosts as you want.

In this example, we will use the domain site1.citizix.com for the virtual host but be free to use the domain of your choice. Please note that the DNS for the domain to be used should already be configured to point to the IP address of the server. Confirm that with:

1
dig -t A site1.citizix.com

Creating directory and sample content

The default page of apache is configured as a default virtual host serving content from /var/www/html. It’s a best practice to create the directory of your site in the /var/www directory since its a best practice to serve from there.

Lets create a directory for our site with the following command:

1
sudo mkdir /var/www/site1.citizix.com

Lets create a simple index.php page to serve from our site. You will require vim installed for this to work, if not use this command:

1
sudo dnf install -y vim

Edit the file:

1
sudo vim /var/www/site1.citizix.com/index.php

Add content to the file:

1
<?php phpinfo(); ?>

Finally, lets make sure that the apache user owns the site directory:

1
sudo chown -R apache.apache /var/www/site1.citizix.com

Creating virtual host

Apache Virtual host files specifies the configuration of each sites and tell the Apache web server how to respond to various domain requests.

The Apache configurations will be stored in the directory /etc/httpd/conf.d/. Lets create a config for our site:

1
sudo vim /etc/httpd/conf.d/site1.citizix.com.conf

Add the following content:

1
2
3
4
5
6
7
8
<VirtualHost *:80>
    ServerName site1.citizix.com
    ServerAlias site1.citizix.com
    DocumentRoot /var/www/site1.citizix.com
    DirectoryIndex index.php
    ErrorLog /var/log/httpd/site1.citizix.com.error.log
    CustomLog /var/log/httpd/site1.citizix.com.requests.log combined
</VirtualHost>

This will tell Apache the following:

  • ServerName and ServerAlias - name of the host to serve
  • DocumentRoot - where to find the root directly that holds the publicly accessible web documents
  • DirectoryIndex - The file that apache will serve when the site is accessed
  • ErrorLog - where to store error logs for this site
  • CustomLog - where to store request logs for this particular site

Save and close the file when you are finished

Testing the Virtual Host

Now that the configuration is in place, we need to test that everything is working as expected. Since we created the index.php file above, lets access it by going to http://server_url/. In my case this is http://site1.citizix.com/. You should see a PHP info page.

Conclusion

In this guide, we managed to set up Apache to serve traffic on a Rocky Linux 8 server.

comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy