In this article we’ll walk you through the steps to deploy a static website on Apache web server. We will start by installing apache, upload the website content, configure a domain name, configure DNS records and finally install the free SSL certificate.
What is Apache?
Apache is one of the most widely used open-source web servers globally. It powers millions of websites and provides a robust and flexible platform for hosting various types of content, including but not limited to static and dynamic web pages.
Step-by-Step Guide to Deploying a Static Website on Apache
Step 1: Install Apache
Access your VPS in readiness to deploy your static website on apache. The focus of this article will be on a VPS/EC2 instance operating on Ubuntu. To install apache webserver, update the package repository first, then proceed. Refer to the commands below:
sudo apt update #update package repository
sudo apt install apache2 -y #Install apache
sudo systemctl start apache2 #start the service
sudo systemctl enable apache2 #enable the service start automatically on server reboot
sudo systemctl status apache2 #check current status
To confirm that the installation was a success, visit the IP address of the server <http://Your-IP-Address> on the browser and you should get the default apache page.
Step 2: Prepare Your Static Website Files and upload
Prepare your static website files for deployment. There are a number of ways to get your files to the server, including: Use github, ftp method, SCP and the wget method. In this article, we will use SCP method, as it is easy to use. Compress your website files in a file named StaticWeb.zip(use any preferred name). Open terminal/command prompt in the same location as the StaticWeb.zip and type in the following command. Replace the dummy info with the correct information.
On your server, create the root directory for your application
sudo mkdir -p /var/www/yourdomain-name #create your website's root directory
Copy website files to any directory your username has access to on the server, eg /home/your-username. If you have access to the root user, you can directory copy your website file to the path set up in the previous step.
scp StaticWeb.zip username@server-IP:/home/username/ #Upload your compressed files in your home folder
Access your server and move the file to the root directory/document root of your application.
sudo mv StaticWeb.zip /var/www/yourdomain-name/
cd /var/www/yourdomain-name/
sudo unzip StaticWeb.zip
sudo apt install unzip #Install unzip if not installed
Set up permissions so that apache process can read and serve websitye files: This is optional and is only necessary when your website doesnt work as expected due to permission issues.
sudo chmod -R 755 /var/www/yourdomain-name
sudo chown -R www-data:www-data /var/www/yourdomain-name
Step 4: Configure Apache
Set up a configuration file for the virtualhost, for the purpose of configuring a domain name.
sudo nano /etc/apache2/sites-available/yourdomain-name.conf
Add the code below:
<VirtualHost *:80>
ServerName your-domain.com
ServerAlias your-server-IP
ServerAlias www.your-domain.com
DocumentRoot /var/www/yourdomain-name
</VirtualHost>
Enable the site and reload Apache
sudo a2ensite yourdomain-name.com
sudo systemctl reload apache2
Redirect IP to Domain
For SEO benefits, you may want to redirect users visting the IP address of your server to the domain name instead. To achieve this, check out the procedure below.
Open the apache configuration file we created earlier.
sudo nano /etc/apache2/sites-available/yourdomain-name.com.conf
Add the following block to replace what we had added previously. Update the dummy information used as needed, eg your-domain, the path to the document root,etc.
<VirtualHost *:80>
ServerName your-domain.com
ServerAlias www.your-domain.com
DocumentRoot /var/www/your-domain.com
# Redirect IP to domain
RewriteEngine On
RewriteCond %{HTTP_HOST} !^your-domain\.com$ [NC]
RewriteRule ^(.*)$ http://your-domain.com/$1 [L,R=301]
</VirtualHost>
Enable the mod_rewrite module (if not already enabled):
sudo a2enmod rewrite
sudo systemctl reload apache2
Enable HTTPS with Let’s Encrypt
Adding HTTPS ensures your website is secure. Follow these steps to enable HTTPS:
Install Certbot:
Install the modules required for SSL certificate installation.
sudo apt install certbot python3-certbot-apache -y
Run Certbot to Configure SSL
Run the following command and follow the prompts to install SSL on the set domain names.
sudo certbot --apache
Renew Certificates Automatically
Certbot automatically configures certificate renewal. You can verify with:
sudo certbot renew --dry-run
At this stage, you should have a fully operation static web application running on an apache server.
Make a donation to support us
Web Hosting and email hosting Packages
Related Articles:
- Deploy Django API and react JS on Apache web server
- How to Install and configure Apache Tomcat server on Linux
- How to deploy Django project on an Apache Server
- How to Deploy a Static Website on Nginx with Domain Configuration and Free SSL on a VPS
- How to deploy a Node.js Application on an Nginx Web Server
- How To Push a Project to GitHub | A Beginner’s Guide