Introduction
Deploying a website on a VPS gives developers greater control over their hosting environment, application configuration, and deployment process.
If you are developing a Next.js application using Visual Studio Code, you can use GitHub to store your code and GitHub Actions to automate the deployment process.
In this tutorial, we will deploy a Next.js project named vouchiqo-new on a Hostinger VPS and configure automatic deployment using GitHub Actions.
The tutorial covers the complete process, starting from connecting to the VPS and installing Node.js to configuring PM2, Nginx, domain DNS, SSL certificates, and automatic deployment.
After completing this setup, your development workflow will become simple:
- Write or update code in VS Code.
- Commit your changes using Git.
- Push the code to GitHub.
- GitHub Actions automatically connects to your VPS.
- The VPS downloads the latest code and builds the application.
- PM2 restarts the application.
- Your updated website becomes available online.
You will no longer need to manually upload project files or restart the server after every code update.
1. What Is Hostinger VPS Deployment?
A Virtual Private Server, commonly known as a VPS, is a virtual server that provides dedicated resources within a physical server.
Unlike traditional shared hosting, a VPS allows you to manage your operating system, install software, configure services, and run applications according to your requirements.
For a Next.js application, a VPS can run the Node.js production server while Nginx handles incoming HTTP and HTTPS requests.
Next.js supports deployment as a Node.js server, Docker container, or static export. In this tutorial, we will use the Node.js server approach because it supports the full Next.js feature set.
Official documentation: https://nextjs.org/docs/app/getting-started/deploying
Technologies used in this tutorial
| Technology | Purpose |
| Hostinger VPS | Cloud server for hosting the application |
| Ubuntu | Server operating system |
| GitHub | Source code repository |
| GitHub Actions | Automatic deployment workflow |
| Node.js | JavaScript runtime |
| npm | Package manager |
| Next.js | Web application framework |
| PM2 | Application process manager |
| Nginx | Reverse proxy and web server |
| Let's Encrypt | Free SSL certificates |
| Certbot | SSL certificate installation and renewal |
2. VPS Details and Project Configuration
The following configuration is based on the Hostinger VPS shown in the deployment dashboard.
| Configuration | Value |
| VPS hostname | srv2xxxxxx.hstgr.cloud |
| VPS IP address | YOUR_VPS_IP |
| VPS plan | KVM 1 |
| Operating system | Ubuntu |
| Project name | vouchiqo-new |
| Project directory | /var/www/vouchiqo-new |
| Application port | 3001 |
| Deployment user | deploy |
The actual VPS IP should be entered in your terminal and GitHub secrets where required. It is represented by YOUR_VPS_IP throughout this article to avoid unnecessarily publishing infrastructure details.
We will use port 3001 for Vouchiqo so it can run alongside another application that may already be using port 3000.
3. Understanding the Automatic Deployment Architecture
Before configuring the server, understand how the deployment system works.
The complete workflow is:
VS Code → Git → GitHub → GitHub Actions → SSH → Hostinger VPS → PM2 → Nginx → Website
VS Code
You write and modify your application code locally.
Git
Git tracks changes to your source code and allows you to create commits.
GitHub
GitHub stores your repository and acts as the central source of truth for your application.
GitHub Actions
GitHub Actions detects changes pushed to the main branch and starts the deployment workflow.
SSH
SSH provides a secure connection between GitHub Actions and your VPS.
Node.js and npm
Node.js runs your Next.js application, while npm installs project dependencies and executes build commands.
PM2
PM2 manages the application process and helps keep it running in the background.
Nginx
Nginx receives requests from your domain and forwards them to the Next.js server running on port 3001.
4. Prepare Your Next.js Project
Open your vouchiqo-new project in Visual Studio Code.
Open the terminal:
Terminal → New Terminal
Install the dependencies:
npm install
Build your application:
npm run build
If the build completes successfully, your project is ready for production deployment.
Check package.json
Open your package.json file and verify that it contains the required scripts.
{
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
}
}
Your project may contain additional scripts. Keep them as they are.
The important scripts are:
- build creates the production build.
- start starts the production server.
Check Node.js version
Run:
node -v
Note the version.
For a new Next.js project, use a supported Node.js LTS version compatible with your installed Next.js version. Next.js documentation currently lists Node.js 20.9 as the minimum for the documented installation requirements.
Documentation: https://nextjs.org/docs/app/getting-started/installation
5. Upload Your Project to GitHub
GitHub will store your source code and trigger the deployment workflow.
Open GitHub and create a new repository.
Repository name:
vouchiqo-new
Choose Private if you do not want your source code to be publicly accessible.
Configure .gitignore
Before uploading your project, open .gitignore.
Make sure it contains:
node_modules .next .env .env.local .env.production .env.production.local *.log
This prevents local dependencies, build files, and environment secrets from being uploaded.
Initialize Git
If your project is not already connected to GitHub, run:
git init
Add your files:
git add .
Create the initial commit:
git commit -m "Initial Vouchiqo project"
Set the main branch:
git branch -M main
Connect your GitHub repository:
git remote add origin [email protected]:YOUR_GITHUB_USERNAME/vouchiqo-new.git
Replace YOUR_GITHUB_USERNAME with your actual GitHub username.
Push your project:
git push -u origin main
Your project is now stored on GitHub.
If Git is already initialized and connected to GitHub, do not repeat the initialization or remote setup commands.
6. Connect to Your Hostinger VPS Using SSH
Open PowerShell on your Windows computer.
Connect to your VPS:
ssh root@YOUR_VPS_IP
Replace YOUR_VPS_IP with the IP address shown in your Hostinger VPS dashboard.
Enter your root password when requested.
The password will not appear while typing. This is normal behavior.
You can also use the Hostinger browser terminal if you are unable to connect through SSH.
Once connected, you will be working directly on your Ubuntu server.
Update Ubuntu
Run:
apt update
Then:
apt upgrade -y
These commands update the package information and install available system updates.
Install required packages
Run:
apt install -y git curl nginx build-essential
These packages provide:
- Git for downloading your project
- Curl for downloading software and testing services
- Nginx for serving your website
- Build tools required by some Node.js packages
If Nginx is already installed and serving another website, do not reinstall or remove its existing configuration.
7. Create a Dedicated Deployment User
For security, create a separate Linux user named deploy.
This user will manage the application and will be used by GitHub Actions.
While connected as root, run:
adduser deploy
Set a password when prompted.
Add the user to the sudo group:
usermod -aG sudo deploy
Create the project directory:
mkdir -p /var/www/vouchiqo-new
Give ownership to the deployment user:
chown -R deploy:deploy /var/www/vouchiqo-new
Switch to the deployment user:
su - deploy
From this point, use the deploy user for application setup.
Do not run the production application as root.
8. Install Node.js Using NVM
Node.js is required to run your Next.js application.
NVM allows you to install and manage Node.js versions without requiring a system-wide Node.js installation.
Run the following command as the deploy user:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
Reload your terminal configuration:
source ~/.bashrc
Install Node.js LTS:
nvm install --lts
Use the installed version:
nvm use --lts
Check the installation:
node -v npm -v
Make sure the Node.js version is compatible with your local development environment.
9. Install PM2
PM2 is a process manager for Node.js applications.
It allows you to run your application in the background, monitor logs, restart processes, and restore applications after a VPS reboot.
Install PM2:
npm install -g pm2
Check the installation:
pm2 -v
PM2 is now ready to manage your Next.js application.
10. Configure VPS Access to GitHub
Your VPS needs permission to download your private GitHub repository.
We will create a repository-specific SSH deploy key.
Run as the deploy user:
ssh-keygen -t ed25519 -C "vouchiqo-vps" -f ~/.ssh/vouchiqo_github -N ""
This creates two files:
~/.ssh/vouchiqo_github ~/.ssh/vouchiqo_github.pub
The file without .pub is the private key.
The file with .pub is the public key.
Copy the public key
Run:
cat ~/.ssh/vouchiqo_github.pub
Copy the complete public key.
Add the key to GitHub
Open your vouchiqo-new repository.
Go to:
Settings → Deploy keys → Add deploy key
Enter:
Title:
Vouchiqo VPS
Paste your public key into the Key field.
Keep Allow write access unchecked.
Click Add deploy key.
This allows your VPS to read the repository without giving it permission to modify your GitHub code.
Configure SSH
On the VPS, run:
nano ~/.ssh/config
Paste:
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/vouchiqo_github
IdentitiesOnly yes
Save the file.
Set permissions:
chmod 600 ~/.ssh/config
Add GitHub to the known hosts file using a verified GitHub SSH host key:
ssh-keyscan github.com >> ~/.ssh/known_hosts
For stronger security, verify the SSH host key fingerprint against GitHub's official documentation before trusting it.
Test the connection:
ssh -T [email protected]
If successful, GitHub will confirm that you have authenticated.
11. Clone Your Project on the VPS
Go to the project directory:
cd /var/www/vouchiqo-new
Clone the repository:
git clone [email protected]:YOUR_GITHUB_USERNAME/vouchiqo-new.git .
Replace YOUR_GITHUB_USERNAME with your GitHub username.
The dot at the end means Git should clone the repository into the current directory.
Check your files:
ls -la
You should see your project files, including:
package.json package-lock.json public app next.config.js
Your actual project structure may be different.
12. Configure Production Environment Variables
Your application may require environment variables for:
- Database connections
- Authentication
- Payment gateways
- API keys
- Email services
- Backend URLs
Create the production environment file:
cd /var/www/vouchiqo-new nano .env.local
Add the actual environment variables required by your application.
Example:
DATABASE_URL=YOUR_PRODUCTION_DATABASE_URL NEXTAUTH_SECRET=YOUR_AUTHENTICATION_SECRET NEXTAUTH_URL=https://YOUR_DOMAIN
These are placeholders. Do not use them as real production values.
Save the file.
Set permissions:
chmod 600 .env.local
Never upload .env.local to GitHub.
Important Next.js environment variable rule
Variables beginning with NEXT_PUBLIC_ are usually embedded into the browser-side JavaScript during next build.
If you change a NEXT_PUBLIC_ variable, rebuild the application.
Do not place private credentials in NEXT_PUBLIC_ variables because they may become visible to website visitors.
If your project needs environment variables during the build process, make sure those variables are available before running npm run build.
Official documentation: https://nextjs.org/docs/app/guides/self-hosting
13. Install Dependencies and Build the Application
Go to your project directory:
cd /var/www/vouchiqo-new
Install dependencies:
npm ci
The npm ci command installs dependencies using your package-lock.json file.
If your repository does not contain package-lock.json, run npm install and commit the generated lock file to GitHub.
Build the application:
npm run build
Wait for the build to complete.
If the build fails, check the error message before continuing.
Common causes include:
- Missing environment variables
- Incorrect Node.js version
- Missing dependencies
- TypeScript errors
- Invalid imports
- Database connection errors during build
Do not proceed until the production build is successful.
14. Configure PM2 for Next.js
We will configure PM2 to run the application on port 3001.
Create the PM2 configuration file:
cd /var/www/vouchiqo-new nano ecosystem.config.cjs
Paste:
module.exports = {
apps: [
{
name: "vouchiqo-new",
cwd: "/var/www/vouchiqo-new",
script: "npm",
args: "start",
env: {
NODE_ENV: "production",
PORT: 3001
}
}
]
};
Save the file.
Start the application:
pm2 start ecosystem.config.cjs
Check the status:
pm2 status
Your application should show:
vouchiqo-new online
Test the application
Run:
curl -I http://127.0.0.1:3001
You should receive an HTTP response.
If the application is not responding, check:
pm2 logs vouchiqo-new
Configure PM2 startup
Run:
pm2 startup
PM2 will print a command that must be executed with sudo.
Copy the command exactly as printed and execute it.
Then run:
pm2 save
This allows PM2 to restore your application after a VPS reboot.
15. Configure Nginx Reverse Proxy
Nginx will forward traffic from your domain to your Next.js application.
First, connect as root if you are currently logged in as the deployment user.
Create a new Nginx configuration:
nano /etc/nginx/sites-available/vouchiqo-new
Paste:
server {
listen 80;
server_name YOUR_DOMAIN www.YOUR_DOMAIN;
location / {
proxy_pass http://127.0.0.1:3001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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;
}
}
Replace YOUR_DOMAIN with your actual domain.
If both vouchiqo.com and vouchiqo.in should point to the same application, configure both domain names in the server_name directive after confirming that both DNS records point to your VPS.
Save the file.
Enable the configuration:
ln -s /etc/nginx/sites-available/vouchiqo-new /etc/nginx/sites-enabled/
Check the Nginx configuration:
nginx -t
If the configuration is valid, reload Nginx:
systemctl reload nginx
Do not remove or overwrite your existing Himtrek Nginx configuration.
Each website should have its own configuration file.
Nginx acts as a reverse proxy, forwarding incoming requests to the application server. This is the recommended approach for self-hosted Next.js applications.
Official documentation: https://nextjs.org/docs/app/guides/self-hosting
16. Configure Domain DNS
Open your domain provider's DNS settings.
For your primary domain, create:
| Type | Name | Value |
| A | @ | YOUR_VPS_IP |
| A | www | YOUR_VPS_IP |
Replace YOUR_VPS_IP with the actual VPS IP address.
If your domain already points to this VPS, you may not need to change anything.
Wait for DNS propagation.
You can check your domain using:
nslookup YOUR_DOMAIN
The domain should resolve to your VPS IP.
Important: If your domain has email services, do not delete existing MX, TXT, SPF, DKIM, or DMARC records. These records may be required for email delivery.
Once DNS is configured, open:
http://YOUR_DOMAIN
Your website should load.
17. Configure HTTPS Using Let's Encrypt
HTTPS protects communication between your website and visitors.
Install Certbot:
apt install -y certbot python3-certbot-nginx
Run:
certbot --nginx -d YOUR_DOMAIN -d www.YOUR_DOMAIN
Replace YOUR_DOMAIN with your actual domain.
Follow the instructions.
Choose the option to redirect HTTP traffic to HTTPS.
After the certificate is installed, open:
https://YOUR_DOMAIN
Your website should now use HTTPS.
Test certificate renewal:
certbot renew --dry-run
Certbot supports automated certificate renewal, which helps keep your HTTPS certificate valid.
Official documentation: https://certbot.eff.org/
18. Create the GitHub Actions SSH Key
GitHub Actions needs permission to connect to your VPS.
We will create a separate SSH key for this purpose.
Open PowerShell on your Windows computer.
Run:
ssh-keygen -t ed25519 -C "github-actions-vouchiqo" -f "$env:USERPROFILE\.ssh\vouchiqo_actions"
When asked for a passphrase, press Enter twice to leave it empty.
This creates:
vouchiqo_actions vouchiqo_actions.pub
The private key is the file without .pub.
The public key is the file with .pub.
Add the public key to your VPS
Display the public key:
Get-Content "$env:USERPROFILE\.ssh\vouchiqo_actions.pub"
Copy the entire output.
Connect to your VPS as the deployment user:
ssh deploy@YOUR_VPS_IP
Create the SSH directory:
mkdir -p ~/.ssh chmod 700 ~/.ssh
Open the authorized keys file:
nano ~/.ssh/authorized_keys
Paste the public key on a new line.
Save the file.
Set permissions:
chmod 600 ~/.ssh/authorized_keys
GitHub Actions can now connect to your VPS using this key.
19. Configure GitHub Actions Secrets
Open your GitHub repository:
vouchiqo-new
Go to:
Settings → Secrets and variables → Actions
Click New repository secret.
Create the following secrets.
Secret 1: VPS_HOST
Value:
YOUR_VPS_IP
This is the IP address of your Hostinger VPS.
Secret 2: VPS_USER
Value:
deploy
This is the Linux deployment username.
Secret 3: VPS_SSH_KEY
Open the private key file:
vouchiqo_actions
Copy its complete contents, including the BEGIN and END lines.
Paste the private key into the VPS_SSH_KEY secret.
Never paste the .pub public key into this secret.
GitHub Actions secrets allow sensitive information to be stored securely and referenced by workflows without placing the actual values directly into your repository.
Official documentation: https://docs.github.com/en/actions/how-tos/write-workflows/choose-what-workflows-do/use-secrets
20. Create the Automatic Deployment Workflow
Open your project in VS Code.
Create the following directory structure:
vouchiqo-new/ │ ├── .github/ │ └── workflows/ │ └── deploy.yml │ ├── app/ ├── public/ ├── package.json └── ...
Create the file:
.github/workflows/deploy.yml
Paste the following workflow:
name: Deploy Vouchiqo to Hostinger VPS
on:
push:
branches:
- main
workflow_dispatch:
permissions:
contents: read
concurrency:
group: vouchiqo-production
cancel-in-progress: false
jobs:
deploy:
name: Deploy to VPS
runs-on: ubuntu-latest
steps:
- name: Connect and deploy
uses: appleboy/[email protected]
with:
host: ${{ secrets.VPS_HOST }}
username: ${{ secrets.VPS_USER }}
key: ${{ secrets.VPS_SSH_KEY }}
port: 22
command_timeout: 20m
script: |
set -e
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
cd /var/www/vouchiqo-new
echo "Downloading latest code"
git fetch origin main
git reset --hard origin/main
echo "Installing dependencies"
npm ci
echo "Building application"
npm run build
echo "Restarting application"
pm2 startOrReload ecosystem.config.cjs --update-env
pm2 save
echo "Checking application"
curl -fsS http://127.0.0.1:3001 > /dev/null
echo "Deployment completed successfully"
This workflow automatically deploys your application whenever code is pushed to the main branch.
The workflow performs the following tasks:
- Connects to the VPS using SSH.
- Opens the project directory.
- Downloads the latest code.
- Installs dependencies.
- Builds the Next.js application.
- Starts or reloads the PM2 application.
- Saves the PM2 process list.
- Checks whether the application is responding.
The workflow also includes workflow_dispatch, which allows you to manually trigger a deployment from GitHub Actions.
GitHub supports the push event for triggering workflows when commits are pushed to a branch.
Official documentation: https://docs.github.com/en/actions/reference/workflows-and-actions/events-that-trigger-workflows
21. Push the Deployment Workflow to GitHub
Open your VS Code terminal.
Run:
git add .
Commit your changes:
git commit -m "Configure automatic deployment"
Push to GitHub:
git push origin main
Now open your GitHub repository.
Click:
Actions
You should see:
Deploy Vouchiqo to Hostinger VPS
Click the workflow to view the deployment logs.
If everything is configured correctly, the workflow will complete successfully.
Your website is now connected to GitHub automatic deployment.
22. Test Automatic Deployment
Open your Vouchiqo project in VS Code.
Make a small change to your website.
For example, change a heading:
<h1>Welcome to Vouchiqo</h1>
To:
<h1>Welcome to Vouchiqo New</h1>
Save the file.
Run:
git add . git commit -m "Test automatic deployment" git push origin main
GitHub Actions will automatically start the deployment workflow.
Wait until the workflow finishes.
Refresh your website.
If the new heading appears, automatic deployment is working correctly.
23. Daily Development Workflow
After completing the setup, your daily workflow becomes simple.
Whenever you make a change to your website:
git add . git commit -m "Update website" git push origin main
GitHub Actions will automatically deploy the changes.
You no longer need to:
- Upload files manually
- Connect to the VPS for every update
- Run npm install manually
- Build the application manually
- Restart PM2 manually
Your deployment process is now automated.
24. Troubleshooting Common Errors
Permission denied (publickey)
Check whether the GitHub Actions public key is present in:
/home/deploy/.ssh/authorized_keys
Check whether the GitHub secret VPS_SSH_KEY contains the matching private key.
npm ci failed
Make sure your GitHub repository contains:
package-lock.json
If it does not exist, run:
npm install
Commit the generated package-lock.json file and push it to GitHub.
npm run build failed
Connect to your VPS and run:
cd /var/www/vouchiqo-new npm run build
Check the error message.
Make sure all required environment variables are configured in .env.local.
Application is not running
Check PM2:
pm2 status
View application logs:
pm2 logs vouchiqo-new
Restart the application:
pm2 restart vouchiqo-new
Website is not opening
Check Nginx:
nginx -t
Check the service:
systemctl status nginx
Check whether your application is responding:
curl http://127.0.0.1:3001
Port 3001 is already in use
Check the port:
ss -ltnp | grep 3001
If another application is using port 3001, choose another port and update the PM2 configuration and Nginx configuration.
25. Security Best Practices for VPS Deployment
For a production website, follow these security practices:
- Never share your root password.
- Never upload private SSH keys to GitHub.
- Never upload .env.local to GitHub.
- Use a separate deployment user instead of root.
- Use private GitHub repositories for private projects.
- Keep Ubuntu and Node.js updated.
- Configure firewall rules carefully.
- Keep your existing website configurations intact.
- Configure regular VPS backups.
- Use HTTPS for your production website.
Configure UFW firewall
Before enabling UFW, make sure SSH is allowed:
ufw allow OpenSSH
Allow HTTP:
ufw allow 80/tcp
Allow HTTPS:
ufw allow 443/tcp
Check:
ufw status
If UFW is not enabled, enable it only after confirming SSH access is allowed:
ufw enable
Do not enable the firewall without checking the SSH rule, or you may lock yourself out of your VPS.
26. Frequently Asked Questions
Can I automatically deploy a Next.js website on Hostinger VPS?
Yes. You can use GitHub Actions to connect to your Hostinger VPS through SSH and automatically deploy your Next.js application whenever you push code to GitHub.
Do I need to upload my website files manually after deployment?
No. Once GitHub Actions is configured, your deployment workflow downloads the latest code from GitHub and builds your application automatically.
Can I host multiple Next.js websites on one Hostinger VPS?
Yes. You can host multiple applications on one VPS by using separate project directories, different internal application ports, individual PM2 processes, and separate Nginx configurations.
Why use PM2 with Next.js?
PM2 manages the Node.js application process, keeps it running in the background, provides logs, and can restore applications after a VPS reboot.
Why use Nginx with Next.js?
Nginx acts as a reverse proxy. It receives requests from your domain and forwards them to your Next.js application running on an internal port.
Is GitHub Actions automatic deployment free?
GitHub Actions provides usage allowances depending on the repository and account plan. Public repositories and private repositories have different usage limits. Check the current GitHub Actions pricing and usage limits for your account.
Can I deploy without a domain?
Yes. You can test your application using the VPS IP address and port, but a production website should use a domain with HTTPS. When Nginx is configured, the application can be accessed through the domain.
27. Final Deployment Checklist
Before considering your deployment complete, verify the following:
- Next.js project builds successfully
- GitHub repository is connected
- VPS has Node.js and npm installed
- Deployment user is created
- VPS can clone the private GitHub repository
- Production environment variables are configured
- Application runs on port 3001
- PM2 is managing the application
- PM2 startup is configured
- Nginx configuration is enabled
- Domain DNS points to the VPS
- HTTPS certificate is installed
- GitHub Actions secrets are configured
- Deployment workflow is pushed to GitHub
- Automatic deployment succeeds
- Website updates after pushing new code
Conclusion
Deploying a Next.js application on a Hostinger VPS with GitHub Actions is an effective way to automate your website deployment workflow.
By combining GitHub, GitHub Actions, Node.js, PM2, and Nginx, you can create a deployment system that automatically builds and updates your website whenever you push new code.
This approach reduces manual work, improves consistency, and allows you to focus on developing your application rather than managing repetitive deployment tasks.
Your final workflow is:
Code → Commit → Push → Automatic Build → Automatic Deployment → Live Website
Once configured, your Vouchiqo website will be connected to an automatic deployment pipeline, making future updates faster and easier.
Official Documentation and References
- Next.js Deployment: https://nextjs.org/docs/app/getting-started/deploying
- Next.js Self-Hosting: https://nextjs.org/docs/app/guides/self-hosting
- GitHub Actions Secrets: https://docs.github.com/en/actions/how-tos/write-workflows/choose-what-workflows-do/use-secrets
- GitHub Actions Workflow Events: https://docs.github.com/en/actions/reference/workflows-and-actions/events-that-trigger-workflows
- Nginx: https://nginx.org/en/docs/
- Certbot: https://certbot.eff.org/



