Last active
August 12, 2025 15:53
-
-
Save raghavmri/a07a4207d382a500486db0c54eb78d4c 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 | |
# ============================================================================== | |
# Configuration Variables - Edit these values as needed | |
# ============================================================================== | |
DB_NAME="wordpress" | |
DB_USER="wordpressuser" | |
DB_PASSWORD="your_strong_password" | |
WP_URL="https://wordpress.org/latest.tar.gz" | |
WP_DIR="/var/www/html" | |
# ============================================================================== | |
# Update system packages | |
echo "--- Updating system packages ---" | |
sudo apt update -y | |
sudo apt upgrade -y | |
# Install Apache | |
echo "--- Installing Apache2 ---" | |
sudo apt install -y apache2 | |
# Install MySQL and secure installation (non-interactive) | |
echo "--- Installing MySQL Server and securing it ---" | |
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password password $DB_PASSWORD" | |
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password_again password $DB_PASSWORD" | |
sudo apt install -y mysql-server | |
sudo mysql_secure_installation --use-default --password=$DB_PASSWORD | |
# Install PHP and required modules | |
echo "--- Installing PHP and required modules ---" | |
sudo apt install -y php libapache2-mod-php php-mysql php-cli php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip | |
# Restart Apache to load PHP module | |
echo "--- Restarting Apache2 ---" | |
sudo systemctl restart apache2 | |
# Create MySQL database and user for WordPress | |
echo "--- Creating MySQL database and user ---" | |
sudo mysql -u root -p"$DB_PASSWORD" <<EOF | |
CREATE DATABASE $DB_NAME; | |
CREATE USER '$DB_USER'@'localhost' IDENTIFIED BY '$DB_PASSWORD'; | |
GRANT ALL PRIVILEGES ON $DB_NAME.* TO '$DB_USER'@'localhost'; | |
FLUSH PRIVILEGES; | |
EOF | |
# Download, extract, and configure WordPress | |
echo "--- Downloading and configuring WordPress ---" | |
cd /tmp | |
wget $WP_URL | |
tar -xzvf latest.tar.gz | |
sudo rsync -avP /tmp/wordpress/ "$WP_DIR/" | |
sudo chown -R www-data:www-data "$WP_DIR/" | |
sudo find "$WP_DIR/" -type d -exec chmod 755 {} \; | |
sudo find "$WP_DIR/" -type f -exec chmod 644 {} \; | |
# Create wp-config.php and replace placeholders | |
echo "--- Generating wp-config.php file ---" | |
cd "$WP_DIR/" | |
sudo cp wp-config-sample.php wp-config.php | |
sudo sed -i "s/database_name_here/$DB_NAME/g" wp-config.php | |
sudo sed -i "s/username_here/$DB_USER/g" wp-config.php | |
sudo sed -i "s/password_here/$DB_PASSWORD/g" wp-config.php | |
# ------------------------------------------------------------------------------ | |
# NEW: Configure Apache to serve WordPress | |
# ------------------------------------------------------------------------------ | |
echo "--- Configuring Apache Virtual Host for WordPress ---" | |
# Create a new Apache configuration file for WordPress | |
# Note: You can customize the file name if you want, e.g., yourdomain.conf | |
APACHE_CONF_FILE="wordpress.conf" | |
sudo bash -c "cat > /etc/apache2/sites-available/$APACHE_CONF_FILE" <<EOF | |
<VirtualHost *:80> | |
ServerAdmin webmaster@localhost | |
DocumentRoot $WP_DIR | |
ErrorLog \${APACHE_LOG_DIR}/error.log | |
CustomLog \${APACHE_LOG_DIR}/access.log combined | |
<Directory $WP_DIR> | |
Options Indexes FollowSymLinks | |
AllowOverride All | |
Require all granted | |
</Directory> | |
</VirtualHost> | |
EOF | |
# Disable the default Apache site | |
sudo a2dissite 000-default.conf | |
# Enable the new WordPress site | |
sudo a2ensite "$APACHE_CONF_FILE" | |
# Enable the Apache Rewrite module, which is required for WordPress permalinks | |
sudo a2enmod rewrite | |
# Restart Apache to apply all new changes | |
echo "--- Restarting Apache2 to apply new configuration ---" | |
sudo systemctl restart apache2 | |
# ------------------------------------------------------------------------------ | |
# END of new code block | |
# ------------------------------------------------------------------------------ | |
echo "--- Installation complete! ---" | |
echo "You can now navigate to your server's IP address in a web browser to complete the WordPress setup." | |
echo "Your database credentials are: DB_NAME=$DB_NAME, DB_USER=$DB_USER, DB_PASSWORD=$DB_PASSWORD" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment