Learn step-by-step how to setup Git on AlmaLinux 9, create a secure Git user, set up a bare repository, and configure Nginx for automatic website deployment.
We prioritize secure, efficient, and automated code deployment. Using Git on AlmaLinux 9 combined with Nginx web server enables seamless updates to websites or applications with minimal downtime and effort. This comprehensive tutorial covers installation, user setup, repository configuration, deployment automation, and testing — everything needed for production-ready Git deployment on AlmaLinux 9.
Prerequisites
- A AlmaLinux 9 installed dedicated server or KVM VPS.
- A root user or normal user with administrative privileges.
- Basic Linux command knowledge.
How to setup Git on AlmaLinux 9
1. Update the System and Install Git and Nginx
Start by updating your system packages and installing Git and Nginx:
sudo dnf update -y
sudo dnf install git nginx -y
Check the installed Git version:
git --version
Check Nginx status:
sudo systemctl status nginx
If Nginx is not running, start and enable it:
sudo systemctl start nginx
sudo systemctl enable nginx
2. Create a Dedicated git User for Git Repository Management
Create a system user git without password and without sudo privileges:
sudo adduser git --shell /bin/bash --create-home
sudo passwd -l git
This creates the user and locks the password to prevent direct login via password — access will be via SSH keys only.
3. Prepare the Deployment Directory /var/www/project
Create the directory where the live website files will be deployed:
sudo mkdir -p /var/www/project
Assign ownership to the git user and the web group (nginx in AlmaLinux):
sudo chown -R git:nginx /var/www/project
sudo chmod -R 775 /var/www/project
This allows the git user to write deployed files and nginx to read them.
4. Configure SSH Access for the git User
Switch to git user:
sudo su - git
Create .ssh
directory and set permissions:
mkdir ~/.ssh
chmod 700 ~/.ssh
Create authorized_keys
file:
nano ~/.ssh/authorized_keys
Paste public SSH keys of authorized developers here, then save and exit.
Set proper permissions:
chmod 600 ~/.ssh/authorized_keys
5. Create a Bare Git Repository as git User
Log back in as git user:
mkdir -p ~/repos/project.git
cd ~/repos/project.git
git init --bare
This bare repository will receive code pushes.
6. Configure the post-receive Git Hook for Auto Deployment
Create and edit the post-receive hook script inside the bare repo:
cd ~/repos/project.git/hooks
nano post-receive
Paste this content to checkout files into the deployment directory on push:
#!/bin/bash
GIT_WORK_TREE=/var/www/project git checkout -f
Save and exit, then make it executable:
chmod +x post-receive
Exit back to your main user:
exit
7. Configure SELinux (Important for AlmaLinux)
AlmaLinux has SELinux enabled by default, which may block Nginx from serving files in /var/www/project
. Set proper SELinux context:
sudo dnf install policycoreutils-python-utils -y
sudo semanage fcontext -a -t httpd_sys_content_t "/var/www/project(/.*)?"
sudo restorecon -Rv /var/www/project
If SELinux denies writes from Git user in deployment, you might need to allow it or temporarily set SELinux permissive mode (not recommended for production).
8. Configure Nginx to Serve the Project
Create an Nginx server block:
sudo nano /etc/nginx/conf.d/project.conf
Add:
server {
listen 80;
server_name your_domain_or_ip;
root /var/www/project;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
Test Nginx config and reload:
sudo nginx -t
sudo systemctl reload nginx
9. Test the Git Deployment
On your local machine:
Clone the bare repo (replace git@server_ip):
git clone git@server_ip:repos/project.git
cd project
Add a test file and push:
echo "Hello AlmaLinux World" > index.html
git add index.html
git commit -m "Initial commit"
git push origin master
- On the server, verify
/var/www/project/index.html
contains your content. - Open browser to your server IP/domain — the page should display "Hello AlmaLinux World."
Best Practices for AlmaLinux Git Deployment
- Use SSH keys for secure authentication.
- Assign proper ownership and permissions to deployment directories.
- Configure SELinux correctly to avoid access issues.
- Do not give git user sudo privileges.
- Regularly update system and software (sudo dnf update).
- Backup bare repositories and deployment directories.
- Monitor Nginx logs in /var/log/nginx/.
We've Learnt step-by-step how to setup Git on AlmaLinux 9. By following these steps, we ensure a secure, efficient, and automated code deployment setup on AlmaLinux 9 Server with Git and Nginx. This streamlines managing websites and apps, helping our clients get updates live faster with fewer errors.