How to Host HTML Pages on Ubuntu Server with Apache

How to Host HTML Pages on Ubuntu Server with Apache

Hosting HTML pages on your own server gives you full control over your website and its environment. Apache is one of the most popular web servers, and Ubuntu is a favored Linux distribution for hosting due to its stability and large support community. This guide will walk you through the process of setting up an Ubuntu server with Apache to host your HTML pages.

1. Setting Up Your Ubuntu Server

Before you start, ensure you have an Ubuntu server running. This could be on a local machine, a virtual machine, or a cloud service provider like AWS, DigitalOcean, or Google Cloud. If you’re using a fresh installation, your first step is to update your server’s package list.

1.1. Update the Server

Open your terminal and execute the following commands:

sudo apt update
sudo apt upgrade

This updates the package lists and upgrades your installed packages to the latest versions.

1.2. Install Apache Web Server

To install Apache, use the following command:

sudo apt install apache2

Apache will install, and the service will start automatically.

1.3. Check Apache Installation

Once installed, verify that Apache is running by entering your server’s IP address in a web browser. You should see the Apache default welcome page, confirming that the server is working.

2. Configuring Apache

Now that Apache is installed, you need to configure it to serve your HTML pages.

2.1. Understand Apache Directories

By default, Apache serves files from the /var/www/html directory. This is where you will place your HTML files.

2.2. Create Your HTML Page

Let’s create a simple HTML page. Open your terminal and type:

sudo nano /var/www/html/index.html

This opens a text editor where you can type your HTML content. For example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My First Website</title>
</head>
<body>
    <h1>Welcome to My Website</h1>
    <p>This is a simple HTML page hosted on Ubuntu with Apache.</p>
</body>
</html>

After entering the content, save and exit by pressing CTRL + X, then Y, and finally ENTER.

2.3. Set Permissions

Ensure Apache has the necessary permissions to access your files. Set the correct permissions using:

sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html

This ensures that the Apache user (www-data) owns the files and directories, and they have the appropriate permissions.

3. Managing Apache Virtual Hosts

If you plan to host multiple websites or HTML pages, you should use Apache’s virtual hosts feature.

3.1. Create a Virtual Host File

Apache stores configuration files for each site in the /etc/apache2/sites-available/ directory. To create a new virtual host file, use:

sudo nano /etc/apache2/sites-available/mywebsite.conf

Replace mywebsite.conf with a descriptive name. Enter the following configuration:

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

This configuration specifies that Apache should serve files from /var/www/mywebsite when requests are made to mywebsite.com.

3.2. Enable the Virtual Host

Create the document root directory for your website:

sudo mkdir -p /var/www/mywebsite

Enable the virtual host using:

sudo a2ensite mywebsite.conf

Then, reload Apache to apply the changes:

sudo systemctl reload apache2

3.3. Update DNS Settings

Ensure your domain points to your server’s IP address by updating your DNS settings with your domain registrar. This step is necessary to make your site accessible via the domain name.

4. Securing Your Apache Server

Security is crucial for any web server. Follow these steps to secure your Apache server.

4.1. Enable UFW Firewall

Ubuntu’s Uncomplicated Firewall (UFW) helps control network traffic. Enable UFW and allow Apache traffic with:

sudo ufw allow in "Apache Full"
sudo ufw enable

4.2. Set Up SSL/TLS with Let’s Encrypt

Secure your website with HTTPS using a free SSL certificate from Let’s Encrypt. Install Certbot, the tool for managing SSL certificates:

sudo apt install certbot python3-certbot-apache

Obtain and install the certificate with:

sudo certbot --apache

Certbot will guide you through the process, including configuring automatic renewals.

4.3. Regular Security Updates

Keep your server secure by regularly applying updates:

sudo apt update
sudo apt upgrade

This helps protect against known vulnerabilities.

5. Testing and Maintaining Your Apache Server

Regular testing and maintenance ensure your server runs smoothly and securely.

5.1. Test Configuration

Before restarting Apache after changes, test the configuration with:

sudo apache2ctl configtest

This checks for syntax errors in your configuration files.

5.2. Monitor Apache Logs

Monitor Apache’s logs for errors or suspicious activity:

  • Access log: /var/log/apache2/access.log
  • Error log: /var/log/apache2/error.log

View logs using:

sudo tail -f /var/log/apache2/access.log

5.3. Backup Your Server

Regularly back up your server and website files. This can be done manually or automated using tools like rsync or third-party backup solutions.

Conclusion

Hosting HTML pages on an Ubuntu server with Apache is a straightforward process. By following this guide, you’ve learned how to install Apache, configure virtual hosts, and secure your server. With these skills, you can manage your own web server, giving you more control over your website and its environment. Remember to keep your server updated and monitor it regularly to maintain its performance and security.

Fedya Serafiev

Fedya Serafiev

Fedya Serafiev owns the website linuxcodelab.eu. He finds satisfaction in helping people solve even the most complex technical problems. His current goal is to write easy-to-follow articles so that such problems do not arise at all.

Thank you for reading the article! If you found the information useful, you can donate using the buttons below: