- How to Host a Website on an Apache Web Server
- Install Apache Web Server in Linux
- Host a Simple HTML Website on Apache
- Linux Shell Tips
- Manage Apache Web Server in Linux
- How To Change Default Index Page in Apache
- How To Change Default Index Page in Apache Web Server
- Change default index page using Apache Configuration
- Change default index page using .htaccess
How to Host a Website on an Apache Web Server
The Apache HTTP Server (commonly referred to simply as Apache), is a free and open-source web server software brought to you by the Apache Software Foundation. Apache has been around for more than 2 decades and is considered beginner-friendly.
In this tutorial, you will learn how to install an Apache webserver to host a simple HTML website running on a Linux platform.
Install Apache Web Server in Linux
On Ubuntu Linux and other Debian-based distributions such as Linux Mint, Apache can be installed with the following command.
$ sudo apt install apache2 -y
On Red Hat Enterprise Linux and related distributions such as CentOS, Fedora, and Oracle Linux, Apache can be installed with the following command.
On Ubuntu Linux and other Debian-based distributions, you can start and check the status of the Apache webserver by running the commands below.
$ sudo systemctl start apache2 $ sudo systemctl status apache2
On Red Hat Enterprise Linux and related distributions, run the following commands to start and check the status of Apache.
$ sudo systemctl start httpd $ sudo systemctl status httpd
Once you have confirmed that Apache is active, open a web browser and enter the IP address of your Linux server. You may also enter localhost in place of your server IP.
You should see a test page that confirms that Apache is up and running properly.
http://IP-Addresss OR http://localhost
Host a Simple HTML Website on Apache
After you have confirmed that Apache is working properly, you are now ready to add your website content. On Apache, the default location where publicly accessible web content is stored in /var/www/html. This is commonly referred to as the website root.
The first page that is loaded when users visit your website is called the index page. Let us create one as follows.
Firstly, change into the website root with the command below.
On Ubuntu Linux, run the command below to rename the default index page file.
$ sudo mv index.html index.html.bk
On Red Hat, there is nothing to rename here as the default index page file is not stored in this location.
Next, create a new index file with:
Copy and paste the sample HTML code below into the open text editor.
Linux Shell Tips
This website is hosted on Apache.
Save and close the index.html file.
Now, go back to your web browser and refresh the page. You should see your new website as shown in the image below.
Manage Apache Web Server in Linux
As we wrap up this tutorial, let us highlight some basic commands for managing Apache in addition to the ones that we have already used. As you may have noticed, the Apache web service is referred to as apache2 on Ubuntu while it is called httpd on Red Hat Linux.
To configure Apache to automatically start when the Linux server is rebooted, run:
$ sudo systemctl enable apache2 $ sudo systemctl enable httpd
To disable automatic starting of Apache when the Linux server is rebooted, run:
$ sudo systemctl disable apache2 $ sudo systemctl disable httpd
$ sudo systemctl restart apache2 $ sudo systemctl restart httpd
$ sudo systemctl stop apache2 $ sudo systemctl stop httpd
Conclusion
In this tutorial, we have described how to install Apache on Ubuntu Linux as well as Red Hat Linux. We also showed you how to replace the default Apache web page with your own content.
How To Change Default Index Page in Apache
For every website, you will eventually need to change the default index page to the home page of your website/blog. In this article, we will look at how to change default index page in Apache Web Server.
How To Change Default Index Page in Apache Web Server
Here are the steps to change default index page in Apache web server. You can change default index page via Apache Server configuration file, or using .htaccess file. We will look at both approaches below.
Change default index page using Apache Configuration
Apache configuration file is present at one of the following locations depending on your installation:
- /etc/apache2/httpd.conf
- /etc/apache2/apache2.conf
- /etc/httpd/httpd.conf
- /etc/httpd/conf/httpd.conf
Open terminal and run the following command to open Apache configuration file
$ sudo vi /etc/apache2/httpd.conf
You will see the following lines of code.
# # DirectoryIndex: sets the file that Apache will serve if a directory # is requested. #DirectoryIndex index.html index.php
Change index.html index.php to your choice of web page (e.g home.html).
Make sure you have placed this file home.html at /var/www/html/. If you have placed it in a different folder (e.g /var/www/html/product/) then modify the path above accordingly (e.g /product/home.html).
DirectoryIndex /product/home.html
You can also add multiple index pages to Apache as shown below
DirectoryIndex home.html welcome.html
Restart Apache web server to apply changes.
$ sudo service apache2 restart
Change default index page using .htaccess
You can also change default index page for Apache using .htaccess. Before proceeding, please enable mod_rewrite (.htaccess) in your Apache web server.
Open .htaccess file, typically located at /var/www/html/.htaccess
$ sudo vi /var/www/html/.htaccess
Add the following line to .htaccess file to set index page to home.html.
Restart Apache web server to apply changes.
$ sudo service apache2 restart
That’s it. Open browser and visit http://your_server_or_ip and you will see the new page. Replace your_server_or_ip with your domain name, or server IP address.
Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it Today!