How to Setup Apache2 Server on Ubuntu Server and Connect Domain

How to Setup Apache2 Server on Ubuntu Server and Connect Domain

To set up an Apache2 server on an Ubuntu server and connect a domain, follow the steps below:

Step 1: Install Apache2

First, update the server package index and install Apache2 using the following commands:

sudo apt-get update
sudo apt-get install apache2

Step 2: Configure Firewall

Next, configure the firewall to allow traffic on port 80 (HTTP) and 443 (HTTPS) using the following commands:

sudo ufw allow 'Apache'
sudo ufw allow 'OpenSSH'
sudo ufw enable

Step 3: Test Apache2

After installing and configuring the firewall, test if Apache2 is running by typing your server's IP address or hostname on your browser. You should see the Apache2 default page.

Step 4: Connect Your Domain

To connect your domain to the Apache2 server, you need to create an A record in your domain registrar's DNS settings. Point the A record to your server's IP address.

Step 5: Configure Virtual Host

Next, configure a virtual host on Apache2 to serve your domain. Create a configuration file for your domain using the following command:

sudo nano /etc/apache2/sites-available/{yourdomain}.conf

Add the following code to the file:

<VirtualHost *:80>
    ServerName {yourdomain}
    ServerAlias www.{yourdomain}
    DocumentRoot /var/www/html/{yourdomain}
    ErrorLog ${APACHE_LOG_DIR}/{yourdomain}-error.log
    CustomLog ${APACHE_LOG_DIR}/{yourdomain}-access.log combined
</VirtualHost>

Save and close the file.

Step 6: Enable Virtual Host

After configuring the virtual host, enable it using the following command:

sudo a2ensite {yourdomain}.conf

Step 7: Restart Apache2

Finally, restart Apache2 to apply the changes using the following command:

sudo systemctl restart apache2

After completing these steps, your domain should be connected to your Apache2 server. You can test it by typing your domain on your browser.