Installing Apache On CentOS 8

One of the common services that a Linux server offers is being a web server

Our web server will host dynamic websites and web applications. So we will install a LAMP (Stack) server. LAMP was one of the first stacks to become popular. It is composed of Linux, the Apache web server, the MySQL database, and either PHP, Python, or Perl.

Install Apache

Step 1 : We will start with the installation of the Apache web server:

sudo yum install httpd

Press y and Enter

Step 2 : We can start it using the command systemctl:

sudo systemctl start httpd.service

Step 3 : we need to add it to the system startup services:

sudo systemctl enable httpd.service

Configure Firewall

Before testing the service, we need to make sure that the server firewall allows web access

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

Test Apache

Step 1 : We need to create an example HTML page to be opened at the client web browser:

sudo bash -c 'echo "<h1>Welcome</h1>" > /var/www/html/index.html'

Step 2 : We can now test the web server by typing in any other machine web browser inside the same network IP address of the server (http://Server_IP_Address).

Set Up Virtual Hosts

Now we will set up two virtual hosts on Apache to show Apache’s capacity to support multiple websites.

Virtual hosts allow us to host multiple domains. Each configured domain will direct the visitors to a specific folder specified to the website, which holds its information.

Step 1 : we need to make the folders where we are going to store our websites.

sudo mkdir -p /var/www/devtutorial1.local/home
sudo mkdir -p /var/www/devtutorial2.local/home

Step 2 : We must also change the permissions for the general web directory /var/www

sudo gpasswd -a "$USER" apache
sudo chown -R "$USER":apache /var/www
find /var/www -type f -exec chmod 0660 {} \;
sudo find /var/www -type d -exec chmod 2770 {} \;

Step 3 : we need to create an example HTML page to be opened at the client web browser:

nano /var/www/devtutorial1.local/home/index.html

Then we add some HTML code

<html>
  <head>
    <title> Home Page</title>
  </head>
  <body>
    <h1>Welcome to the home, web server 1</h1>
  </body>
</html>

For the second host

nano /var/www/devtutorial2.local/home/index.html

And then we put in the following HTML code

<html>
  <head>
    <title> Home Page</title>
  </head>
  <body>
    <h1>Welcome to the home, web server 2</h1>
  </body>
</html>

Step 4 : we need to create the virtual host files in the Apache configuration folder

sudo nano /etc/httpd/conf.d/devtutorial1.local.conf

Then we put the following configuration inside it:

<VirtualHost *:80>

    ServerName www.devtutorial1.local
    ServerAlias devtutorial1.local
    DocumentRoot /var/www/devtutorial1.local/home
    ErrorLog /var/log/httpd/devtutorial1.local_error.log
    CustomLog /var/log/httpd/devtutorial1.local_requests.log combined

</VirtualHost>

We save the file, For the second virtual host.

sudo nano /etc/httpd/conf.d/devtutorial2.local.conf

And we put the following commands inside it:

<VirtualHost *:80>

    ServerName www.devtutorial2.local
    ServerAlias devtutorial2.local
    DocumentRoot /var/www/devtutorial2.local/home
    ErrorLog /var/log/httpd/devtutorial2.local_error.log
    CustomLog /var/log/httpd/devtutorial2.local_requests.log combined

</VirtualHost>

Step 5 : we need to restart the Apache service.

sudo systemctl restart httpd.service

Step 6 : Then we go to the client web browser and we type the domain in the address bar

Configure Apache

Enable Mod_rewrite Module

The mod_rewrite module is enabled by default on CentOS 8.

httpd -M

Step 1 : If the rewrite_module does not appear in the output, you can enable it by editing 00-base.conf.

sudo nano /etc/httpd/conf.modules.d/00-base.conf

Step 2 : Add or uncomment the following line:

LoadModule rewrite_module modules/mod_rewrite.so

Step 3 : We need to restart the Apache service.

sudo systemctl restart httpd.service

Enable .htaccess File

Step 1 : Before using a .htaccess file, we need to update the AllowOverride setting

sudo nano /etc/httpd/conf/httpd.conf

Step 2 : Locate the <Directory /var/www> section and change the AllowOverride directive from None to All:

<Directory "/var/www">
    AllowOverride All
    # Allow open access:
    Require all granted
</Directory>

Step 3 : Restart Apache to apply the change:

sudo systemctl restart httpd.service
[menu name=”centos8″]

Add Comment