Install Wordpress on Ubuntu 24 04 LTS [Updated 2025]


 

#!/bin/bash

# WordPress Installation Script for Ubuntu 24.04
# This script installs WordPress with Apache, MySQL, and PHP (LAMP stack)

# Check if the script is run as root
if [ "$(id -u)" -ne 0 ]; then
    echo "This script must be run as root. Use sudo." >&2
    exit 1
fi

# Update system packages
echo "Updating system packages..."
apt update && apt upgrade -y

# Install Apache
echo "Installing Apache..."
apt install -y apache2

# Enable Apache modules
a2enmod rewrite
systemctl restart apache2

# Install MySQL
echo "Installing MySQL..."
apt install -y mysql-server

# Run MySQL secure installation (automated version)
echo "Configuring MySQL security..."
mysql_secure_installation <<EOF
n
n
y
y
y
y
EOF

# Install PHP and required extensions
echo "Installing PHP and extensions..."
apt install -y php libapache2-mod-php php-mysql php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip

# Restart Apache to load PHP module
systemctl restart apache2

# Create MySQL database for WordPress
echo "Creating MySQL database for WordPress..."
read -p "Enter a name for the WordPress database: " dbname
read -p "Enter a username for the WordPress database: " dbuser
read -sp "Enter a password for the WordPress database user: " dbpass
echo ""

mysql -e "CREATE DATABASE ${dbname} DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;"
mysql -e "CREATE USER '${dbuser}'@'localhost' IDENTIFIED BY '${dbpass}';"
mysql -e "GRANT ALL ON ${dbname}.* TO '${dbuser}'@'localhost';"
mysql -e "FLUSH PRIVILEGES;"

# Download and install WordPress
echo "Downloading WordPress..."
cd /tmp || exit
wget https://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gz

# Move WordPress to web directory
echo "Installing WordPress..."
mv wordpress /var/www/html/
chown -R www-data:www-data /var/www/html/wordpress
chmod -R 755 /var/www/html/wordpress

# Create WordPress config file from sample
cp /var/www/html/wordpress/wp-config-sample.php /var/www/html/wordpress/wp-config.php

# Set database credentials in wp-config.php
sed -i "s/database_name_here/${dbname}/" /var/www/html/wordpress/wp-config.php
sed -i "s/username_here/${dbuser}/" /var/www/html/wordpress/wp-config.php
sed -i "s/password_here/${dbpass}/" /var/www/html/wordpress/wp-config.php

# Generate and set WordPress security keys
SALT=$(curl -s https://api.wordpress.org/secret-key/1.1/salt/)
printf '%s\n' "g/put your unique phrase here/d" a "$SALT" . w | ed -s /var/www/html/wordpress/wp-config.php

# Configure Apache virtual host
echo "Configuring Apache virtual host..."
cat > /etc/apache2/sites-available/wordpress.conf <<EOF
<VirtualHost *:80>
    ServerAdmin admin@example.com
    DocumentRoot /var/www/html/wordpress
    ServerName example.com
    ServerAlias www.example.com

    <Directory /var/www/html/wordpress>
        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 virtual host and disable the default
a2ensite wordpress
a2dissite 000-default
systemctl restart apache2

# Install Certbot for Let's Encrypt SSL (optional)
read -p "Do you want to install Let's Encrypt SSL certificate? (y/n): " install_ssl
if [ "$install_ssl" = "y" ] || [ "$install_ssl" = "Y" ]; then
    apt install -y certbot python3-certbot-apache
    read -p "Enter your domain name (example.com): " domain_name
    certbot --apache -d $domain_name -d www.$domain_name
    systemctl restart apache2
fi

echo "WordPress installation complete!"
echo "You can access your WordPress site at:"
echo "http://$(hostname -I | cut -d' ' -f1)/wordpress"
echo "or http://your-domain.com if you configured DNS and SSL"
echo "Follow the WordPress setup wizard to complete the installation."

Comments