Part 1: Setting Up the Basic Environment
1. Introduction
Hello, my name is Nam, and I am currently a third-year AI major at FPT University. I have a strong passion for technology and have successfully deployed a project on an Ubuntu VPS platform. Through this article, I hope to guide you step by step in bringing your website closer to users in a professional and efficient manner.
1.1 What is VPS Ubuntu ?
VPS (Virtual Private Server) is an independent virtual server that operates within a larger physical server. Unlike traditional shared hosting, VPS offers dedicated and separately allocated resources.
Outstanding Advantages of VPS:
- Dedicated Resources: CPU, RAM, and storage are allocated exclusively, ensuring stable performance.
- Full Control: Root access allows customization of the server to meet specific needs.
- Scalability: Easily upgrade or downgrade resources as required.
- High Security: An isolated environment enhances security.
2. Preparation
2.1 System Requirements
- A VPS running Ubuntu Server (latest LTS version)
- At least 1GB of RAM
- Minimum 20GB of storage
- Stable internet connection
2.2 Installing the Ubuntu Operating System
To install Ubuntu accurately and efficiently, you can refer to the official guide from the Ubuntu website or watch this video tutorial on YouTube by Anh Việt Nguyễn AI. These resources provide step-by-step instructions, from downloading the ISO file and creating a bootable USB stick to completing the installation directly on your server or computer.
I recommend using Ubuntu 20.04.6 LTS (Focal Fossa). This version is stable, has long-term support (LTS), and in my experience, it integrates well with libraries and is less prone to errors compared to other versions.
2.3 Checking Connectivity
Before proceeding with the next steps, ensure that your VPS has a stable internet connection. You can verify this by pinging a popular website, such as Google:
ping -c 4 google.com
Output Example: namhost@server-host-839:~$ ping -c 4 google.com
PING google.com(hkg07s48-in-x0e.1e100.net (2404:6800:4005:80a::200e)) 56 data bytes
64 bytes from hkg07s01-in-x0e.1e100.net (2404:6800:4005:80a::200e): icmp_seq=1 ttl=58 time=45.2 ms
64 bytes from hkg07s01-in-x0e.1e100.net (2404:6800:4005:80a::200e): icmp_seq=2 ttl=58 time=45.4 ms
64 bytes from hkg07s01-in-x0e.1e100.net (2404:6800:4005:80a::200e): icmp_seq=3 ttl=58 time=44.6 ms
64 bytes from hkg07s01-in-x0e.1e100.net (2404:6800:4005:80a::200e): icmp_seq=4 ttl=58 time=45.3 ms --- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 44.557/45.115/45.430/0.332 ms
4 packets transmitted, 4 received, 0% packet loss: This result indicates that all packets were successfully sent and received, confirming a stable internet connection. Now you’re ready to move on to the next step.
2.4 Updating the System
To ensure system security and optimal performance, it’s important to update all packages. You can do this by running the following commands:
sudo apt update && sudo apt upgrade -y
3. Environment Setup
3.1 Installing Nginx
Nginx is a high-performance, lightweight web server widely used in modern web applications. To install and configure Nginx on your VPS, follow these steps:
-
Step 1: Install Nginx
sudo apt update sudo apt install nginx -y
-
Step 2: Configuring the Firewall
Before checking Nginx, you need to adjust the firewall settings to allow access to the service. When you install Nginx, it automatically registers as a service with ufw, making it easy to grant access to Nginx.
To list the application profiles that ufw supports, use the following command:
sudo ufw app list
Output: Available applications: Nginx Full Nginx HTTP Nginx HTTPS OpenSSH
If ufw is not installed run this command and enable the firewall (if not already enabled), use the following:
sudo apt-get install ufw sudo ufw enable sudo ufw status
The displayed results will include:
- Nginx Full: Allows ports 80 (HTTP) and 443 (HTTPS).
- Nginx HTTP: Allows only port 80 (HTTP).
- Nginx HTTPS: Allows only port 443 (HTTPS).
- OpenSSH: Allows SSH connections (port 22).
-
Step 3: Configuring Firewall Rules
- Activate the most restrictive firewall profile that still allows the traffic you have previously configured. This helps protect your system from unwanted threats by only opening the ports necessary for the operation of your application. For example:
- If you're only using HTTP traffic, you can enable the Nginx HTTP profile:
sudo ufw allow 'Nginx HTTP'
- If you're using both HTTP and HTTPS traffic, you can enable the Nginx Full profile:
sudo ufw allow 'Nginx Full'
- Then, check the current status of the firewall to ensure that the correct rules are applied:
sudo ufw status
Output: namhost@server-host-839:~$ sudo ufw status Status: active To Action From -- ------ ---- Nginx HTTP ALLOW Anywhere Nginx HTTP (v6) ALLOW Anywhere (v6)
-
Step 4: Starting and Enabling Nginx
sudo systemctl start nginx sudo systemctl enable nginx #Enable Nginx to start automatically on boot sudo systemctl status nginx
namhost@server-host-839:~$ systemctl status nginx ● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2024-11-25 08:09:39 +07; 6h ago Docs: man:nginx(8) Main PID: 874 (nginx) Tasks: 9 (limit: 4470) Memory: 14.1M CGroup: /system.slice/nginx.service ├─874 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; ├─875 nginx: worker process
After completing the installation, you can access the website through the IP address of your VPS, or by using http://localhost or http://127.0.0.1/ if you're working directly on the server.
If you see the Nginx welcome page, congratulations, you have successfully installed Nginx! 🎉
This confirms that Nginx is running correctly and serving content on your server.
You can also check if your computer can communicate with other devices in the same Local Area Network (LAN).
sudo apt update sudo apt install net-tools
To check the IP address of your computer, you can use the following command:
ifconfig
Output Example: namhost@server-host-839:~$ ifconfig eno1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.100.221 netmask 255.255.255.0 broadcast 192.168.100.255 inet6 fd00:db80::6 prefixlen 128 scopeid 0x0<global> inet6 2405:4802:80c2:cf00:b14:d108:2ab3:c023 prefixlen 64 scopeid 0x0<global> inet6 fd00:db80::c173:6ff3:2152:e89b prefixlen 64 scopeid 0x0<global> inet6 2405:4802:80c2:cf00:bf20:6e4:4350:e2a prefixlen 64 scopeid 0x0<global> inet6 fd00:db80::3d69:5558:97c4:e32c prefixlen 64 scopeid 0x0<global> inet6 fe80::8e42:2ec0:47fb:6a2d prefixlen 64 scopeid 0x20<link> inet6 2405:4802:80c2:cf00::6 prefixlen 128 scopeid 0x0<global> ether 10:e7:c6:7e:e6:0a txqueuelen 1000 (Ethernet) RX packets 383751 bytes 114963673 (114.9 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 597949 bytes 249543541 (249.5 MB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1000 (Local Loopback) RX packets 3299 bytes 314563 (314.5 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 3299 bytes 314563 (314.5 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
My computer's IP address is 192.168.100.221. Everyone on my network can connect to this computer via port 80 (HTTP) http://192.168.100.221:80
3.2 Nginx Management
Basic management commands:
- Start:
sudo systemctl start nginx
- Stop:
sudo systemctl stop nginx
- Restart:
sudo systemctl restart nginx
- Reload:
sudo systemctl reload nginx
- status:
sudo systemctl status nginx
References:
After setting up the internal network environment, we will move forward and expand the accessibility from outside the LAN by opening ports and deploying a domain name. This will make your website easily accessible from anywhere in the world.
Thank you for taking the time to read this article! This is my first post, where I’ve gathered information from various sources. I hope the steps provided will help you easily deploy a website on Ubuntu VPS in a local environment. If you have any questions, suggestions, or need further assistance, feel free to leave a comment. Your feedback is invaluable and will help me improve future content!
Part 2: Expanding Connectivity: Configuring Ports and Deploying a Public Domain (Expected: 12/5/2024)