Hosting a website on Ubuntu 18.04 using apache2

Apache  is an open-source free http server that works on both Unix and Windows operating system. For anyone unfamiliar with the term, an http server is where all your website’s content is stored, so that it can be accessed by the client. This blog post runs through how I installed apache2 and hosted a webpage that was accessible through our home’s LAN. Feel free to try it out for yourself! It’s an easy process that takes less than an hour to finish.

1. Make sure your system is updated

Use this command, to make sure all the packages in your Ubuntu installation have been updated. This ensures that you get the best installation.

sudo apt-get update

2. Install apache

Use the apt-get command for a secure installation. The apt command obtains the package from the repository.

sudo apt-get install apache2

As soon as you type this, a prompt asks whether additional disk space may be used or not. Hit y to proceed with the installation

3. Start apache2

Use the following command to start the apache server

sudo systemctl start apache2 

4. Check whether apache2 is running

sudo service apache2 status

This should return a statement like this: ping

5. Open the index.html file

Installing apache will create a file index.html in the directory /var/www. Navigate over to the directory using the command

cd /var/www

Next, open the index.html file using gedit

sudo gedit index.html

Using sudo to open the index.html file makes sure you can create changes to the contents of the file.

6. Create your webpage

The index.html file already comes with some default content in it. You can now remove this content, and replace it with your own webpage.

Once you’re done, save it and then head over to your internet browser and type in

 localhost 

Your webpage should appear!

7. Create a new webpage (optional)

If you don’t want to change the index.html file and would rather have a different file with a unique name, use gedit to create a different file

gedit yourname.html

note: replace yourname in yourname.html to whatever you want to call your file. For example if you want to call it mooncheese, use mooncheese.html

You can now write your content and save it.

However now when you type localhost into your web browser, the contents of index.html are going to show up and not yourname.html.

To bring up yourname.html go to your browser and type in

localhost/yourname.html

Your webpage should now show up in the browser.

If you aren’t okay with this and want yourname.html to show up if you type in localhost on your browser go to step

8. Access your webpage through the LAN

LAN stands for local area network. In most cases, this means the wi-fi you’re connected to. Any devices connected to your LAN will be able to access your webpage.

To access your webpage on any other device apart from the machine that your server is running on, first use the following command (in the machine your server is running on)

ifconfig

This command shows the system’s network interfaces. Scroll down to find the inet address. This is the address asigned to your device. It’s a number that looks something like:

192.168.0.097

Type this address the internet browser of the device you want to access your webpage from.

If you created a new webpage ( in step 7 ) and want to go to that page, type the following into your web browser

192.168.0.197/yourname.html

Your webpage should show up!

9. Change the directory configuration settings (optional)

If you’ve created a webpage (see step 7) and would like it to show up instead of index.html when you type in localhost try the following steps

Open terminal and head to the dir conf folder in the mods enabled directory. To do that type:

cd /etc/apache2/mods-enabled

Then use gedit to open the dir.conf file

sudo gedit dir.conf

Once you open this, go to the line DirectoryIndex and change it so that the first page mentioned is yourname.html

ping2

Doing this means that the first file a web-browser will look up is yourname.html

Finally, go to your internet browser and type in

localhost

You should see yourname.html

When accessing yourname.html from another device connected to the LAN you no longer have to use

192.168.0.197/yourname.html

instead just using the inet address alone will do

192.168.0.197

If you type the above into the url bar of your internet browser, your webpage should show up!

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Design a site like this with WordPress.com
Get started