How to Install WordPress on Ubuntu 24.04

Installing Ubuntu on your server can give you greater control and customization for your WordPress site. Let's dive into the process, ensuring your site runs smoothly and efficiently
Here are the steps for Installing WordPress on Ubuntu 24.04
Step 1: Update the server
sudo apt update
This command updates the list of available packages and their versions from the Ubuntu repositories.
Step 2: Install nginx server
sudo apt install nginx -y
Nginx will serve up your WordPress site to users. The -y flag just says "yes" to all prompts, making the installation quicker.
Step 3: Install Mysql
sudo apt install mysql-server mysql-client -y
Installs the MySQL database server and client.
WordPress requires a database to store content, user information, settings, and more. MySQL is one of the most popular database systems used with WordPress.
Step 4: Install PHP and Required Extensions
sudo apt install php-fpm php-mysql php-xml php-mbstring php-curl php-zip php-gd php-imagick -y
Installs PHP and the necessary PHP extensions for WordPress.
PHP is the scripting language that WordPress uses. The extensions add extra features needed by WordPress, like database interaction (php-mysql), image processing (php-imagick), and handling form data (php-mbstring).
Step 5: Create a Database for WordPress
sudo mysql -u root -p
Creates a new MySQL database and user for WordPress.

WordPress needs its own database to store content. Creating a separate user for WordPress improves security by limiting access to only the WordPress database.
Step 6: Download and extract latest version of WordPress
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -xvzf latest.tar.gz
This is the core software needed to run your WordPress website.
Step 7: Move WordPress Files
sudo mv wordpress /var/www/mywordpresssite
Moves the extracted WordPress files to the web root directory.
Nginx needs to know where the website files are. By moving them to /var/www/mywordpresssite, you set the document root where Nginx will find the files to serve.
Step 8: Configure WordPress
cd /var/www/mywordpresssite
sudo cp wp-config-sample.php wp-config.php
wp-config.php file

Configures the WordPress settings to connect to the MySQL database.
WordPress needs to know the database name, username, password, and host to store and retrieve content.
Step 9: Set Up Correct Permissions
sudo chown -R www-data:www-data /var/www/mywordpresssite
sudo find /var/www/mywordpresssite/ -type d -exec chmod 750 {} \;
sudo find /var/www/mywordpresssite/ -type f -exec chmod 640 {} \;
Sets the appropriate file and directory permissions for WordPress.
Ensures that the web server (Nginx) can read and write files as necessary, while limiting access to other users on the server, enhancing security.
Step 10: Configure Nginx
Configures Nginx to serve your WordPress site.
sudo nano /etc/nginx/sites-available/mywordpresssite

It tells Nginx where to find your WordPress files and how to handle PHP requests.
Step 11: Enable the Site and Test Nginx Configuration
sudo ln -s /etc/nginx/sites-available/mywordpresssite /etc/nginx/sites-enabled/ sudo nginx -t
Creates a symbolic link to enable the Nginx site configuration and tests the Nginx configuration for errors.
Activates your site and ensures that Nginx is correctly configured before reloading.
Step 12: Reload Nginx
Reloads Nginx to apply the new configuration.
sudo systemctl reload nginx
Step 13: Complete the WordPress Installation
Access WordPress: Open your web browser and go to http://your_public_ip.




Automating the installation using bash script
This script automates the process, including downloading WordPress, setting up the database, configuring Nginx, and adjusting file permissions. You only need to run it, and the setup will be ready for you to complete through the WordPress web interface.
./install_wordpress.sh
#!/bin/bash
# Update package list and install necessary packages
sudo apt update
sudo apt install -y nginx mysql-server mysql-client php-fpm php-mysql php-xml
php-mbstring php-curl php-zip php-gd php-imagick wget unzip
# Secure MySQL installation (Optional)
# Uncomment the following line if you want to run the MySQL secure installation
interactively
# sudo mysql_secure_installation
# Create a MySQL database and user for WordPress
DB_NAME="wordpress_db"
DB_USER="wp_user"
DB_PASSWORD="password123"
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;"
# Download and extract WordPress
cd /var/www/
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -xvzf latest.tar.gz
sudo mv wordpress mywordpresssite
# Configure WordPress
cd /var/www/mywordpresssite
sudo cp wp-config-sample.php wp-config.php
sudo sed -i "s/database_name_here/${DB_NAME}/" wp-config.php
sudo sed -i "s/username_here/${DB_USER}/" wp-config.php
sudo sed -i "s/password_here/${DB_PASSWORD}/" wp-config.php
# Set correct permissions
sudo chown -R www-data:www-data /var/www/mywordpresssite
sudo find /var/www/mywordpresssite/ -type d -exec chmod 750 {} \;
sudo find /var/www/mywordpresssite/ -type f -exec chmod 640 {} \;
# Configure Nginx
NGINX_CONF="/etc/nginx/sites-available/mywordpresssite"
sudo bash -c "cat > ${NGINX_CONF}" <<EOF
server {
listen 8080;
server_name your_public_ip;
root /var/www/mywordpresssite;
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/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
EOF
# Replace 'your_public_ip' with the server's public IP
PUBLIC_IP=$(curl -s ifconfig.me)
sudo sed -i "s/your_public_ip/${PUBLIC_IP}/" ${NGINX_CONF}
# Enable the Nginx site and reload the configuration
sudo ln -s /etc/nginx/sites-available/mywordpresssite /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
# Output completion message
echo "WordPress has been installed. Open http://${PUBLIC_IP} in your browser
to complete the setup."
Follow me here: @dity@
\======================================================================================================================================

