Hosting a website securely and efficiently involves leveraging Cloudflare’s powerful CDN and security features combined with Nginx’s robust web server capabilities on an Ubuntu 22.04 server. This comprehensive guide aims to walk you through the process step-by-step, ensuring your website is up and running smoothly.
Step 1: Deploying a Cloud Instance on Shape.host
- Log in to Shape.host Dashboard:
- Navigate to the Shape.host website and log in to your account.
- Create a New Instance:
- Click on the “Create” button located at the top right corner of the dashboard.
- From the dropdown menu, select “Instances”.
- Select Instance Location:
- Choose the desired location for your server. For this tutorial, we’ll select “New York, USA”.
- Choose a Plan:
- Select a plan that fits your requirements. For example, you might choose a plan with 2 cores CPU, 2 GB Memory, and 50 GB SSD disk space.
- Select an Operating System:
- Scroll down to the “Choose an image” section and select “Ubuntu 22.04”.
- Configure Additional Options:
- (Optional) You can configure additional options like User Data Configuration and IPv6 Networking.
- Enter a hostname for your instance, e.g., “Tutorial Ubuntu”.
- Click on the “Create instance” button to deploy the instance.
Step 2: Connecting to Your Instance
- Retrieve SSH Credentials:
- Note the IP address of your newly created instance from the Shape.host dashboard.
- Connect via SSH:
- Open a terminal on your local machine.
- Use the following command to connect to your instance:
ssh root@your_instance_ip
- Replace
your_instance_ip
with the actual IP address of your instance.
Before you begin, ensure you have the following:
- An Ubuntu 22.04 server with root access.
- A domain name registered and pointed to your server’s IP address.
- A Cloudflare account with your domain added.
- Basic familiarity with using the command line.
Step 3: Update and Upgrade Your Server
First, make sure your server is up to date by running the following commands:
apt update
apt upgrade -y
Step 4: Install Nginx
Nginx will serve as our high-performance web server. Install it with:
apt install nginx -y
Start Nginx and enable it to run on system boot:
systemctl start nginx
systemctl enable nginx
Verify Nginx installation by accessing your server’s IP address in a web browser. You should see the default Nginx welcome page.
Step 5: Configure Firewall
Allow HTTP and HTTPS traffic through the firewall:
ufw allow 'Nginx Full'
ufw enable
Confirm the firewall status:
ufw status
Step 6: Install Certbot for SSL
To secure your website with SSL/TLS certificates from Let’s Encrypt, install Certbot and the Nginx plugin:
apt install certbot python3-certbot-nginx -y
Step 7: Obtain SSL Certificates
Run Certbot to obtain SSL certificates for your domain. Replace yourdomain.com
with your actual domain:
certbot --nginx -d tutorials.shape.host
Follow the prompts to agree to the terms and conditions and provide your email address for renewal notices.
Troubleshooting DNS Issues
If Certbot fails with a DNS resolution error (NXDOMAIN) for yourdomain.com
, ensure:
- DNS Records: Verify that both
yourdomain.com
have A records pointing to your server’s IP address in your DNS provider’s control panel. - Propagation: DNS changes may take time to propagate. Wait a few minutes and try again.
Step 8: Configure Nginx
Create a new Nginx server block configuration for your website:
nano /etc/nginx/sites-available/yourdomain.com
Replace yourdomain.com
with your actual domain and configure Nginx to serve your website:
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://localhost:3000; # Replace with your application's address
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Enable the configuration by creating a symbolic link to the sites-enabled directory and test the Nginx configuration:
ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
nginx -t
systemctl reload nginx
Step 9: Configure Cloudflare
Log in to your Cloudflare account and ensure your DNS records are correctly configured:
- Add A records for
yourdomain.com
, pointing to your server’s IP address. - Set Cloudflare SSL mode to “Full (strict)” under SSL/TLS settings for enhanced security.
Step 10: Optimize Security and Performance
Utilize Cloudflare’s features like HTTP/2, Brotli compression, and firewall rules to enhance your website’s security and performance.
For those preferring managed hosting solutions, consider Shape.host’s Linux SSD VPS services. They provide reliable hosting with high-performance servers, robust security features, and excellent customer support.
By following this guide, you’ll have a secure and efficient website hosted on Ubuntu 22.04 using Cloudflare and Nginx. Whether you’re a novice or an experienced developer, this setup ensures a solid foundation for your online presence.