DEV Community

Cover image for How to Provision an Ubuntu Server & Using Apache for Hosting a Website
Vincent Agu
Vincent Agu

Posted on

How to Provision an Ubuntu Server & Using Apache for Hosting a Website

In today’s fast-paced digital landscape, the need for reliable and scalable hosting solutions has never been greater. Amazon Web Services (AWS) EC2 (Elastic Compute Cloud) stands out as a powerful service for deploying and managing virtual servers in the cloud.

My aim in writing this article to demonstrate how web applications can be deployed quickly. It is a practical demonstration of setting up an ubuntu server and using apache2 to deliver webcontents over HTTP and Optionally HTTPS.

Taking a practical and beginner-friendly approach, I believe this article has something for everyone, whether you’re a developer exploring cloud technologies, a startup launching your first product, or a tech enthusiast curious about AWS.

Prerequisites

  • An active AWS account
  • An SSH Client or Terminal (e.g Termius, GitBash, etc)
  • A domain - optional (for configuring https. I used a free domain)

Step-by-Step Guide

1. Setting up an EC2 Instance on AWS

  • Log into your aws account, navigate to EC2 and click Launch Instance.
  • Input the name for your Instance.
  • Choose Ubuntu 20.04 LTS as the Amazon Machine Image.
  • Select the system architecture 64-bit (x86).
  • Select an instance type t2.micro for a simple server (free tier eligible).
  • Create a key pair & download the private key for SSH access.
  • Ensure the Auto-assign public IP is Enabled
  • Create a security group to allow HTTP (port 80), SSH (port 22) and optionally HTTPS (port 443).
  • Configure Elastic Block Store (EBS) storage (8gb of gp3 for a simple static website).
  • Launch the instance and take note of Public IP Address.

Image description

2. SSH into Instance & Install Apache Web Server

  • Open your SSH client or Terminal (e.g Termius, GitBash, etc)
  • Naviagte to where you downloaded the private key and change the file permissions. chmod 400 myprivate-key.pem
  • SSH into the server
       ssh -i myprivate-key.pem ubuntu@ipaddress
Enter fullscreen mode Exit fullscreen mode
  • Update the apt package manager and install Apache Web Server:
    sudo apt update  ## to update apt package index.  
    sudo apt upgrade  ## to upgrade all updated applications
    sudo install apache2 -y ## to install Apache2 webserver (-y means 'Yes' to all prompts & automatically download dependencies)  
Enter fullscreen mode Exit fullscreen mode
  • Confirm that Apache2 is up and running using:
    sudo systemctl status apache2  ##You should see the message active (running).
Enter fullscreen mode Exit fullscreen mode

3. Create your HTML, CSS & JS File and send a copy to the Instance.

N/B: For the purpose of this article, I used a HTML file with Internal CSS.

  • Launch Visual Studio Code, create/open a project folder and create an index.html file
  • Write HTML code and integrated internal CSS for styling for your website. (You can skip if you already have your html, css and js).
  • From your SSH Client or Terminal, copy the index.html file or files as the case may be (css & js) to the server using secure copy via ssh. And move the file(s) to /var/www/html which is the default folder in which apache renders from. This should automatically replace the default apache's index.html file since it's same name index.hmtl.
scp -i myprivate-key.pem ~/project/index.html ubuntu@ipaddress:/home/ubuntu

sudo mv /home/ubuntu/index.html /var/www/html/
Enter fullscreen mode Exit fullscreen mode

Alternatively, if you have your project files on Git hosting platforms (github, gitlab, Bitbucket, Gitbucket, etc), you can initialized a repository in your Instance and clone/pull the remote repository into your instance.

  • Ensure you set the right permissions for your files. And for enhanced security, change the /var/www/html directory ownership to be owned by Apache's user and group (www-data).
       sudo chmod 644 /var/www/html/yourfile.html 
       sudo chown www-data:www-data /var/www/html -R
Enter fullscreen mode Exit fullscreen mode
  • Confirm that your page is up and running. By typing your aws assigned public ip-address in a browser. Your website should displayed

4. Setting up Firewall and Ensuring you have ports open for SSH, HTTP and optionally HTTPS open
UFW (Uncomplicated firewall comes pretty much installed by default in most linux distributions like the Ubuntu 20.04LTS. But it is important to check the status and ensure you have your SSH port open before enabling it to avoid locking yourself out from the server.

  • Check if UFW is already pre-installed ufw --version. If installed, the version should be shown. After that you should check the status sudo ufw status

  • Ensuring that ports for ssh, http, and optionally https are
    open.

      sudo ufw allow 22      # SSH
      sudo ufw allow 80      # HTTP
      sudo ufw allow 443     # HTTPS
Enter fullscreen mode Exit fullscreen mode
  • Enable Firewall
      sudo ufw enable
Enter fullscreen mode Exit fullscreen mode

5. Register a Domain, and point the A Record and CNAME Record to your servers IP-address
The CNAME Record is typically an allias for your website, if you want your website to be accessible from the 'www' and 'non-www' versions of your doamin name (e.g setting 'A Record' to www.vee-op.dev.com and 'CNAME Record' to vee-op.dev.com). Both pointing to same IP-address.

  • Register in domain platform provider e.g Namecheap, GoDaddy,etc. You can also get a free subdomain from a free domain platform e.g https://freedns.afraid.org/
  • Set an A Record and CNAME in your doamin DNS settings and point both to your AWS server's ip-address.

If you are using the above free domain service, navigate to subdomains and click on the ADD NEW. After setting the A Record you won't be able to set a free CNAME Record since you don't own the doamin but alternatively you can set a second A Record with your allias name pointing to your Ip.

  • Now, Back to your instance from the terminal/ssh client, edit the default virtualhost confg file and modify/add ServerName and ServerAllias to represent the doamin name you set earlier in your Domain provider's platform.
      sudo nano /etc/apache2/sites-available/000-default.conf
Enter fullscreen mode Exit fullscreen mode
     <VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName www.vee-op.dev.com
        ServerAlias vee-op.dev.com
        DocumentRoot /var/www/html
    </VirtualHost>
Enter fullscreen mode Exit fullscreen mode
  • Enabled this new changes on the conf file by;
      sudo a2ensite 000-default.conf   # to enable config file
      sudo apache2ctl configtest       # Test the config (it should return an "OK") 
      sudo systemctl restart apache2     # to restart apache2 webserver
Enter fullscreen mode Exit fullscreen mode

Congrats... Your site should be up and accessible over HTTP

Congratulation image

6. Optional - Configuring HTTPS
To serve your website over https, you will need an SSL certificate and have port 443 open as shown optionally above on both your AWS security group and UFW if you enabled that too. You can obtain a free SSL from Lets Encrypt. To do this, you have to install certbot and run the certbot command to obtain the free ssl for your domain(s)

  • Install certbot using: While in the server from your terminal/ssh client run the command
       sudo apt install certbot python3-certbot-apache -y
Enter fullscreen mode Exit fullscreen mode
  • Run certbot to obtain free SSL certificate for your domain(s).
sudo certbot --apache -d www.vee-op.dev.com -d vee-op.dev.com
Enter fullscreen mode Exit fullscreen mode

If SSL certificates have been acquired sucessful, certbot will automatically create and place a new SSL config. file (usually 000-default-le-ssl.conf or similar name) in the /etc/apache2/sites-available and redirect all http traffic to https

  • Restart or Relaod Apache
       sudo systemctl restart apache2 
Enter fullscreen mode Exit fullscreen mode

Testing:

You can type your domain name in a browser to confirm your webpage is displaying properly.

Genius... You did it again!!! I knew you were capable

Super Mario thumbs up

Conclusion

By following this guide, you’ve taken your first steps into the powerful world of cloud computing. From provisioning your instance to setting up a functional web server, you've experienced firsthand how AWS can streamline the process of deploying web applications.

If you found this guide helpful, I’d love to hear your feedback! Feel free to share your thoughts or questions in the comments below.

Don’t forget to follow me here and on LinkedIn and Github

Top comments (0)