hello and welcome to my coding tutorial to build and deploy my nextjs e-commerce website on Linux Ubuntu operating system using Hostinger VPS server.
In this tutorial we will learn how to:
- buy and configure VPS server and website domain on hostinger
- clone my main amazona project from GitHub
- connect to the hostinger Linux server
- build and deploy automatically applications on every git push
- and at the end I teach you how to encrypt the website using let's encrypt SSL certificate
Let us start by buying hostinger VPS and domain. first of all I'm going to thank hostinger for sponsoring this video. I already used their service for hosting my website and web applications like codingwithbasir.com. In this tutorial we are going to build and deploy a full-functional ecommerce website on hostinger VSP server.
Lets see benefits of using VPS services with Hostinger
- High performance
- KVM virtualization
- Full root access
- Scalability
- Enhanced security
- One-click applications
- VPS AI assistant
- Free automatic backups and real-time snapshots
- select vps kvm 2
- apply coupon code
- make payment
- buy a domain
- add A, AA record for vps ip address
I suppose the vps ip address is 5.182.18.65 and domain name is nextjs-ecommerce.com. you need to change them based on your vps ip address and domain name.
-
connect to vps server
ssh root@5.182.18.65
-
install node
apt-get update curl -fsSL https://deb.nodesource.com/setup_20.x -o nodesource_setup.sh sudo -E bash nodesource_setup.sh sudo apt-get install -y nodejs node -v
-
create git repo
mkdir -p ~/apps/nextjs-ecommerce/repo mkdir -p ~/apps/nextjs-ecommerce/dest cd ~/apps/nextjs-ecommerce/repo git --bare init
-
automate deployment on every git push
nano hooks/post-receive
#!/bin/bash -l echo 'post-receive: Triggered.' cd ~/apps/nextjs-ecommerce/dest/ git --git-dir=/root/apps/nextjs-ecommerce/repo --work-tree=/root/apps/nextjs-ecommerce/dest/ checkout main -f pnpm install pnpm run build pm2 restart nextjs-ecommerce
-
make post-receive executable
chmod ug+x hooks/post-receive
-
npm install -g pnpm
-
create postgres database
- create database at https://vercel.com/docs/storage/vercel-postgres
- copy POSTGRES_URL
cd ../dest nano .envNEXT_PUBLIC_SERVER_URL=http://nextjs-ecommerce.com NEXT_PUBLIC_APP_NAME=Next ECommerce App NEXT_PUBLIC_APP_DESCRIPTION=An ECommerce App built with Next.js, Postgres, Shadcn # Database (vercel or neon db url) POSTGRES_URL=your_postgres_url # NextAuth ($ npx auth secret) AUTH_SECRET=fFLDeFDO7rzsZ/TdxINaG2VUogiPEZX/LgTl2FAoReY= AUTH_TRUST_HOST=true # API Keys RESEND_API_KEY=123 -
[On Dev Machine] clone repo
git clone https://github.com/basir/next-pg-shadcn-ecommerce cd next-pg-shadcn-ecommerce git remote add hostinger ssh://root@5.182.18.65/root/apps/nextjs-ecommerce/repo/ -
[On Dev Machine] add dummy changes, commit and push
git add . && git commit -m "commit message" && git push hostinger
-
config pm2
sudo npm install -g pm2
cd /root/apps/nextjs-ecommerce/dest/
npx drizzle-kit push
npx tsx ./db/seed
sudo pm2 start "npm run start" --name "nextjs-ecommerce"
pm2 save
pm2 startup-
[On Dev Machine] add dummy changes, commit and push
git add . && git commit -m "commit message" && git push hostinger
-
config apache
sudo apt install apache2 sudo a2enmod proxy proxy_http rewrire headers expires sudo nano /etc/apache2/sites-available/nextjs-ecommerce.com.conf
<VirtualHost *:80> ServerName nextjs-ecommerce.com ServerAlias www.nextjs-ecommerce.com ProxyRequests Off ProxyPreserveHost On ProxyVia Full <Proxy *> Require all granted </Proxy> ProxyPass / http://127.0.0.1:3000/ ProxyPassReverse / http://127.0.0.1:3000/ </VirtualHost>
sudo a2dissite 000-default.conf sudo a2ensite nextjs-ecommerce.com.conf sudo service apache2 restart
-
secure website
sudo apt install certbot python3-certbot-apache sudo certbot -d nextjs-ecommerce.com -d www.nextjs-ecommerce.com -m basir.jafarzadeh@gmail.com --apache --agree-tos --no-eff-email --redirect sudo certbot renew --dry-run
-
update http to https in .env file
cd /root/apps/nextjs-ecommerce/dest/ nano .envNEXT_PUBLIC_SERVER_URL=https://nextjs-ecommerce.com