Learn step-by-step how to setup Git on Ubuntu 24.04, create a secure Git user, set up a bare repository, and configure Nginx for automatic website deployment.
We understand the need for simple, reliable, and automated code deployment. Setting up Git on an Ubuntu Server combined with Nginx for serving websites lets us deploy updates securely and efficiently. This guide walks through all necessary steps to get Git-based deployment running from scratch.
Prerequisites
- A Ubuntu 24.04 installed dedicated server or KVM VPS.
- A root user or normal user with administrative privileges.
- Basic Linux command knowledge.
How to setup Git on Ubuntu 24.04
1. Install Git and Nginx on Ubuntu Server
First, update package info and install Git and Nginx:
sudo apt update
sudo apt install git nginx -y
Check Git and Nginx status:
git --version
sudo systemctl status nginx
2. Create a Dedicated git User for Deployment
For security and simplicity, create a new user named git to manage repositories and deployments:
sudo adduser --disabled-password --gecos "" git
This creates the user without a password, disabling direct login. Access via SSH key only (recommended).
3. Setup the Deployment Directory /var/www/project
Create the directory where the website files will live:
sudo mkdir -p /var/www/project
Set ownership so git user can write here:
sudo chown -R git:www-data /var/www/project
sudo chmod -R 775 /var/www/project
4. Setup SSH Access for the git User
Switch to the git user:
sudo su - git
Create .ssh
directory and authorized_keys
:
mkdir ~/.ssh
chmod 700 ~/.ssh
nano ~/.ssh/authorized_keys
Paste the public SSH keys of all developers allowed to push code, save and exit.
Set permissions:
chmod 600 ~/.ssh/authorized_keys
5. Create a Bare Git Repository for the Project
Bare repositories store Git data but no working files — perfect for deployment triggers.
As the git user, create the repo:
mkdir -p ~/repos/project.git
cd ~/repos/project.git
git init --bare
6. Configure the post-receive Hook for Auto Deployment
Create the hook script inside the bare repo:
cd ~/repos/project.git/hooks
nano post-receive
Paste the following script to checkout code into /var/www/project:
#!/bin/bash
GIT_WORK_TREE=/var/www/project git checkout -f
Save and exit, then make it executable:
chmod +x post-receive
Finally, exit git
user and become root
user.
exit
7. Configure Nginx to Serve Your Project
Create an Nginx site config file:
sudo nano /etc/nginx/sites-available/project
Example content:
server {
listen 80;
server_name your_domain_or_ip;
root /var/www/project;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
Enable the site:
sudo ln -s /etc/nginx/sites-available/project /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
8. Test the Deployment
On your local machine:
Clone the bare repo (replace git@server_ip with your server IP/domain
):
git clone git@server_ip:repos/project.git
cd project
Add a test file and push:
echo "Hello World" > index.html
git add index.html
git commit -m "Initial commit"
git push origin master
- Check on the server
/var/www/project/index.html
. It should be updated with the pushed content. - Visit your server IP or domain in a browser. You should see "
Hello World
."
Best Practices Summary
- Use SSH keys for secure Git access.
- Keep git user permissions limited to deployment tasks.
- Always back up your bare repos and deployed code.
- Keep your system updated (sudo apt update && sudo apt upgrade).
- Monitor Nginx logs (
/var/log/nginx/
) for any serving issues.
By following these steps, we ensure a secure, efficient, and automated code deployment setup on Ubuntu Server with Git and Nginx. This streamlines managing websites and apps, helping our clients get updates live faster with fewer errors.