How to Set Up a Website with Apache on Ubuntu 24.04

Apache is one of the most popular web servers in the world, known for its versatility and compatibility with many different operating systems. Setting up a website with Apache on an Ubuntu server involves a few straightforward steps, including installing the server software, configuring virtual hosts, and ensuring proper permissions and security.

This tutorial will guide you through the necessary steps using Ubuntu 24.04 LTS as a reference, but the instructions are applicable to any version of Ubuntu and any Linux operating system.

Step 1: Install Apache

First, you need to install Apache on your Ubuntu server. Open your terminal and run the following commands:

sudo apt update
sudo apt install apache2

This will install Apache and all required dependencies. After the installation is complete, you can check if Apache is running with:

sudo systemctl status apache2

Step 2: Configure Your Domain

Before proceeding, make sure your domain name (e.g., zahiralam.com) is pointing to the IP address of your server. This involves configuring DNS settings through your domain registrar.

Step 3: Create a Directory for Your Website

Create a directory where your website’s files will reside:

sudo mkdir -p /var/www/html/zahiralam

You can now add your HTML, CSS, JavaScript, and other files to this directory.

Step 4: Set Up Apache Virtual Host

You need to create a virtual host file for your domain. This file will direct Apache to serve your site’s files when someone accesses your domain.

1. Create a virtual host configuration file:

sudo vim /etc/apache2/sites-available/zahiralam.com.conf

2. Add the following configuration to the file, modifying DocumentRoot if your files are in a different directory:

<VirtualHost *:80>
    ServerAdmin admin@zahiralam.com
    ServerName zahiralam.com
    ServerAlias www.zahiralam.com
    DocumentRoot /var/www/html/zahiralam

    <Directory /var/www/html/zahiralam>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/zahiralam.error.log
    CustomLog ${APACHE_LOG_DIR}/zahiralam.access.log combined
</VirtualHost>

3. Save and close the file.

Step 5: Enable the Site

Enable your new site configuration and reload Apache to apply changes:

sudo a2ensite zahiralam.com.conf
sudo systemctl reload apache2

Step 6: Adjust Permissions and Ownership

Although your site might work without this step, it’s a good practice to set the proper permissions and ownership to ensure security and proper server operation:

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

Step 7: Test Your Website

Open a web browser and navigate to zahiralam.com. You should see your website live. If not, check Apache’s error logs for troubleshooting:

less /var/log/apache2/zahiralam.error.log

Setting up a website with Apache on Ubuntu is a process that can be completed in just a few steps. By following this guide, you can host your own static website and gain the flexibility to manage it on your own server. Always remember to keep your server and software updated to protect against vulnerabilities.

Leave a Reply

Your email address will not be published. Required fields are marked *