Skip to main content

Command Palette

Search for a command to run...

Use Apache2 to Serve Static Sites

Published
1 min readView as Markdown
Use Apache2 to Serve Static Sites

We will be learning how to use Apache2 on our Linux Ubuntu machine to serve static sites.

Lets install apache2 first

sudo apt update
sudo apt install apache2 -y
sudo systemctl status apache2

Check your local browser http://localhost to check if apache index.html is showing.

This is the path where apache serves files from: /var/www/html/

Create an index.html file here to show your own custom web page.

To create a custom directory we do the following.

sudo mkdir -p /var/www/mywebsite
sudo chown -R $USER:$USER /var/www/mywebsite

Add HTML:

nano /var/www/mywebsite/index.html

Create the config file:

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

Add this:

<VirtualHost *:80>
    ServerName mywebsite.local
    ServerAlias www.mywebsite.local

    DocumentRoot /var/www/mywebsite

    <Directory /var/www/mywebsite>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/mywebsite_error.log
    CustomLog ${APACHE_LOG_DIR}/mywebsite_access.log combined
</VirtualHost>

Then enable the site:

sudo a2ensite mywebsite.conf
sudo systemctl reload apache2

You can disable the previous one (optional).

sudo a2dissite 000-default.conf
sudo systemctl reload apache2

Now Make Sure these Permissions are correct!

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

To check config you can:

sudo apachectl configtest