- Guide – Usage of DirectoryIndex in Apache/2 with (.htaccess) File
- Setup index.html as a default page when directory accessed
- Setup multiple file as a default page when directory accessed
- Configure and host an application in Apache web server in Linux
- Prerequisites
- Allowing Apache Traffic through Firewall
- Managing Apache Services
- Setting Up Virtual Hosts in Apache
- Step 1: Create a directory for your domain
- Step 2: Set ownership and permissions
- Step 3: Create sample index.html page for your domain
- This is a test page for test.org website!
- Step 4: Create a new virtual host file
- Step 5: Enable virtual host file
- Step 6: Configure hosts file (Optional)
- Step 6: Test for errors
- Step 7: Test if Apache is serving your domain name
- Configure Apache to listen on different port
- Allow/Deny access from specific IP addresses
- Step 1: Enable apache .htaccess
- Step 2: Create .htaccess file
- Step 3: Allow/Deny IP addresses
Guide – Usage of DirectoryIndex in Apache/2 with (.htaccess) File
DirectoryIndex used to allow you to land default page when client access a directory. For example, When a visitor request a directory to access it on your website, you can define the file to load when they request to a directory. To display a “index.html” file instead of “index.php” when client request a directory . Simply we can create a .htaccess file on our root directory or any other location.
Setup index.html as a default page when directory accessed
#vim /var/www/html/.htaccessDirectoryIndex index.html
Here we have define default page “index.html” to load if client request “/web” directory.
We can also define multiple file to load default if client access “/web” directory.
Setup multiple file as a default page when directory accessed
#vim /var/www/html/.htaccessDirectoryIndex index.html index.php index.shtml
You can see I have define DirectoyIndex first load default page “index.php” than “index.php” and so on.
If client request a directory web server try to load first “index.html” file. If “index.html” file is not available on the accessed directory, than web server try to load next define file “index.php” and so on.
If non of them files are available on directory. Apache server try to revert it’s default setting and it will display an error message like “ 403 Error-Forbidden! ” . If you get this error that means Indexing is not allowed on your web server.
Here you can see my vhost configuration file for example.
ServerName www.example.com DocumentRoot /var/www/html CustomLog /var/log/httpd/vhosts/www.example.com/access_log combined ErrorLog /var/log/httpd/vhosts/www.example.com/error_log Options -Indexes -MultiViews +FollowSymLinks #AllowOverride None AllowOverride All #AllowOverride AuthConfig Limit FileInfo Order allow,deny Allow from all DirectoryIndex index.html DirectoryIndex index.html index.php index.shtml Deny from all
After editing your Apache configuration file you will need to restart/reload Apache service.
# service httpd reload OR # service httpd restart
Configure and host an application in Apache web server in Linux
In our previous article, “How to install Apache web server on Linux”, we explained how to install Apache on a Linux machine. Now in this article, we will follow up with the basic configuration of Apache that will include
- Allow Apache Traffic through Firewall
- Managing Apache Services
- Setting Up Virtual Hosts in Apache
- Configure Apache to listen on a different port
- Allow/Deny access from specific IP addresses
Prerequisites
Note: The commands discussed in this article have been tested on Ubuntu 20.04 LTS (Focal Fossa). The same commands are also valid for Debian distribution.
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:
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'
Managing 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:
In the following output, active (running) status shows that the Apache service is active and running without any issues.
For 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
Setting Up 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 “test.org”. For multiple domains, follow the same steps for each domain.
Step 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 test.org, we will create the directory with the following command:
Make sure to replace test.org with your domain name.
Step 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/ test.org
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/ test.org
Step 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/test.org directory.
We are using the Nano editor for this purpose:
$ sudo nano /var/www/test.org/index.html
Add the following content in the file.
This is a test page for test.org website!
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.
Step 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 test.org, we will create a separate virtual host file.
Issue the following command in Terminal to create the virtual host file for your domain:
$ sudo nano /etc/apache2/sites-available/test.org.conf
Add the below content in the file.
ServerAdmin [email protected] ServerName test.org ServerAlias www.test.org DocumentRoot /var/www/test.org/html ErrorLog $/error.log CustomLog $/access.log combined
Make sure to replace the test.org with your own domain name.
Once you are done with editing, save and close the file.
Step 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 test.org.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:
Step 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:
Add the following entry in this file replacing the server_IP and domain_name with the IP address and domain name of your environment.
In our example, the entry would be:
Now save and close the /etc/hosts file.
Step 6: Test for errors
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. You may also see the following warning message in the test result.
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
Add the following entry in the file replacing test.org with your domain name:
Again, test Apache for configuration errors. Now you will see the warning has removed.
Step 7: 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.
In our example, it would be:
The following page indicates the virtual host has been successfully configured and Apache is serving our domain name.
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_nmuber.
1. Edit the /etc/apache2/ports.conf file using the below command:
$ sudo nano /etc/apache2/ports.conf
This is the default view of the ports.conf file where you can see the port 80 configured as default port.
Change this 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/test.org.conf file
In the above command, make sure to replace the test.org.conf with your virtual host file name.
Find the entry 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 .
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/test.org.conf file, add an entry in the following way:
Once you are done, restart the Apache service using the following command in Terminal:
$ sudo systemctl restart apache2
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/test.org.conf
After the VirtualHost block, append the following lines in the file:
Options Indexes FollowSymLinks AllowOverride All Require all granted
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.
Then create .htaccess file here using the following command:
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
That is all there is to it! In this article, you have learned the basics of Apache configurations 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. For more information about Apache configurations, visit Apache server official documentation at http://httpd.apache.org/docs/.
Ummara Mushtaq is a Telecommunication engineer with two years of experience in server support and networking. She writes technical articles based on Linux system administration for LinuxWays.