Have you ever felt frustrated when uploading code to shared hosting was slow and limited? I was like that in the first years of my career. Until discovering the "free world" of virtual servers, everything changed completely. deploying a website to VPS using SSH helps you take full control of the server in your hands, optimizing performance down to every megabyte of RAM, it feels very "good". This article is all the real-life experience that Pham Hai has gathered so you can bring your project online quickly and professionally, without having to be a system superhero.
Why Do Professionals Choose to Deploy to VPS Using SSH & What Do You Need to Prepare?
Deployment via SSH offers maximum control, high performance, and infinite customization compared to conventional hosting. You only need to prepare a VPS, domain and basic command line knowledge to get started.
Forget shared hosting, this is why VPS is true love
VPS provides independent resources, allowing you to deeply intervene in the system with root user rights instead of being confined to pre-built control panels.
Using shared hosting is like staying in a shared apartment, you are not allowed to arbitrarily repair the house or install strange software. And VPS (Virtual Private Server) is your private home. You can freely install any web server like Nginx or Apache as you like to optimize the type of code you are writing. Operating directly through Terminal with Linux commands helps the process of processing, compressing files, and moving data many times faster than clicking and waiting on the slow cPanel interface. Furthermore, your RAM and CPU resources are independent, without fear of "neighbor's" websites affecting page loading speed.
Checklist of necessary equipment before leaving
Before getting started, you need to make sure you have rented a VPS, have an IP domain name, and set up secure connection tools.
Don't rush to type commands without preparing enough "ammunition". Missing this step will cause you to waste a lot of time. Here are the must-haves on your desk:
| Tools/Resources | Request details |
|---|---|
| VPS & Hệ điều hành | Recommended Ubuntu 20.04/22.04, CentOS or Debian. Minimum RAM 1GB. |
| Domain & IP | A domain (domain name) has configured DNS pointing Record A to the IP address of the VPS. |
| Bảo mật kết nối | Pair public key and private key to log in via SSH key securely. |
At Pham Hai, I always recommend that you create an SSH key from the beginning to log in instead of using the original password. This helps completely prevent brute-force attacks from hackers targeting port 22 of the server.
5-Step Process of Deploy Website to Ubuntu VPS 'Textbook Standard'
The standard process includes 5 steps: connecting the server, setting up the environment, pulling the code via Git, configuring the web server and securing with an SSL certificate. Below are the most detailed instructions for deploying a website to an Ubuntu VPS using SSH.
Step 1: Connect to SSH & "Clean House" for the New Server
The first step is to log in to the VPS, update system software packages and set up a basic firewall to protect the server from unauthorized access.
Cách deploy website lên VPS cho người mới luôn bắt đầu từ việc làm quen với màn hình đen chữ trắng. Mở Terminal (hoặc PuTTY nếu dùng Windows) lên và gõ lệnh: ssh root@dia_chi_ip_cua_ban. Lần đầu kết nối, hệ thống sẽ hỏi bạn có tin tưởng IP này không, hãy gõ "yes".
Sau khi vào được bên trong bằng quyền root user (hoặc user có quyền sudo), việc đầu tiên là chạy lệnh update thông qua package manager (như apt trên Ubuntu hoặc yum, dnf trên CentOS). Bạn gõ lệnh sau để cập nhật hệ thống:
sudo apt update && sudo apt upgrade -y
Kế tiếp, đừng quên bật Firewall (tường lửa). Với hệ điều hành Ubuntu, việc cấu hình UFW rất đơn giản. Bạn chỉ cần mở các cổng cần thiết: sudo ufw allow OpenSSH, sudo ufw allow 'Nginx Full', rồi kích hoạt bằng sudo ufw enable. Nếu bạn còn bỡ ngỡ với các lệnh này và muốn xây dựng nền tảng vững chắc, việc đọc thêm tài liệu Quản trị Linux server cơ bản cho developer là bước khởi đầu rất cần thiết.
Step 2: Install "The Power of Three" - Web Server, Database & Language
Depending on the source code, you will need to install Nginx or Apache, a database management system (MySQL/MariaDB), and a code execution environment (PHP, Node.js).
Server mới mua về trống trơn thì không thể chạy web được. Mình thường ưu tiên dùng Nginx vì nó xử lý các request đồng thời cực kỳ trâu bò và tốn ít RAM. Lệnh cài đặt rất ngắn gọn: sudo apt install nginx -y.
Tiếp theo là thiết lập Database. Bạn có thể chọn MySQL hoặc MariaDB. Sau khi cài đặt xong qua lệnh sudo apt install mysql-server, hãy nhớ chạy lệnh mysql_secure_installation. Bước này giúp bạn xóa các user rác, database test và đặt mật khẩu root thật khó đoán để bảo mật dữ liệu.
Cuối cùng, cài môi trường ngôn ngữ lập trình. Nếu dự án chạy PHP, bạn cài php-fpm và các extension liên quan. Nếu bạn chạy ứng dụng Javascript, hãy cài đặt Node.js thông qua NVM (Node Version Manager) để dễ dàng chuyển đổi các phiên bản khi cần thiết.
Step 3: Push Code to VPS - Use Git for Professionals!
Using Git to clone code from the repository (Github/Gitlab) to the VPS web directory is the safest, fastest and easiest method for version management.
Quên việc nén file zip rồi hì hục đẩy qua FTP đi, thời đại này chúng ta xài Git. Trên VPS, bạn dùng lệnh mkdir để tạo thư mục chứa code, ví dụ: sudo mkdir -p /var/www/mywebsite. Sau đó dùng lệnh cd để di chuyển vào thư mục vừa tạo: cd /var/www/mywebsite.
Tiếp theo, hãy liên kết server với Github của bạn. Chạy lệnh git clone [link_repo_cua_ban] . (nhớ có dấu chấm ở cuối để code bung thẳng ra thư mục hiện tại). Việc dùng Git mang lại lợi ích khổng lồ: sau này mỗi lần fix bug hay có tính năng mới, bạn chỉ cần SSH vào và gõ git pull là code mới tự động được cập nhật. Cực kỳ nhàn và không sợ copy đè nhầm file!
Step 4: Configure Nginx "Directions" for Domain
Create a virtual host file in Nginx to tell the server when the user types the domain name into the browser, which code directory to display the content from.
Đây là bước cấu hình Nginx khi deploy website lên VPS mang tính quyết định. Bạn cần tạo một file cấu hình mới trong thư mục /etc/nginx/sites-available/. Dùng trình soạn thảo nano: sudo nano /etc/nginx/sites-available/mywebsite.
Trong file này, bạn khai báo chỉ thị server_name chính là domain của bạn (ví dụ: phamhai.com), và root chỉ đường dẫn tuyệt đối tới thư mục code /var/www/mywebsite. Nếu bạn chạy app Node.js, bạn sẽ không dùng root mà cấu hình Nginx làm reverse proxy để đẩy traffic từ port 80 về cổng nội bộ (ví dụ 3000).
Sau khi lưu file, tạo symlink sang thư mục sites-enabled. Luôn nhớ kiểm tra lỗi cú pháp bằng lệnh sudo nginx -t. Nếu màn hình báo "syntax is ok", bạn khởi động lại dịch vụ bằng lệnh systemctl: sudo systemctl restart nginx.
Step 5: Install Free SSL With Let's Encrypt - The Web Must Be HTTPS!
Securing your website with an SSL certificate is a mandatory standard today. Certbot and Let's Encrypt tools help you fully automate this allocation process for free.
Installing SSL for your website on VPS using SSH is now as easy as pie. Google Chrome browser now sees an HTTP website and immediately displays a big "Not Secure" warning. Customers who see it will run away.
Bạn chỉ cần cài đặt Certbot - một công cụ tự động hóa tuyệt vời: sudo apt install certbot python3-certbot-nginx. Sau đó chạy lệnh sudo certbot --nginx -d domaincuaban.com -d www.domaincuaban.com. Certbot sẽ tự động xác minh tên miền, sinh chứng chỉ SSL, tự động sửa file cấu hình Nginx để ép luồng chạy qua HTTPS và thiết lập cronjob tự động gia hạn mỗi 90 ngày. Để hiểu sâu hơn về cơ chế mã hóa này, việc tìm hiểu SSL HTTPS là gì cách cài cho website sẽ cung cấp cho bạn cái nhìn tổng quan nhất.
Real Experience: Deploy Popular Types of Websites
Each framework or CMS will have its own "tricks" when posted to the production environment to ensure stability, loading speed and data security.
Deploy Laravel website: Pay attention to the .env file and directory permissions
When deploying the Laravel framework, you need to install packages via Composer, set up the correct environment file (.env) and give write permission to the storage/cache directory.
Để deploy Laravel lên VPS bằng SSH trơn tru, sau khi clone code về, việc đầu tiên là chạy lệnh composer install --optimize-autoloader --no-dev để cài đặt thư viện. Tiếp đó, copy file .env.example thành .env, tạo app key mới và điền thông tin database chính xác vào đó.
Lỗi kinh điển nhất của anh em làm Laravel là gặp màn hình trắng hoặc lỗi 500 do sai phân quyền. Bạn bắt buộc phải dùng lệnh chown để cấp quyền sở hữu thư mục cho Nginx: sudo chown -R www-data:www-data /var/www/laravel. Sau đó dùng lệnh chmod cấp quyền ghi cho các thư mục sinh file động: sudo chmod -R 775 storage bootstrap/cache. Nếu bạn hay bị rối ở các con số phân quyền này, bài viết Linux file permission chmod chown giải thích sẽ giúp bạn gỡ rối triệt để.
Deploy WordPress website: Configure wp-config.php and database
The process of putting WordPress on the server focuses on importing the database from localhost and tweaking the wp-config.php file to connect successfully.
Việc deploy WordPress lên VPS bằng SSH đòi hỏi sự cẩn thận ở khâu dữ liệu. Bạn phải export database từ máy local thành file .sql, đẩy lên server và import vào MySQL qua lệnh: mysql -u root -p database_name < file_data.sql.
Tiếp theo, mở file wp-config.php và sửa thông tin DB_NAME, DB_USER, DB_PASSWORD cho khớp với database mới tạo trên VPS. Một mẹo nhỏ từ kinh nghiệm của mình là bạn nên tăng giới hạn upload file của PHP (upload_max_filesize và post_max_size trong file php.ini) để không bị văng lỗi khi tải các theme hoặc plugin nặng. Với những ai muốn một lộ trình hoàn chỉnh, hướng dẫn Cấu hình VPS Ubuntu chạy WordPress từ đầu là tài liệu bạn không nên bỏ qua.
Deploy website Node.js/Next.js: Don't forget PM2!
Applications that run with Javascript need a process manager like PM2 to keep the application running in the background and automatically restart when the server crashes.
Để deploy website Node.js lên VPS bằng SSH (tương tự với các framework như Next.js), sau khi kéo code, bạn dùng npm hoặc yarn để cài dependencies: npm install. Nếu là dự án Next.js, bạn cần chạy thêm lệnh build: npm run build.
Sai lầm của người mới là gõ lệnh npm start thẳng trên Terminal. Khi bạn tắt cửa sổ Terminal đó đi, website sẽ sập ngay lập tức. Giải pháp chuyên nghiệp là dùng PM2. Cài đặt nó bằng lệnh npm install -g pm2, sau đó khởi chạy app: pm2 start npm --name "my-app" -- start. Cuối cùng, gõ pm2 save và pm2 startup. Ứng dụng của bạn giờ đây sẽ sống dai như đỉa, tự động bật lại kể cả khi VPS bị khởi động lại.
Deploy static website (HTML/CSS/JS): Rsync is enough!
For static websites (landing pages, portfolios) without a backend, synchronizing files from a personal computer to a VPS using the rsync command is the fastest and safest option.
The steps to deploy a static website to VPS using SSH are super simple, you don't need to install Git or run messy build commands on the server that waste resources.
Bạn chỉ cần build web ở máy tính local. Sau đó mở Terminal nội bộ lên và dùng lệnh rsync: rsync -avz ./dist/ root@ip_vps:/var/www/html/. Lệnh này sẽ nén dữ liệu và đồng bộ chính xác thư mục code tĩnh của bạn thẳng lên thư mục web mặc định của Nginx trên VPS. Tốc độ cực nhanh và chỉ cập nhật những file có sự thay đổi!
Improve Skills: Automation & Handling Common Errors
Automating the CI/CD process and mastering how to read error logs will help you manage the server leisurely, save time and handle problems more professionally.
Automated deployment with Github Actions (CI/CD) - Set & Forget!
Setting up CI/CD helps code automatically be built and pushed to the server every time you push a new commit to the main branch, completely eliminating manual operations.
Cứ mỗi lần sửa một dòng code CSS lại phải SSH vào VPS gõ git pull thì quả thật rất tù túng. Tại Phạm Hải, tụi mình luôn thiết lập quy trình deploy website tự động lên VPS cho mọi dự án của khách hàng.
Bằng cách sử dụng Github Actions (một dạng CI/CD pipeline), bạn chỉ cần viết một file .yml cấu hình. Mỗi khi có code mới được push lên nhánh main, Github sẽ tự động kích hoạt một container nhỏ (thường dùng chung với Docker), kết nối vào VPS của bạn qua SSH key, kéo code mới, tự động chạy npm install và restart lại PM2. Bạn chỉ việc gõ git push, sau đó nhâm nhi ly cafe chờ web tự động cập nhật. Cảm giác đó thực sự rất "pro"!
Catching and curing errors: 502 Bad Gateway, Permission Denied and friends
Checking Nginx and application log files is the golden key to quickly diagnose and resolve common errors when bringing the web to the real environment.
The skill of troubleshooting errors when deploying a website to a VPS using SSH is what distinguishes between apprentices and skilled workers. See the 502 Bad Gateway error screen? Don't panic. This error is usually because Nginx (acting as a proxy) cannot connect to the Node.js or PHP-FPM app behind it. Check to see if your app is running on the correct port and if PM2 is reporting a recurring crash error.
Nếu gặp lỗi 403 Forbidden? 99% nguyên nhân là do sai phân quyền thư mục (chưa cấp quyền đọc cho user www-data). Lệnh cứu mạng của bạn trong mọi tình huống luôn là xem log: sudo tail -f /var/log/nginx/error.log. Việc kiên nhẫn đọc từng dòng log sẽ chỉ điểm chính xác server đang "đau" ở file nào, dòng số mấy để bạn bốc thuốc chữa bệnh kịp thời.
Deploying via SSH to VPS sounds "dangerous" and dry with all the code, but it is actually a very logical and sequential process. Mastering this guide to deploy website to VPS using SSH will not only help you free your project from the crampedness of shared hosting, have full control over the system, but also open a new door to take your developer skills to a new level. It may be a bit difficult at first to get used to Terminal, but the feeling of personally managing and optimizing your "brainchild" to run smoothly on a private server is really worth it.
Are you having trouble configuring Nginx, or having difficulty generating SSH keys? Or do you have a better "optimization trick" you want to share? Please leave a comment below, I and the community will discuss and troubleshoot for you!
Lưu ý: Các thông tin trong bài viết này chỉ mang tính chất tham khảo. Để có lời khuyên tốt nhất, vui lòng liên hệ trực tiếp với chúng tôi để được tư vấn cụ thể dựa trên nhu cầu thực tế của bạn.