Created
May 24, 2024 01:16
-
-
Save mtahle/74092c1564fa76f1bac9391b7d73d6ba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Error handling | |
set -e | |
set -o pipefail | |
error_exit() { | |
echo "Error: $1" | |
exit 1 | |
} | |
echo "Welcome to the Nginx reverse proxy installation script." | |
# Check if Nginx is already installed | |
if dpkg -l nginx | grep -q '^ii'; then | |
read -p "Nginx is already installed. Do you want to add a new configuration? (y/n): " add_config | |
if [[ "$add_config" != "y" && "$add_config" != "Y" ]]; then | |
echo "Nginx configuration skipped." | |
exit 0 | |
fi | |
else | |
read -p "Do you want to install Nginx as a reverse proxy? (y/n): " install_nginx | |
if [[ "$install_nginx" != "y" && "$install_nginx" != "Y" ]]; then | |
echo "Nginx installation skipped." | |
exit 0 | |
fi | |
# Update package lists | |
echo "Updating package lists..." | |
sudo apt update || error_exit "Failed to update package lists." | |
# Install Nginx | |
echo "Installing Nginx..." | |
sudo apt install -y nginx || error_exit "Failed to install Nginx." | |
fi | |
# Check if Certbot is already installed | |
if dpkg -l certbot | grep -q '^ii'; then | |
echo "Certbot is already installed." | |
else | |
read -p "Do you want to install Certbot for managing SSL certificates? (y/n): " install_certbot | |
if [[ "$install_certbot" != "y" && "$install_certbot" != "Y" ]]; then | |
echo "Certbot installation skipped." | |
exit 0 | |
fi | |
# Install Certbot | |
echo "Installing Certbot..." | |
sudo apt install -y certbot python3-certbot-nginx || error_exit "Failed to install Certbot." | |
fi | |
# Prompt user for website name | |
read -p "Enter the domain name for the website (e.g., example.com): " domain_name | |
# Check if config file already exists | |
conf_file="/etc/nginx/sites-available/${domain_name}" | |
if [ -f "$conf_file" ]; then | |
read -p "A configuration file for $domain_name already exists. Do you want to overwrite it? (y/n): " overwrite_conf | |
if [[ "$overwrite_conf" == "y" || "$overwrite_conf" == "Y" ]]; then | |
echo "Overwriting existing configuration file..." | |
else | |
echo "Keeping current configuration file. Exiting..." | |
exit 0 | |
fi | |
fi | |
# Configure Nginx as a reverse proxy for PHP-FPM | |
cat <<EOF | sudo tee "$conf_file" | |
server { | |
listen 80; | |
listen [::]:80; | |
server_name ${domain_name}; | |
root /var/www/${domain_name}/public; # Change to your website's document root | |
index index.php index.html index.htm; | |
location / { | |
try_files \$uri \$uri/ /index.php?\$query_string; | |
} | |
location ~ \.php$ { | |
include snippets/fastcgi-php.conf; | |
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; # Change to match your PHP version | |
} | |
location ~ /\.ht { | |
deny all; | |
} | |
} | |
EOF | |
# Create a symbolic link to enable the site | |
sudo ln -sf "$conf_file" /etc/nginx/sites-enabled/ | |
# Test Nginx configuration | |
sudo nginx -t || error_exit "Nginx configuration test failed." | |
# Reload Nginx to apply changes | |
sudo systemctl reload nginx || error_exit "Failed to reload Nginx." | |
# Obtain SSL certificate using Certbot | |
echo "Obtaining SSL certificate using Certbot..." | |
sudo certbot --nginx -d "$domain_name" || error_exit "Failed to obtain SSL certificate using Certbot." | |
echo "Nginx has been successfully configured as a reverse proxy for ${domain_name}, and SSL certificate has been obtained." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment