Set up new WordPress in Ubuntu, Debian, Mint…

.sh file ==>

#!/bin/bash

# Variables
FOLDER_NAME=${1:-wordpress}
DB_NAME=${2:-wordpress}
DB_USER=${3:-wordpressuser}
DB_PASSWORD=${4:-password}
DOMAIN_NAME=${5:-example.com}

# Update and install necessary packages
sudo apt update
sudo apt install -y apache2 mysql-server php php-mysql libapache2-mod-php php-cli unzip wget certbot python3-certbot-apache

# Download and extract the latest WordPress
wget https://wordpress.org/latest.zip
unzip latest.zip
sudo mv wordpress /var/www/html/$FOLDER_NAME

# Set permissions
sudo chown -R www-data:www-data /var/www/html/$FOLDER_NAME
sudo chmod -R 755 /var/www/html/$FOLDER_NAME

# Create a MySQL database and user for WordPress
sudo mysql -e "CREATE DATABASE $DB_NAME;"
sudo mysql -e "CREATE USER '$DB_USER'@'localhost' IDENTIFIED BY '$DB_PASSWORD';"
sudo mysql -e "GRANT ALL PRIVILEGES ON $DB_NAME.* TO '$DB_USER'@'localhost';"
sudo mysql -e "FLUSH PRIVILEGES;"

# Configure Apache virtual host
sudo bash -c "cat <<EOF > /etc/apache2/sites-available/$FOLDER_NAME.conf
<VirtualHost *:80>
    ServerAdmin admin@$DOMAIN_NAME
    DocumentRoot /var/www/html/$FOLDER_NAME
    ServerName $DOMAIN_NAME
    ServerAlias www.$DOMAIN_NAME

    <Directory /var/www/html/$FOLDER_NAME>
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog \${APACHE_LOG_DIR}/error.log
    CustomLog \${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
EOF"

# Enable the WordPress site and rewrite module
sudo a2ensite $FOLDER_NAME.conf
sudo a2enmod rewrite
sudo systemctl restart apache2

# Obtain SSL certificate and configure auto-renewal
sudo certbot --apache -d $DOMAIN_NAME -d www.$DOMAIN_NAME

# Set up auto-renewal
sudo systemctl enable certbot.timer

Choose from apache or nginx

#!/bin/bash

# Variables
FOLDER_NAME=${1:-wordpress}
DB_NAME=${2:-wordpress}
DB_USER=${3:-wordpressuser}
DB_PASSWORD=${4:-password}
DOMAIN_NAME=${5:-example.com}
WEB_SERVER=${6:-apache} # Choose between 'apache' or 'nginx'

# Update and install necessary packages
sudo apt update
sudo apt install -y mysql-server php php-mysql php-cli unzip wget certbot

if [ "$WEB_SERVER" == "apache" ]; then
    sudo apt install -y apache2 libapache2-mod-php python3-certbot-apache
elif [ "$WEB_SERVER" == "nginx" ]; then
    sudo apt install -y nginx php-fpm python3-certbot-nginx
else
    echo "Invalid web server choice. Please choose 'apache' or 'nginx'."
    exit 1
fi

# Download and extract the latest WordPress
wget https://wordpress.org/latest.zip
unzip latest.zip
sudo mv wordpress /var/www/html/$FOLDER_NAME

# Set permissions
sudo chown -R www-data:www-data /var/www/html/$FOLDER_NAME
sudo chmod -R 755 /var/www/html/$FOLDER_NAME

# Create a MySQL database and user for WordPress
sudo mysql -e "CREATE DATABASE $DB_NAME;"
sudo mysql -e "CREATE USER '$DB_USER'@'localhost' IDENTIFIED BY '$DB_PASSWORD';"
sudo mysql -e "GRANT ALL PRIVILEGES ON $DB_NAME.* TO '$DB_USER'@'localhost';"
sudo mysql -e "FLUSH PRIVILEGES;"

if [ "$WEB_SERVER" == "apache" ]; then
    # Configure Apache virtual host
    sudo bash -c "cat <<EOF > /etc/apache2/sites-available/$FOLDER_NAME.conf
<VirtualHost *:80>
    ServerAdmin admin@$DOMAIN_NAME
    DocumentRoot /var/www/html/$FOLDER_NAME
    ServerName $DOMAIN_NAME
    ServerAlias www.$DOMAIN_NAME

    <Directory /var/www/html/$FOLDER_NAME>
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog \${APACHE_LOG_DIR}/error.log
    CustomLog \${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
EOF"

    # Enable the WordPress site and rewrite module
    sudo a2ensite $FOLDER_NAME.conf
    sudo a2enmod rewrite
    sudo systemctl restart apache2

    # Obtain SSL certificate and configure auto-renewal
    sudo certbot --apache -d $DOMAIN_NAME -d www.$DOMAIN_NAME
elif [ "$WEB_SERVER" == "nginx" ]; then
    # Configure Nginx server block
    sudo bash -c "cat <<EOF > /etc/nginx/sites-available/$FOLDER_NAME
server {
    listen 80;
    server_name $DOMAIN_NAME www.$DOMAIN_NAME;

    root /var/www/html/$FOLDER_NAME;
    index index.php index.html index.htm;

    location / {
        try_files \$uri \$uri/ /index.php?\$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }

    error_log /var/log/nginx/$FOLDER_NAME.error.log;
    access_log /var/log/nginx/$FOLDER_NAME.access.log;
}
EOF"

    # Enable the WordPress site
    sudo ln -s /etc/nginx/sites-available/$FOLDER_NAME /etc/nginx/sites-enabled/
    sudo systemctl restart nginx

    # Obtain SSL certificate and configure auto-renewal
    sudo certbot --nginx -d $DOMAIN_NAME -d www.$DOMAIN_NAME
fi

# Set up auto-renewal
sudo systemctl enable certbot.timer