Configure and host an application in Apache web server in Linux - How to do it ?





Apache is the most commonly used Web server on Linux systems. Web servers are used to serve Web pages requested by client computers. This configuration is termed LAMP (Linux, Apache, MySQL and Perl/Python/PHP) and forms a powerful and robust platform for the development and deployment of Web-based applications.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform basic configuration of Apache on their Servers.
In this context, we shall look into how to Configure and host an application in Apache.

How to configure Apache on Linux (Ubuntu 20.04 LTS) ?

Here, you will learn the basic configuration of Apache such as:
i. Allow Apache Traffic through Firewall
ii. Managing Apache Services
iii. Setting Up Virtual Hosts in Apache
iv. Configure Apache to listen on a different port
v. Allow/Deny access from specific IP addresses

Let us look into the steps for Apache configuration.

1. Allowing Apache Traffic through Firewall

If a firewall is enabled on your OS, you will have to allow Apache traffic through it. The Apache server by default listens on port 80 for HTTP and 443 for https.
In order to allow HTTP (port 80) traffic through the firewall, execute the following command in Terminal:

$ sudo ufw allow 'Apache'

To allow HTTPS (port 443) traffic through the firewall, execute the following command in Terminal:

$ sudo ufw allow 'Apache Secure'

In order to allow both HTTP (port 80) and HTTPS (port 443) traffic through in the firewall, execute the following commands in Terminal:

$ sudo ufw allow 'Apache Full'


How to Manage Apache Services ?

After installation, the Apache service automatically starts running in the background. To view the status of Apache service, issue the below command in Terminal:

$ systemctl status apache2

An active (running) status shows that the Apache service is active and running without any issues.

To manually starting the Apache service, use the below command:

$ sudo systemctl start apache2

For enabling the Apache service to automatically start at startup/boot, use the below command:

$ sudo systemctl enable apache2

For restarting the Apache service, use the below command:

$ sudo systemctl restart apache2

For stopping the Apache service, use the below command:

$ sudo systemctl stop apache2


Steps to configure Virtual Hosts in Apache ?

Virtual host in Apache allows you to run a number of websites from a single Apache Web Server. In the following section, we will configure one virtual host for our domain “ibmimedia.com”. For multiple domains, follow the same steps for each domain.

1. Create a directory for your domain

The first step will be to create a directory for your domain. If you need to host multiple domains, create separate directories for every domain. For our domain ibmimedia.com, we will create the directory with the following command:

$ sudo mkdir /var/www/ibmimedia.com

Make sure to replace ibmimedia.com with your domain name.

2. Set ownership and permissions

The directory for our domain is currently owned by the root user. In order to allow other users to modify the files, we will need to change the ownership of the directory. Use the following command in Terminal do so:

$ sudo chown -R $USER:$USER /var/www/ ibmimedia.com

Also, set required permissions on the domain directory. The 755 in the below command assigns read and execute permissions to everyone while the read, write and execute permissions to the owner of the file.
$ sudo chmod -R 755 /var/www/ ibmimedia.com

3. Create sample index.html page for your domain

Now create a sample index.html page for your domain to serve some content. Create a file index.html in the /var/www/ibmimedia.com directory.
We are using the Nano editor for this purpose:

$ sudo nano /var/www/ibmimedia.com/index.html

Add the following content in the file.

<html>
<head>
<title>Your ibmimedia.com server block is up!</title>
</head>
<body>
<h1>This is a test page for ibmimedia.com website!</h1>
</body>
</html>

Once you are done with editing, save and close the index.html file.
Now the sample index.html page has been created for our site.

4. Create a new virtual host file

In Apaches, there is a default virtual host file that contains configurations for the default web server. For our domain ibmimedia.com, we will create a separate virtual host file.
So, Issue the following command in Terminal to create the virtual host file for your domain:

$ sudo nano /etc/apache2/sites-available/ibmimedia.com.conf

Add the below content in the file.

<VirtualHost *:80>
ServerAdmin admin@ibmimedia.com
ServerName ibmimedia.com
ServerAlias www.ibmimedia.com
DocumentRoot /var/www/ibmimedia.com/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Make sure to replace the ibmimedia.com with your own domain name.
Once you are done with editing, save and close the file.

5. Enable virtual host file

Now we will enable the virtual host file that we have created in the previous step. Issue the below command in Terminal to do so:

$ sudo a2ensite ibmimedia.com.conf

We are not using the default virtual host file, so we can disable it. Use the following command to disable it:

$ sudo a2dissite 000-default.conf

Now reload Apache configurations using the following command:

$ systemctl reload apache2


6. Configure hosts file (Optional)

If you do not have an actual domain name and only want to test the procedure using any test domain, you will need to add an entry for your domain in the /etc/hosts file.
Edit the /etc/hosts file using the following command in Terminal:

$ sudo nano /etc/hosts

Add the following entry in this file replacing the server_IP and domain_name with the IP address and domain name of your environment.

server_IP domain_name

In our example, the entry would be:

192.168.72.157 ibmimedia.com

Now save and close the /etc/hosts file.

7. Test for errors

i. Now test Apache configurations for any errors. Use the following command to do so:

$ sudo apache2ctl configtest

If everything is okay, you will the Syntax OK message in the output.

ii. To suppress this message, add ServerName directive in the /etc/apache2/apache2.conf file.
Edit the /etc/apache2/apache2.conf file using following command in Terminal:
$ sudo nano /etc/apache2/apache2.conf
iii. Add the following entry in the file replacing ibmimedia.com with your domain name:

ServerName ibmimedia.com

iv. Save and close the file.
v. Again, test Apache for configuration errors.
Now you will see the warning has removed.

8. Test if Apache is serving your domain name

Now, test the setup by navigating to the following address in the address bar of your web browser.

http://domain_name

In our example, it would be:
http://ibmimedia.com

You will see a page which indicates the virtual host has been successfully configured and Apache is serving our domain name.

How to Configure Apache to listen on different port ?

By default, Apache listens to web traffic on port 80. There are some cases when you may need to change the Apache port like when some other service is already listening on port 80, ISP has blocked port 80, or you want to prevent port 80 attacks. However, remember that after changing the default port, you must have to point the browsers to the new port like http://domain_name:port_number.

1. Edit the /etc/apache2/ports.conf file using the below command:

$ sudo nano /etc/apache2/ports.conf

Change the port number to any other value you want the Apache server to listen to.
Save and close the ports.conf file once you are done with the editing.

2. Now, you will need to configure your virtual host to listen on the new port. To edit the virtual host file, use the following command in Terminal:

$ sudo nano /etc/apache2/sites-avaialble/ibmimedia.com.conf file

In the above command, make sure to replace the ibmimedia.com.conf with your virtual host file name.
Find the entry <VirtualHost *:80> and change the value from 80 to any number you want the Apache to listen to. For instance, to change the port number to 9090, the entry would be changed to <VirtualHost *:9090>.

Note: To set a port number, choose a value with 1024 to 65535 range.
Once done, save and close the file.

Apache can also be configured to listen on multiple ports. For instance, to set port 80 and 9090 as listening ports, add the following entries in the /etc/apache2/ports.conf file:

Listen 80
Listen 9090

Also in the /etc/apache2/sites-avaialble/ibmimedia.com.conf file, add an entry in the following way:

<VirtualHost *:80 *:9090>

Once you are done, restart the Apache service using the following command in Terminal:

$ sudo systemctl restart apache2


Steps to Allow/Deny access from specific IP addresses ?

In the following section, we will see how to allow/deny specific IP addresses from accessing our site. We will use the .htaccess file for this purpose.

Step 1: Enable apache .htaccess

First, we will need to enable the .htaccess file. To do so, issue the following command in Terminal:

$ sudo nano /etc/apache2/sites-available/ibmimedia.com.conf

After the VirtualHost block, append the following lines in the file:

<Directory /var/www/html/example.com/public_html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

Now save the file and restart apache using the following command:

$ sudo systemctl apache2 restart


Step 2: Create .htaccess file

Now we will need to create .htaccess file. Navigate to the virtual host root directory.

$ cd /var/www/ibmimedia.com

Then create .htaccess file here using the following command:

$ sudo nano .htaccess


Step 3: Allow/Deny IP addresses

To deny certain IP addresses from accessing your website, you will need to add entries in the .htaccess file in the following way:
order deny, allow
# To deny IP address 192.168.9.8
allow from 192.168.9.8
# To deny all IP addresses from 192.168.9.0 to 192.168.9.255
allow from 192.168.9

To allow For this purpose, IP addresses from accessing your website, you will need to add entries in the .htaccess file in the following way:
order deny, allow
# To deny all IP addresses
Deny from all
# It will allow the IP address 192.168.9.30
allow from 192.168.9.30
# It will allow all IP addresses from 192.168.9.0 through 192.168.9.255
allow from 192.168.9

[Need urgent assistance to configure Apache Web Server? We are available to help you. ]


Conclusion

This article will guide you on how to configure #Apache on #Linux. This includes #firewall configuration, managing Apache services, setting up virtual hosts, changing default listening ports, and allowing/denying specific IPs from accessing the #sites.
Apache #HTTP Server is a free and open-source web server that delivers web content through the internet. It is commonly referred to as Apache and after development, it quickly became the most popular HTTP client on the web.
The httpd. conf file is the main configuration file for the Apache web server. It's highly recommended to run Apache in standalone type for better performance and speed. ServerRoot "/etc/httpd" The option ServerRoot specifies the directory in which the configuration files of the Apache server lives.
On most systems if you installed Apache with a package manager, or it came preinstalled, the Apache configuration file is located in one of these locations:
1. /etc/apache2/httpd. conf.
2. /etc/apache2/apache2. conf.
3. /etc/httpd/httpd. conf.
4. /etc/httpd/conf/httpd. conf.


Related Post


Keep In Touch

We hope to hear from you.

Accept File Type: jpg,jpeg,png,txt,pdf,doc,docx