Git è un sistema di controllo della versione gratuito e open source che può essere utilizzato per tenere traccia delle modifiche al codice. Git ti consente di creare molti repository per la stessa applicazione e di coordinare il lavoro su quei file tra più persone. Viene utilizzato principalmente per la gestione del codice sorgente nello sviluppo di software.
In questo articolo impareremo come installare un server HTTP Git con Nginx su Ubuntu 16.04.
Requisiti
- Server Ubuntu 16.04 fresco installato sul tuo sistema.
- Utente Sudo con privilegi di root.
- Indirizzo IP statico 192.168.15.189 configura sul tuo server
1 Per iniziare
Prima di iniziare, dovrai aggiornare il tuo sistema con l'ultima versione stabile.
Puoi farlo eseguendo il seguente comando:
sudo apt-get update -y
sudo apt-get upgrade -y
Una volta che il tuo sistema è stato aggiornato, riavvia il sistema e accedi con sudo user.
2 Installa i pacchetti richiesti
Innanzitutto, dovrai installare alcuni pacchetti richiesti tra cui nginx, git, nano e fcgiwrap sul tuo sistema. Puoi installarli tutti eseguendo il seguente comando:
sudo apt-get install nginx git nano fcgiwrap apache2-utils -y
Una volta installati tutti i pacchetti richiesti, dovrai creare una directory per il repository Git. Puoi farlo eseguendo il seguente comando:
sudo mkdir /var/www/html/git
Quindi, dai l'autorizzazione adeguata alla directory Git:
sudo chown -R www-data:www-data /var/www/html/git
Una volta terminato, puoi procedere alla configurazione del server web Nginx.
3 Configura Nginx
Innanzitutto, dovrai configurare Nginx per trasmettere il traffico Git a Git. Puoi farlo modificando il file di configurazione predefinito di Nginx:
sudo nano /etc/nginx/sites-available/default
Modifica il file come mostrato di seguito:
# Default server configuration # server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html/git; # Add index.php to the list if you are using PHP index index.html index.htm index.nginx-debian.html; server_name _; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; } location ~ (/.*) { client_max_body_size 0; # Git pushes can be massive, just to make sure nginx doesn't suddenly cut the connection add this. auth_basic "Git Login"; # Whatever text will do. auth_basic_user_file "/var/www/html/git/htpasswd"; include /etc/nginx/fastcgi_params; # Include the default fastcgi configs fastcgi_param SCRIPT_FILENAME /usr/lib/git-core/git-http-backend; # Tells fastcgi to pass the request to the git http backend executable fastcgi_param GIT_HTTP_EXPORT_ALL ""; fastcgi_param GIT_PROJECT_ROOT /var/www/html/git; # /var/www/git is the location of all of your git repositories. fastcgi_param REMOTE_USER $remote_user; fastcgi_param PATH_INFO $1; # Takes the capture group from our location directive and gives git that. fastcgi_pass unix:/var/run/fcgiwrap.socket; # Pass the request to fastcgi } }
Salva e chiudi il file quando hai finito. Quindi testa Nginx per qualsiasi errore di configurazione con il seguente comando:
sudo nginx -t
Se tutto va bene, dovresti vedere il seguente output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Successivamente, dovrai creare un account utente di cui dovrai utilizzare per sfogliare il commit nel repository. Puoi creare un utente con il nome hitesh usando l'utilità htpasswd:
sudo htpasswd -c /var/www/html/git/htpasswd hitesh
Infine, riavvia Nginx per applicare tutte le modifiche con il seguente comando:
sudo systemctl restart nginx
Puoi controllare lo stato del server Nginx con il seguente comando:
sudo systemctl status nginx
Dovresti vedere il seguente output:
?? nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2017-06-20 23:00:11 IST; 51min ago Process: 12415 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS) Process: 7616 ExecReload=/usr/sbin/nginx -g daemon on; master_process on; -s reload (code=exited, status=0/SUCCESS) Process: 12423 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Process: 12419 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Main PID: 12427 (nginx) CGroup: /system.slice/nginx.service ??????12427 nginx: master process /usr/sbin/nginx -g daemon on; master_process on ??????12431 nginx: worker process Jun 20 23:00:11 localhost systemd[1]: Stopped A high performance web server and a reverse proxy server. Jun 20 23:00:11 localhost systemd[1]: Starting A high performance web server and a reverse proxy server... Jun 20 23:00:11 localhost systemd[1]: nginx.service: Failed to read PID from file /run/nginx.pid: Invalid argument Jun 20 23:00:11 localhost systemd[1]: Started A high performance web server and a reverse proxy server.
4 Crea repository Git
Una volta che tutto è configurato correttamente, è il momento di creare un repository Git.
Puoi creare un repository con nome repo.git con il seguente comando:
cd /var/www/html/git
sudo mkdir hitesh.git
sudo cd hitesh.git
sudo git --bare init
sudo git update-server-info
sudo chown -R www-data.www-data .
sudo chmod -R 777 .
Successivamente, dovrai consentire il servizio http tramite il firewall UFW. Per impostazione predefinita UFW è disabilitato sul tuo sistema, quindi devi prima abilitarlo. Puoi abilitarlo con il seguente comando:
sudo ufw enable
Una volta abilitato il firewall UFW, puoi consentire il servizio HTTP eseguendo il comando seguente:
sudo ufw allow http
Ora puoi controllare lo stato del firewall UFW eseguendo il comando seguente:
sudo ufw status
Ok, questo è tutto per la configurazione lato server. Ora puoi passare al lato client per testare Git.
5 Testare Git sul computer client
Prima di iniziare, dovrai installare git sul sistema client. Puoi installarlo con il seguente comando:
sudo apt-get install git -y
Innanzitutto, crea un repository locale con il seguente comando:
sudo mkdir ~/testproject
Quindi, cambia la directory in testproject e avvia il nuovo repository remoto con il seguente comando:
cd ~/testproject
git init
git remote add origin http://[email protected]/hitesh.git
Quindi, crea alcuni file e directory con il seguente comando:
mkdir test1 test2 test3
echo "This is my first repository" > test1/repo1
echo "This is my second repository" > test2/repo2
echo "This is my third repository" > test3/repo3
Quindi, esegui il comando seguente per aggiungere tutti i file e le directory al repository:
git add .
git commit -a -m "Add files and directoires"
Dovresti vedere il seguente output:
[master 002fac9] Add files and directoires 3 files changed, 3 insertions(+) create mode 100644 repo1 create mode 100644 repo2 create mode 100644 repo3
Quindi, invia tutti i file e le directory al server Git con il seguente comando:
git push origin master
Dovresti vedere il seguente output:
Password for 'http://[email protected]': Counting objects: 6, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (5/5), 422 bytes | 0 bytes/s, done. Total 5 (delta 0), reused 0 (delta 0) To http://[email protected]/hitesh.git 68f1270..002fac9 master -> master
Ora, tutti i tuoi file e directory sono stati salvati sul tuo server Git.
Il processo di creazione del tuo repository Git è ora completato. Ora puoi clonare facilmente il tuo repository in futuro. Puoi clonare il tuo repository usando il seguente comando sul sistema remoto:
git clone [email protected]:/var/www/html/git/hitesh.git
Dovresti vedere il seguente output:
Cloning into 'hitesh'... [email protected]'s password: remote: Counting objects: 8, done. remote: Compressing objects: 100% (3/3), done. Receiving objects: 100% (8/8), 598 bytes | 0 bytes/s, done. remote: Total 8 (delta 0), reused 0 (delta 0) Checking connectivity... done.
Ora, cambia la directory nel repository clonato con il seguente comando:
cd hitesh
tree
Dovresti vedere il seguente output:
. |-- test1 | `-- repo1 |-- test2 | `-- repo2 `-- test3 `-- repo3 3 directories, 3 files
Conclusione
Spero che ora tu possa facilmente eseguire il push, pull, clonare e eseguire il commit del codice sorgente utilizzando il server Git. Sentiti libero di commentarmi se hai qualche dubbio.