update from sparkleup
This commit is contained in:
parent
810b41ab5a
commit
df28afa445
165
work/ctm-385.md
165
work/ctm-385.md
|
@ -11,23 +11,13 @@ You can install Jenkins later in this tutorial, if you don't have it installed y
|
|||
This guide assumes that you are using Ubuntu 22.04. Before you begin, you should have a non-**root** user account with `sudo` privileges set up on your system. You can learn how to do this by following the [Ubuntu 22.04 initial server setup tutorial](https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-22-04). You will also need the Nginx server installed and hosting your domain. You can learn how to do this with the [How To Install Nginx on Ubuntu 22.04 tutorial](https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-22-04).
|
||||
|
||||
Additionally, having your Jenkins instance secured by SSL is very important. If is visible on the internet, you can secure it with Let's Encrypt. You can learn how to do this with the [How to Secure Nginx with Let's Encrypt on Ubuntu 22.04 tutorial](https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-22-04).
|
||||
As stated previously, this tutorial assumes that Jenkins is already installed. [This tutorial](https://www.digitalocean.com/community/tutorials/how-to-install-and-use-jenkins-on-ubuntu-12-04) will show you how to install Jenkins if necessary. You will probably need to switch to the root user for that article.
|
||||
|
||||
## Step 1 — Configure Nginx
|
||||
|
||||
Nginx has become a favorite web server for its speed and flexibility in recent years, which makes it an idea choice for our application.
|
||||
|
||||
|
||||
### Get a Certificate
|
||||
|
||||
Next, you will need to purchase or create an SSL certificate. These commands are for a self-signed certificate, but you should get an officially [signed certificate](https://www.digitalocean.com/community/tutorials/openssl-essentials-working-with-ssl-certificates-private-keys-and-csrs) if you want to avoid browser warnings.
|
||||
|
||||
Move into the proper directory and generate a certificate:
|
||||
|
||||
cd /etc/nginx
|
||||
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/cert.key -out /etc/nginx/cert.crt
|
||||
|
||||
You will be prompted to enter some information about the certificate. You can fill this out however you'd like; just be aware the information will be visible in the certificate properties. We've set the number of bits to 2048 since that's the minimum needed to get it signed by a CA. If you want to get the certificate signed, you will need to create a CSR.
|
||||
|
||||
### Edit the Configuration
|
||||
|
||||
Next you will need to edit the default Nginx configuration file.
|
||||
|
@ -36,86 +26,67 @@ Next you will need to edit the default Nginx configuration file.
|
|||
|
||||
Here is what the final configuration might look like; the sections are broken down and briefly explained below. You can update or replace the existing config file, although you may want to make a quick copy first.
|
||||
|
||||
server {
|
||||
listen 80;
|
||||
return 301 https://$host$request_uri;
|
||||
```nginx
|
||||
server {23
|
||||
listen 80;
|
||||
return 301 https://$host$request_uri;
|
||||
}
|
||||
|
||||
server {
|
||||
|
||||
listen 443;
|
||||
server_name <^>jenkins.domain.com<^>;
|
||||
|
||||
access_log /var/log/nginx/jenkins.access.log;
|
||||
|
||||
location / {
|
||||
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
|
||||
proxy_pass http://localhost:8080;
|
||||
proxy_read_timeout 90;
|
||||
|
||||
proxy_redirect http://localhost:8080 https://<^>jenkins.domain.com<^>;
|
||||
}
|
||||
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
server {
|
||||
|
||||
listen 443;
|
||||
server_name <^>jenkins.domain.com<^>;
|
||||
|
||||
ssl_certificate /etc/nginx/cert.crt;
|
||||
ssl_certificate_key /etc/nginx/cert.key;
|
||||
|
||||
ssl on;
|
||||
ssl_session_cache builtin:1000 shared:SSL:10m;
|
||||
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
|
||||
ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
|
||||
ssl_prefer_server_ciphers on;
|
||||
|
||||
access_log /var/log/nginx/jenkins.access.log;
|
||||
|
||||
location / {
|
||||
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
|
||||
# Fix the “It appears that your reverse proxy set up is broken" error.
|
||||
proxy_pass http://localhost:8080;
|
||||
proxy_read_timeout 90;
|
||||
|
||||
proxy_redirect http://localhost:8080 https://<^>jenkins.domain.com<^>;
|
||||
}
|
||||
}
|
||||
|
||||
In our configuration, the <^>cert.crt<^> and <^>cert.key<^> settings reflect the location where we created our SSL certificate. You will need to update the <^>server_name<^> and `proxy_redirect` lines with your own domain name. There is some additional Nginx magic going on as well that tells requests to be read by Nginx and rewritten on the response side to ensure the reverse proxy is working.
|
||||
You will need to update the <^>server_name<^> and `proxy_redirect` lines with your own domain name. There is some additional Nginx magic going on as well that tells requests to be read by Nginx and rewritten on the response side to ensure the reverse proxy is working.
|
||||
|
||||
The first section tells the Nginx server to listen to any requests that come in on port 80 (default HTTP) and redirect them to HTTPS.
|
||||
|
||||
...
|
||||
server {
|
||||
listen 80;
|
||||
return 301 https://$host$request_uri;
|
||||
}
|
||||
...
|
||||
```nginx
|
||||
...
|
||||
server {
|
||||
listen 80;
|
||||
return 301 https://$host$request_uri;
|
||||
}
|
||||
...
|
||||
```
|
||||
|
||||
Next we have the SSL settings. This is a good set of defaults but can definitely be expanded on. For more explanation, please read [this tutorial](https://www.digitalocean.com/community/tutorials/how-to-create-an-ssl-certificate-on-nginx-for-ubuntu-14-04).
|
||||
After that, the proxying happens. It basically takes any incoming requests and proxies them to the Jenkins instance that is bound/listening to port 8080 on the local network interface. This is a slightly different situation, but [this tutorial](https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-as-a-front-end-proxy-for-apache) has some good information about the Nginx proxy settings.
|
||||
|
||||
...
|
||||
listen 443;
|
||||
server_name <^>jenkins.domain.com<^>;
|
||||
```nginx
|
||||
...
|
||||
location / {
|
||||
|
||||
ssl_certificate /etc/nginx/cert.crt;
|
||||
ssl_certificate_key /etc/nginx/cert.key;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
|
||||
ssl on;
|
||||
ssl_session_cache builtin:1000 shared:SSL:10m;
|
||||
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
|
||||
ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
|
||||
ssl_prefer_server_ciphers on;
|
||||
...
|
||||
proxy_pass http://localhost:8080;
|
||||
proxy_read_timeout 90;
|
||||
|
||||
The final section is where the proxying happens. It basically takes any incoming requests and proxies them to the Jenkins instance that is bound/listening to port 8080 on the local network interface. This is a slightly different situation, but [this tutorial](https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-as-a-front-end-proxy-for-apache) has some good information about the Nginx proxy settings.
|
||||
|
||||
...
|
||||
location / {
|
||||
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
|
||||
# Fix the “It appears that your reverse proxy set up is broken" error.
|
||||
proxy_pass http://localhost:8080;
|
||||
proxy_read_timeout 90;
|
||||
|
||||
proxy_redirect http://localhost:8080 https://<^>jenkins.domain.com<^>;
|
||||
}
|
||||
...
|
||||
proxy_redirect http://localhost:8080 https://<^>jenkins.domain.com<^>;
|
||||
}
|
||||
...
|
||||
```
|
||||
|
||||
A few quick things to point out here. If you don't have a domain name that resolves to your Jenkins server, then the <^>proxy\_redirect<^> statement above won't function correctly without modification, so keep that in mind. Also, if you misconfigure the <^>proxy\_pass<^> (by adding a trailing slash for example), you will get something similar to the following in your Jenkins Configuration page.
|
||||
|
||||
|
@ -123,36 +94,36 @@ A few quick things to point out here. If you don't have a domain name that resol
|
|||
|
||||
So, if you see this error, double-check your <^>proxy\_pass<^> and <^>proxy\_redirect<^> settings in the Nginx configuration!
|
||||
|
||||
##Step Two — Configure Jenkins
|
||||
## Step 2 — Configure Jenkins
|
||||
|
||||
As stated previously, this tutorial assumes that Jenkins is already installed. [This tutorial](https://www.digitalocean.com/community/tutorials/how-to-install-and-use-jenkins-on-ubuntu-12-04) will show you how to install Jenkins if necessary. You will probably need to switch to the root user for that article.
|
||||
For Jenkins to work with Nginx, we need to update the Jenkins config to listen only on the localhost address instead of all (0.0.0.0), to ensure traffic gets handled properly. This is an important security step because if Jenkins is still listening on all addresses, then it will still potentially be accessible via its original port (8080). We will modify the <^>/etc/default/jenkins<^> configuration file to make these adjustments.
|
||||
|
||||
For Jenkins to work with Nginx, we need to update the Jenkins config to listen only on the localhost interface instead of all (0.0.0.0), to ensure traffic gets handled properly. This is an important step because if Jenkins is still listening on all interfaces, then it will still potentially be accessible via its original port (8080). We will modify the <^>/etc/default/jenkins<^> configuration file to make these adjustments.
|
||||
|
||||
sudo nano /etc/default/jenkins
|
||||
```command
|
||||
sudo nano /etc/default/jenkins
|
||||
```
|
||||
|
||||
Locate the `JENKINS\_ARGS` line and update it to look like the folowing:
|
||||
Locate the `JENKINS\_ARGS` line and update it to look like the following:
|
||||
|
||||
JENKINS_ARGS="--webroot=/var/cache/jenkins/war --httpListenAddress=127.0.0.1 --httpPort=$HTTP_PORT -ajp13Port=$AJP_PORT"
|
||||
```
|
||||
JENKINS_ARGS="--webroot=/var/cache/jenkins/war --httpListenAddress=127.0.0.1 --httpPort=$HTTP_PORT -ajp13Port=$AJP_PORT"
|
||||
```
|
||||
|
||||
Notice that the **--httpListenAddress=127.0.0.1** setting needs to be either added or modified.
|
||||
|
||||
Then go ahead and restart Jenkins and Nginx.
|
||||
|
||||
sudo service jenkins restart
|
||||
sudo service nginx restart
|
||||
```command
|
||||
sudo service jenkins restart
|
||||
sudo service nginx restart
|
||||
```
|
||||
|
||||
You should now be able to visit your domain using either HTTP or HTTPS, and the Jenkins site will be served securely. You will see a certificate warning if you used a self-signed certificate.
|
||||
You should now be able to visit your domain using HTTPS, and the Jenkins site will be served securely.
|
||||
|
||||
## Optional — Update OAuth URLs
|
||||
|
||||
If you are using the GitHub or another OAuth plugin for authentication, it will probably be broken at this point. For example, when attempting to visit the URL, you will get a "Failed to open page" with a URL similar to the following:
|
||||
If you are using the GitHub or another OAuth plugin for authentication, it will probably be broken at this point. For example, when attempting to visit the URL, you will get a "Failed to open page" with a URL similar to `http://jenkins.domain.com:8080/securityRealm/finishLogin?code=random-string`.
|
||||
|
||||
http://jenkins.domain.com:8080/securityRealm/finishLogin?code=random-string
|
||||
|
||||
To fix this you will need to update a few settings, including your OAuth plugin settings. First update the Jenkins URL (in the Jenkins GUI); it can be found here:
|
||||
|
||||
**Jenkins -> Manage Jenkins -> Configure System -> Jenkins Location**
|
||||
To fix this you will need to update a few settings within Jenkins, including your OAuth plugin settings. First update the Jenkins URL in the Jenkins GUI; it can be found in the **Jenkins -> Manage Jenkins -> Configure System -> Jenkins Location** menu.
|
||||
|
||||
Update the Jenkins URL to use HTTPS - `https://<^>jenkins.domain.com/<^>`
|
||||
|
||||
|
|
Loading…
Reference in New Issue