Setup Jenkins and Gitlab on Custom Subdomain with Nginx Proxy Manager
Tags: devops, gitlab, jenkins
Published: 2022-12-26 08:08:01

## 📍 Description
The goal on this tutorial to make the small labs that the Jenkins, and Gitlab can run on the VPS and also we can access them with custom subdomain like: 

- jenkins.domain.com
- gitlab.domain.com

![Nginx Proxy Manager](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p6wm7a0nhhsv5gys1y1s.png)

To make this possible, we need help **Nginx Proxy Manager** to create proxy to our specific IP Address and port. We will install the jenkins, and gitlab on the docker to make the installation easy.

## 📍 Getting Started

To get started, we need follow this step by step: 

1. Install Docker
2. Install Nginx Proxy Manager on Docker
3. Install Jenkins & Gitlab on Docker
4. Add the DNS Manager and set A record subdomain to VPS IP

At this post, I using the Ubuntu 22, the installation it's depend on the server you using, but on the main line, it's the same. 

### 🔥 Install the docker & docker-compose

First, we install docker with this command: 

`apt install docker`

And, we install docker-compose

`apt install docker-compose`

The docker needed to further action like install the Nginx Proxy Manager, Jenkins and Gitlab. So, make sure you install the docker before do another thing. 

### 🔥 Install Nginx Proxy Manager on Docker

Docker, check! Next, we install the Nginx Proxy Manager on Docker by using this step by step.

- Create a new directory and add docker-compose.yml file similar to this on your new directory:

```
version: '3'
services:
  app:
    image: 'jc21/nginx-proxy-manager:latest'
    restart: unless-stopped
    ports:
      - '80:80'
      - '81:81'
      - '443:443'
    volumes:
      - ./data:/data
      - ./letsencrypt:/etc/letsencrypt
```
- Bring up your stack by running

```
docker-compose up -d
```

- Log in to the Admin UI using this default password: 

```
http://your-vps-ip:81
Email:    admin@example.com
Password: changeme
```

All this step by step is same on the original nginx proxy manager documentation:
https://nginxproxymanager.com/guide/#quick-setup

### 🔥 Install Jenkins 
Okay, nginx proxy manager is already installed, next we need install jenkins on docker. 

You can run this command to install the jenkins on your vps: 

```
docker run -d --name jenkins -p 9091:8080 -p 50000:50000 jenkins/jenkins:lts-jdk11
```

After that, your Jenkins will be run on port 8080, you can grep the initial password on the docker logs. 

Navigate to http://your-vps-ip:9091 and setup the Jenkins with the initial password. 

### 🔥 Install Gitlab 
We successfully install the jenkins and looks like jenkins work perfectly fine, next we need to install gitlab on docker!

- Run this command for initial before install gitlab
```
mkdir -p gitlab; cd gitlab/
mkdir -p /srv/gitlab/{config/ssl,logs,data}
```
- Define the environment variable "GITLAB_HOME" with the value as GitLab data directory "/srv/gitlab" as below.
```
GITLAB_HOME=/srv/gitlab
```

- Go to gitlab directory and add docker-compose.yml file similar to this on gitlab directory, edit the domain and hostname:

```
web:
  image: 'gitlab/gitlab-ce:latest'
  restart: always
  hostname: 'gitlab.yourdomain.com'

  environment:
    GITLAB_OMNIBUS_CONFIG: |
      # Add any other gitlab.rb configuration here, each on its own line
      external_url 'http://gitlab.yourdomain.com'
      gitlab_rails['gitlab_shell_ssh_port'] = 2224

  ports:
    - '8081:80'
    - '4443:443'
    - '2224:22'

  volumes:
    - '${GITLAB_HOME}/config:/etc/gitlab'
    - '${GITLAB_HOME}/logs:/var/log/gitlab'
    - '${GITLAB_HOME}/data:/var/opt/gitlab'
    - '${GITLAB_HOME}/config/ssl:/etc/gitlab/ssl'
```

- Run the installation

```
docker-compose up -d
```

- Get the gitlab initial root password by using this command 
```
sudo docker exec -it gitlab grep 'Password:' /etc/gitlab/initial_root_password
```

## 📍 Add the DNS Manager and set A record subdomain to VPS IP

All the application is already installed. Yey! Next, we need to pointing our custom subdomain to our VPS / Nginx Proxy Manager, and we will manage the subdomain on our Nginx Proxy Manager. 

- Go to your domain DNS Management, and add the subdomain jenkins, gitlab and pointing to your vps (that we install nginx proxy manager, jenkins, gitlab) like screenshot below. 

![DNS Management](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z2ecevefwmu8v9buwkl5.png)

- Go to Nginx Proxy Manager, add the subdomain that we already setup earlier and add the your vps IP and Port. 

Example: We setup the jenkins on port 9091, so we input the your-vps-ip:9091 on the `Forward Hostname / IP` sections, as shown on the screenshot below. 

![Jenkins](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0bv7mdv9809zs6r9621h.png)

- Add the SSL 

Go to SSL tab on the right side and set the SSL as shown on the screenshot below. 

![Jenkins Nginx Proxy Manager](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ils1e2johb9pqppu009f.png)

And saved the changes. 

- Go to your subdomain! 

After that, you just need to navigate to your jenkins subdomain, and you will see the jenkins webservice on your subdomain. 

![Jenkins Nginx Proxy Manager](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n3r026bwqjn3ykxwd8ad.png)

Great! Mission Success! Now, you can add the gitlab and proxy manager custom subdomain too!

#### 🔥 References:
https://forum.gitlab.com/t/how-to-login-for-the-first-time-local-install-with-docker-image/55297
https://www.howtoforge.com/how-to-install-gitlab-with-docker-on-ubuntu-2004/
https://octopus.com/blog/jenkins-docker-install-guide
https://nginxproxymanager.com/guide/#quick-setup
https://aksarakode.com/