Skip to content

Commit 30209ae

Browse files
committed
Proofreading and duplication
1 parent 3573013 commit 30209ae

File tree

14 files changed

+2958
-7
lines changed

14 files changed

+2958
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
---
2+
title: "Automating the deployment of your website on your VPS via GitLab CI/CD"
3+
excerpt: "Find out how to deploy and automate your website’s code via GitLab CI/CD on an OVHcloud VPS"
4+
updated: 2025-01-28
5+
---
6+
7+
## Objective
8+
9+
Automating the deployment of your website on a VPS greatly simplifies the management of updates. With GitLab CI/CD, you can configure an automatic deployment pipeline, avoiding manual deployments. This method ensures fast and reliable deployments, while reducing the risk of human error. Whether you are a beginner or an experienced developer, this tutorial will help you set up a professional solution adapted to your needs.
10+
11+
**Find out how to automate the deployment of your web applications with GitLab CI/CD on an OVHcloud VPS.**
12+
13+
## Requirements
14+
15+
- A functional [VPS](/links/bare-metal/vps) in your OVHcloud account
16+
- An active GitLab account
17+
- A GitLab project containing your website code
18+
- A VPS configured with the necessary services (e.g. Apache/Nginx, PHP, DBMS, etc.)
19+
- Administrative access to the VPS (via SSH)
20+
- Ensure the `rsync` package is installed on the VPS (required for file synchronization)
21+
22+
> [!primary]
23+
> To ensure that you meet the requirements, please read the guides [Installing a web development environment on a VPS or a dedicated server](/pages/bare_metal_cloud/virtual_private_servers/install_env_web_dev_on_vps) and [Securing a VPS](/pages/bare_metal_cloud/virtual_private_servers/secure_your_vps).
24+
25+
> [!warning]
26+
> If you need assistance, please read our guide on [Getting started with a VPS](/pages/bare_metal_cloud/virtual_private_servers/starting_with_a_vps) before reading this guide further.
27+
28+
## Instructions
29+
30+
**Contents:**
31+
32+
- [Configure SSH access for GitLab CI/CD](#configure-ssh)
33+
- [Add private key to GitLab](#add-private-key-gitlab)
34+
- [Configure GitLab CI/CD for automatic deployment](#configure-gitlab-ci-cd)
35+
- [Test and verify pipeline](#test-pipeline)
36+
- [Conclusion](#conclusion)
37+
38+
### Configure SSH access for GitLab CI/CD <a name="configure-ssh"></a>
39+
40+
To allow GitLab CI/CD to automatically deploy your website, configure a secure SSH access to your VPS.
41+
42+
#### Create an SSH key pair
43+
44+
Log in to your VPS via SSH and generate a dedicated SSH key pair for GitLab CI/CD:
45+
46+
```bash
47+
ssh-keygen -t rsa -b 4096 -C "gitlab-ci" -f /home/<user>/.ssh/deploy_key
48+
```
49+
50+
Replace `<user>` with the user configured to connect to your VPS.
51+
52+
Press `Enter` when prompted for a passphrase (leaving the passphrase empty simplifies automating deployment with GitLab CI/CD. However, it is imperative to secure the private key by limiting it to this use and storing it securely).
53+
54+
You get two files:
55+
56+
- `/home/<user>/.ssh/deploy_key`: private key
57+
- `/home/<user>/.ssh/deploy_key.pub`: public key
58+
59+
Refer to the [official GitLab documentation](https://docs.gitlab.com/ee/user/ssh.html){.external} for more details on generating an SSH key pair.
60+
61+
#### Configure the public key on the VPS
62+
63+
To allow GitLab CI/CD to connect to your VPS via SSH and deploy your website’s code there, add the generated public key to the list of authorized keys on the VPS.
64+
65+
1\. Create the `.ssh` directory:
66+
67+
```bash
68+
mkdir -p /home/<user>/.ssh
69+
chmod 700 /home/<user>/.ssh
70+
```
71+
72+
2\. Add the public key to the `authorized_keys` file:
73+
74+
```bash
75+
cat /home/<user>/.ssh/deploy_key.pub >> /home/<user>/.ssh/authorized_keys
76+
chmod 600 /home/<user>/.ssh/authorized_keys
77+
```
78+
79+
3\. Test the SSH connection with the private key to confirm that the access is functional:
80+
81+
```bash
82+
ssh -i /home/<user>/.ssh/deploy_key <user>@<VPS_IP>
83+
```
84+
85+
Replace `<user>` with the user configured to connect to your VPS and `<VPS_IP>` with the IP address of your VPS.
86+
87+
#### Add the public key to GitLab
88+
89+
Once you have configured the public key on your VPS, add it to your GitLab account. Copy the content of the generated public key from your VPS with:
90+
91+
```bash
92+
cat /home/<user>/.ssh/deploy_key.pub
93+
```
94+
95+
Follow the steps in the "Add an SSH key to your GitLab account" section of the [GitLab official documentation](https://docs.gitlab.com/ee/user/ssh.html#add-an-ssh-key-to-your-gitlab-account){.external} to add your public key to your GitLab account.
96+
97+
#### Configure SSH access to GitLab on the VPS
98+
99+
To ensure that GitLab uses the generated private key to establish a secure SSH connection, configure the `~/.ssh/config` file. This step facilitates subsequent commands by eliminating the need to manually specify the private key each time you interact with GitLab.
100+
101+
On your VPS, create or modify the file `~/.ssh/config`:
102+
103+
```bash
104+
nano ~/.ssh/config
105+
```
106+
107+
Add this configuration for access to GitLab:
108+
109+
```console
110+
Host gitlab.com
111+
HostName gitlab.com
112+
User git
113+
IdentityFile /home/<user>/.ssh/deploy_key
114+
```
115+
116+
Save and exit the editor.
117+
118+
Test the SSH connection with GitLab:
119+
120+
```bash
121+
122+
```
123+
124+
The following message should appear:
125+
126+
```console
127+
Welcome to GitLab, <username>!
128+
```
129+
130+
### Add the private key to GitLab <a name="add-private-key-gitlab"></a>
131+
132+
Copy the content of the generated private key to your VPS with:
133+
134+
```bash
135+
cat /home/<user>/.ssh/deploy_key
136+
```
137+
138+
To add the SSH private key as a CI/CD variable, see the [official GitLab documentation](https://docs.gitlab.com/ee/ci/jobs/ssh_keys.html){.external}.
139+
140+
### Configure GitLab CI/CD for automatic deployment <a name="configure-gitlab-ci-cd"></a>
141+
142+
#### Create a `.gitlab-ci.yml` file for the pipeline
143+
144+
1\. In your local GitLab project, create a `.gitlab-ci.yml` file at the root:
145+
146+
```bash
147+
nano .gitlab-ci.yml
148+
```
149+
150+
2\. Add the following configuration to the file to automatically deploy your website:
151+
152+
```yaml
153+
stages:
154+
- deploy
155+
156+
deploy:
157+
stage: deploy
158+
image: debian:bullseye
159+
before_script:
160+
- apt-get update && apt-get install -y rsync openssh-client
161+
- mkdir -p ~/.ssh
162+
- echo "$DEPLOY_PRIVATE_KEY" > ~/.ssh/id_rsa
163+
- chmod 600 ~/.ssh/id_rsa
164+
- ssh-keyscan -H <VPS_IP> >> ~/.ssh/known_hosts
165+
script:
166+
- rsync -avz --delete ./ <user>@<VPS_IP>:/var/www/html/
167+
only:
168+
- main
169+
```
170+
171+
Replace:
172+
173+
- `<VPS_IP>` by your VPS IP address
174+
- `<user>` by the SSH user configured on your VPS
175+
176+
3\. Add the file, commit it then push the changes to the remote repository:
177+
178+
```bash
179+
git add .gitlab-ci.yml
180+
git commit -m "Add GitLab CI/CD pipeline configuration"
181+
git push origin main
182+
```
183+
184+
### Check and test the pipeline <a name="test-pipeline"></a>
185+
186+
#### Check execution of the first pipeline
187+
188+
Go to the `Build > Pipelines` tab in the side menu of GitLab to check that your first pipeline ran successfully.
189+
190+
If an error occurs, click on the failed pipeline to view the logs.
191+
192+
#### **Insufficient permissions**
193+
194+
During the first deployment, errors can occur concerning permissions (`Permission denied (13)`, `rsync: failed to set times`, etc.)
195+
196+
1\. Ensure that the user has the required permissions
197+
198+
Ensure that the SSH user configured on your VPS has write permissions to the entire Git directory (`/var/www/html`) and its subdirectories:
199+
200+
```bash
201+
sudo chown -R <user>:www-data /var/www/html
202+
sudo chmod -R 775 /var/www/html
203+
```
204+
205+
2\. Test locally with rsync
206+
207+
Before relaunching the GitLab pipeline, test the `rsync` command manually from your local machine. This will allow you to confirm that the permissions are correctly configured:
208+
209+
```bash
210+
rsync -avz --no-times --exclude='.git*' -e "ssh -i ./deploy_key -o StrictHostKeyChecking=no" ./ <user>@<VPS_IP>:/var/www/html/
211+
```
212+
213+
If this command succeeds, then restart the pipeline on GitLab.
214+
215+
### Conclusion <a name="conclusion"></a>
216+
217+
By following this guide, you have set up an automatic deployment pipeline between your GitLab project and your OVHcloud VPS using GitLab CI/CD. This workflow streamlines website updates, eliminating time-consuming manual deployments.
218+
219+
## Go further <a name="go-further"></a>
220+
221+
[Getting started with a VPS](/pages/bare_metal_cloud/virtual_private_servers/starting_with_a_vps)
222+
223+
[Secure a VPS](/pages/bare_metal_cloud/virtual_private_servers/secure_your_vps)
224+
225+
For specialized services (SEO, development, etc.), contact the [OVHcloud partners](/links/partner).
226+
227+
Join our [community of users](/links/community).

0 commit comments

Comments
 (0)