In this article, we’ll guide you through the process of how to deploy a static website on Nginx web server. We’ll cover the steps to install Nginx, upload your website files, configure a custom domain name, and secure your website with a free SSL certificate using Let’s Encrypt.
Step 1: Setting Up Your Server
For the sucess of this guide, you will need a server. This could be a VPS purchased from any of the hosting providers, or an EC2 instance provisioned from AWS. The server should be accessible and ready to receive prompts. In this guide, we will use a VPS operating on Ubuntu.
Access Your Server
Access your server as required by your server provider. See below the common ways of accessing the server.
ssh username@server_ip_address #To use this method, you require the VPS username, IP and password.
For EC2 Access, if you have the key pair, use the command as shown below. Alternatively, access your AWS dashboard, navigate to the EC2 instance, and connect using the EC2 Instance connect option which is usually the default option.
chmod 400 "nginx-web.pem" #Assuming your keypair has the name nginx-web.pem. This is applicable on a Linux Machine. On windows, refer to the video above.
ssh -i "nginx-web.pem" [email protected]
OR
ssh -i "nginx-web.pem" ubuntu@Instance-IP
#where ubuntu is the username when you select the Ubuntu as the AMI
Update System Packages
Keep your server up-to-date.
sudo apt update && sudo apt upgrade -y
Install Nginx
Install and start the Nginx web server. To test if it was properly installed, visit the IP on your brower- it should show the default Nginx Page.
sudo apt install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
Step 2: Uploading Your Static Website
Create a Directory for Your Website
Organize your website files under /var/www/yourdomain.com
sudo mkdir -p /var/www/yourdomain.com
Upload Files
There are different ways to get your files on the server. The popular ones are using SCP, cloning a github Reporistory and or wget. See the sample commands below.
scp -r /local/path/to/website/ username@server_ip:/var/www/yourdomain.com
scp -i /path/to/your-key.pem -r /local/path/to/website/ ec2-user@your-ec2-public-ip:/var/www/yourdomain.com
wget https://link-to-your-compressed-web-file.zip
Set Correct Permissions
Ensure Nginx has access to the files:
sudo chown -R $USER:$USER /var/www/yourdomain.com
Step 3: Configuring Nginx for Your Website
Create an Nginx Server Block
Create a configuration file for your site:
sudo nano /etc/nginx/sites-available/yourdomain.com
Add the following content.
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
root /var/www/yourdomain.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Enable the Configuration
Link the configuration to Nginx’s sites-enabled directory:
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
Test and Restart Nginx
Check for syntax errors and restart if syntax is ok.
sudo nginx -t
sudo systemctl restart nginx
Step 4: Configuring a Domain Name
At this point, you can now connect your domain with your server. This is done by doing a DNS setup, which involves pointing your domain name to the IP address of your server. This can be done on Cloudflare or the DNS management platform as provided by your domain provider. See how to manage your DNS records on Cloudflare below.
Note that on updating your DNS records, it may take upto 3 hours(usually within an hour) for full propagation to take place if the settings were done correctly.
Step 5: Installing Free SSL with Let’s Encrypt
Install Certbot
Install Certbot and its Nginx plugin:
sudo apt install certbot python3-certbot-nginx
Generate an SSL Certificate
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
This installs the SSL certificate, adds the redirect code to ensure that your website is only accessible on https and adds a cron job to ensure that the ssl certificate is renewed automatically
Redirect IP traffic to Domain name
You may not want your webapplication to be accessible using the IP. To reiforce this, open your config file and add the following block. Save and restart Nginx. Once done, try to access your Server IP and it will redirect you to the domain name.
nano /etc/nginx/sites-available/yourdomain.com
# Redirect traffic from the server's public IP to the domain
server {
listen 80;
server_name your-server-ip;
return 301 http://yourdomain.com$request_uri;
}
Make a donation to support us
Web Hosting and email hosting Packages
Related content: