Ubuntu 16.04 & Nginx & SSL + Jenkins
- Ubuntu Initial setup
- Connect and upgrade
1234ssh root@[your_server_ip]apt-get updateapt-get full-upgrade
- Increase SSL session time
12sudo nano /etc/ssh/sshd_config
Apply below, if exist, change value, if not exist, add the line.
1234TCPKeepAlive noClientAliveInterval 30ClientAliveCountMax 100 - Create a New User, avoid using root all the time
12adduser [user_name]
Give new user Root privileges
12usermod -aG sudo [user_name] - Switch to the new user, install public key
1234su - [user_name]mkdir ~/.sshchmod 700 ~/.ssh
Copy the public key content you want to use and paste into the remote ssh folder:
12nano ~/.ssh/authorized_keysAfter copy, restrict the permissions:
123chmod 600 ~/.ssh/authorized_keysexit - Testing the new user login with ssh key:
12ssh [user_name]@[your_server_ip]
If you can login, then all settings are good.
If not, check all above steps.
Disable Password Authentication
12sudo nano /etc/ssh/sshd_configFind the line that specifies PasswordAuthentication, uncomment it by deleting the preceding #, then change its value to “no”.
It should look like this after you have made the change:
12PasswordAuthentication noReload SSH daemon:
12sudo systemctl reload sshd- Open a new terminal, test login
12ssh [user_name]@[your_server_ip]
- Setup basic firewall
12345sudo ufw app listsudo ufw allow OpenSSHsudo ufw enablesudo ufw status
- Connect and upgrade
- Nginx and Let’s Encrypt
- Install Nginx, allow firewall rules
1234sudo apt-get install nginxsudo ufw allow 'Nginx FULL'sudo ufw status
- Check the Nginx install
123Check this URL:http://[server_domain_or_IP]
- Domain DNS change: create an A Record that points your domain to the public IP address of your server.
Install certbot
12345$ sudo apt-get install software-properties-common$ sudo add-apt-repository ppa:certbot/certbot$ sudo apt-get update$ sudo apt-get install certbot- Use certbot Webroot Plugin
Edit Nginx default file to allow/.well-known
for Webroot Plugin12sudo nano /etc/nginx/sites-available/defaultInside the server block, add this location block:
1234location ~ /.well-known {allow all;}check for syntac errors:
123sudo nginx -tsudo systemctl restart nginxUse Webroot plugin to request an SSL
12sudo certbot certonly --webroot --webroot-path=[/var/www/html] -d [example.com] -d [ [www.example.com](http://www.example.com) ] - Certificate Files
After obtaining the cert, you will have the following PEM-encoded files:cert.pem
: Your domain’s certificatechain.pem
: The Let’s Encrypt chain certificatefullchain.pem
: cert.pem and chain.pem combinedprivkey.pem
: Your certificate’s private keyCheck that the files exist by running this command (substituting in your domain name):
12sudo ls -l /etc/letsencrypt/live/[your_domain_name] - Generate Strong Diffie-Hellman Group
12sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048
This may take a few minutes but when it’s done you will have a strong DH group at
/etc/ssl/certs/dhparam.pem
. Configure TLS/SSL on Web Server (Nginx)
- We will create a configuration snippet containing our SSL key and certificate file locations.
Within this file, we just need to set the ssl_certificate directive to our certificate file and the ssl_certificate_key to the associated key.
In our case, this will look like this:1234sudo nano /etc/nginx/snippets/ssl-[example.com].confssl_certificate /etc/letsencrypt/live/[example.com]/fullchain.pem;ssl_certificate_key /etc/letsencrypt/live/[example.com]/privkey.pem; - We will create a configuration snippet containing strong SSL settings that can be used with any certificates in the future.
1234567891011121314151617181920212223sudo nano /etc/nginx/snippets/ssl-params.conf# Paste below inside ## from https://cipherli.st/# and https://raymii.org/s/tutorials/Strong_SSL_Security_On_nginx.htmlssl_protocols TLSv1 TLSv1.1 TLSv1.2;ssl_prefer_server_ciphers on;ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";ssl_ecdh_curve secp384r1;ssl_session_cache shared:SSL:10m;ssl_session_tickets off;ssl_stapling on;ssl_stapling_verify on;resolver 8.8.8.8 8.8.4.4 valid=300s;resolver_timeout 5s;# disable HSTS header for now#add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";add_header X-Frame-Options SAMEORIGIN;add_header X-Content-Type-Options nosniff;ssl_dhparam /etc/ssl/certs/dhparam.pem;
- We will adjust the Nginx server blocks to handle SSL requests and use the two snippets above.
Backup server block123sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.baksudo nano /etc/nginx/sites-available/defaultEventally, the server block should looks like:
123456789101112131415161718192021222324252627server {listen 80 default_server;listen [::]:80 default_server;server_name [example.com] [www.example.com];return 301 https://$server_name$request_uri;}server {# SSL configurationlisten 443 ssl http2 default_server;listen [::]:443 ssl http2 default_server;include snippets/ssl-[example.com].conf;include snippets/ssl-params.conf;server_name [example.com] [www.example.com];root /var/www/html;# Add index.php to the list if you are using PHPindex index.html index.htm index.nginx-debian.html;location / {# First attempt to serve request as file, then# as directory, then fall back to displaying a 404.try_files $uri $uri/ =404;}location ~ /.well-known {allow all;}}
This method of configuring Nginx will allow us to keep clean server blocks and put common configuration segments into reusable modules.
- We will create a configuration snippet containing our SSL key and certificate file locations.
check if the nginx setting is right
12sudo nginx -t
- Install Nginx, allow firewall rules
- check firewall
12sudo ufw status
- Enabling the Changes in Nginx
12sudo systemctl restart nginx
use the Qualys SSL Labs Report to see how your server configuration scores:
https://www.ssllabs.com/ssltest/analyze.html?d=[example.com] Setup auto renewal
12345sudo crontab -e# Server UTC time 17:10 is Sydney time 3:10 am10 17 * * * /usr/bin/certbot renew --quiet --renew-hook "/bin/systemctl reload nginx"- Install Jenkins
1234567wget -q -O - https://pkg.jenkins.io/debian/jenkins-ci.org.key | sudo apt-key add -echo deb http://pkg.jenkins.io/debian-stable binary/ | sudo tee /etc/apt/sources.list.d/jenkins.listsudo apt-get updatesudo apt-get install jenkins
Starting Jenkins
123456sudo systemctl start jenkinssudo systemctl status jenkinssudo ufw allow 8080sudo ufw statusTesting
http://
ip_address_or_domain_name
:8080Get password
12sudo cat /var/lib/jenkins/secrets/initialAdminPassword - Config Nginx
123456789101112131415161718192021222324252627282930313233server {listen 80 default_server;listen [::]:80 default_server;server_name jenkins.jingbojin.com;return 301 https://$server_name$request_uri;}server {# SSL configurationlisten 443 ssl http2 default_server;listen [::]:443 ssl http2 default_server;include snippets/ssl-jenkins.jingbojin.com.conf;include snippets/ssl-params.conf;server_name jenkins.jingbojin.com;root /var/www/html;access_log /var/log/nginx/jenkins.access.log;error_log /var/log/nginx/jenkins.error.log;location / {include /etc/nginx/proxy_params;proxy_pass http://localhost:8080;proxy_read_timeout 90s;# Fix potential "It appears that your reverse proxy set up is broken" error.proxy_redirect http://localhost:8080 https://jenkins.jingbojin.com;}location ~ /.well-known {allow all;}}
- Config Jenkins
12sudo nano /etc/default/jenkins
Locate the JENKINS_ARGS line and add
—httpListenAddress=127.0.0.1
to the existing arguments:12JENKINS_ARGS="--webroot=/var/cache/$NAME/war --httpPort=$HTTP_PORT --httpListenAddress=127.0.0.1" - Restart Jenkins and Nginx
12345sudo systemctl restart jenkinssudo systemctl status jenkinssudo systemctl restart nginxsudo systemctl status nginx
- In your web browser, enter “http://[your.ssl.domain.name]”, substituting your domain for your.ssl.domain.name. After you press enter, the URL should start with https and the location bar should indicate that the connection is secure.
- We’ll enter admin in the “User” field and the auto-generated password that Jenkins created and stored when we installed it.
12sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Reference:
initial-server-setup-with-ubuntu-16-04
how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04
how-to-configure-jenkins-with-ssl-using-an-nginx-reverse-proxy
how-do-you-score-a-with-100-on-all-categories-on-ssl-labs-test-with-lets-encry