Setting up Nginx and SSL for your server can be a tedious task if done manually. Thankfully, with a bit of automation, you can easily streamline the process using Bash scripts. In this blog, I'll walk you through two simple Bash scripts: one for configuring Nginx and another for setting up SSL using Certbot. These scripts will make your life easier by automating the setup process, so you can focus more on development rather than server management.
Script 1: Automate Nginx Configuration
This first script automates the setup of Nginx for a domain and proxying traffic to an internal application running on a specific port.
Steps Covered in the Script:
- Input Parameters: Takes the domain and port as inputs.
- Install Nginx: Ensures Nginx is installed on the server.
- Nginx Server Block Creation: Dynamically creates an Nginx configuration file based on the input domain and port.
- Enable Configuration: Links the configuration and restarts Nginx.
The Script:
How It Works:
- This script takes two arguments: the domain and the port.
- It checks if Nginx is installed and, if not, installs it.
- The server block for Nginx is created based on the domain and port provided, enabling you to proxy traffic to your web app.
- Finally, the configuration is linked and Nginx is restarted.
Script 2: Automate SSL Configuration with Certbot
Once you have your Nginx server set up, the next step is to secure it using SSL. Certbot makes this process straightforward, and we can automate it using the following script.
Steps Covered in the Script:
- Input Parameters: Takes the domain and your email as inputs.
- Install Certbot: Installs Certbot and its dependencies.
- Obtain SSL Certificate: Uses Certbot to automatically generate and configure the SSL certificate for your domain.
- Reload Nginx: Ensures Nginx is reloaded to apply the new SSL settings.
The Script:
How It Works:
- This script takes the domain and your email as inputs.
- Certbot is installed along with any required dependencies.
- Certbot then automatically generates the SSL certificate for your domain and updates the Nginx configuration to use HTTPS.
- Nginx is reloaded to apply the changes.
How to Use the Scripts
- Nginx Setup: Run the Nginx script by providing your domain and the port number on which your app is running.
- SSL Setup: After setting up Nginx, run the SSL script to secure your domain with an SSL certificate.
Conclusion
By combining these two scripts, you can automate the entire process of setting up Nginx and SSL on your server. This approach not only saves time but also reduces the chance of errors when configuring the server manually. Once set up, your web application will be accessible over HTTPS with a valid SSL certificate.
Feel free to adapt these scripts to your specific use cases and make your server management more efficient!