dotlinux blog

WordPress Installation on Ubuntu Linux with Apache and MySQL

WordPress is the world’s most popular content management system (CMS), powering over 40% of websites globally. Its flexibility, ease of use, and extensive plugin ecosystem make it ideal for blogs, e-commerce sites, portfolios, and more. While managed WordPress hosting is available, self-hosting on a Linux server gives you full control over your environment, security, and customization.

In this guide, we’ll walk through installing WordPress on an Ubuntu Linux server using Apache (the most widely used web server), MySQL (a robust relational database), and PHP (the scripting language WordPress is built on). This stack—Apache, MySQL, PHP—is commonly referred to as LAMP. By the end, you’ll have a fully functional WordPress site ready to customize.

2026-01

Table of Contents#

  1. Prerequisites
  2. Step 1: Update System Packages
  3. Step 2: Install Apache Web Server
  4. Step 3: Install MySQL Database Server
  5. Step 4: Create a MySQL Database and User for WordPress
  6. Step 5: Install PHP and Required Extensions
  7. Step 6: Download and Configure WordPress
  8. Step 7: Set Up Apache Virtual Host
  9. Step 8: Finalize WordPress Installation via Web Interface
  10. Troubleshooting Common Issues
  11. Conclusion
  12. References

Prerequisites#

Before starting, ensure you have:

  • An Ubuntu Linux server (20.04 LTS or later recommended; tested on 22.04 LTS).
  • Sudo privileges (to install packages and modify system settings).
  • A stable internet connection.
  • (Optional) A registered domain name pointing to your server’s IP (for production use).
  • Basic familiarity with the Linux command line.

Step 1: Update System Packages#

First, update your server’s package list and upgrade existing packages to their latest versions. This ensures compatibility and security:

sudo apt update && sudo apt upgrade -y
  • apt update: Refreshes the list of available packages.
  • apt upgrade -y: Upgrades installed packages (-y auto-confirms prompts).

Step 2: Install Apache Web Server#

Apache is the backbone of your WordPress site, handling HTTP requests and serving content. Install it with:

sudo apt install apache2 -y

Verify Apache Installation#

Check if Apache is running:

sudo systemctl status apache2

You should see active (running) in the output.

Allow Apache Through the Firewall#

If you’re using ufw (Ubuntu’s default firewall), allow HTTP (port 80) and HTTPS (port 443) traffic:

sudo ufw allow 'Apache Full'  # Allows both HTTP and HTTPS
sudo ufw reload

Test Apache by visiting your server’s IP address in a browser (e.g., http://your_server_ip). You’ll see the default Apache welcome page.

Step 3: Install MySQL Database Server#

WordPress stores content (posts, users, settings) in a database. MySQL is a popular choice for this. Install MySQL Server:

sudo apt install mysql-server -y

Secure MySQL Installation#

MySQL comes with a security script to harden your database. Run it:

sudo mysql_secure_installation

Follow the prompts:

  • Set a root password: Choose a strong password (store it securely!).
  • Remove anonymous users: Select Y.
  • Disallow root login remotely: Select Y (root should only log in locally).
  • Remove test database: Select Y.
  • Reload privilege tables: Select Y.

Step 4: Create a MySQL Database and User for WordPress#

WordPress needs a dedicated database and user (avoid using the MySQL root user for security).

Log Into MySQL#

Access the MySQL shell as the root user:

sudo mysql -u root -p

Enter the root password you set earlier.

Create a Database and User#

Run these commands in the MySQL shell (replace wp_db, wp_user, and strong_password with your values):

-- Create a database for WordPress
CREATE DATABASE wp_db DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
 
-- Create a user and grant privileges
CREATE USER 'wp_user'@'localhost' IDENTIFIED BY 'strong_password';
GRANT ALL PRIVILEGES ON wp_db.* TO 'wp_user'@'localhost';
 
-- Flush privileges to apply changes
FLUSH PRIVILEGES;
 
-- Exit the MySQL shell
EXIT;
  • utf8mb4: Supports all Unicode characters, including emojis.
  • 'wp_user'@'localhost': The user can only connect from the local server (safer than remote access).

Step 5: Install PHP and Required Extensions#

WordPress is written in PHP, so we need PHP and extensions like php-mysql (for database connectivity) and php-curl (for plugin/theme updates).

Install PHP 8.1 (recommended for WordPress) and extensions:

sudo apt install php libapache2-mod-php php-mysql php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip -y

Verify PHP Installation#

Create a test PHP file to confirm PHP is working with Apache:

sudo nano /var/www/html/info.php

Paste this code:

<?php phpinfo(); ?>

Save (Ctrl+O, Enter) and exit (Ctrl+X).

Visit http://your_server_ip/info.php in a browser. You’ll see a PHP info page with details about your PHP configuration.

Important: Delete the test file after verification (it exposes server details):

sudo rm /var/www/html/info.php

Step 6: Download and Configure WordPress#

Download WordPress#

WordPress is updated frequently, so download the latest version from the official site:

cd /tmp  # Temporary directory for downloads
wget https://wordpress.org/latest.tar.gz

Extract the archive:

tar -xvzf latest.tar.gz

Move WordPress to Apache’s Web Root#

Apache serves files from /var/www/html/ by default. Move the extracted WordPress folder there:

sudo mv /tmp/wordpress /var/www/html/

Set Proper Permissions#

WordPress needs write access to its files/directories for updates, plugins, and media uploads. Set ownership to the www-data user (Apache’s runtime user):

sudo chown -R www-data:www-data /var/www/html/wordpress/
sudo chmod -R 755 /var/www/html/wordpress/  # Folders: read/write/execute for owner, read/execute for others

Step 7: Configure WordPress#

Create the WordPress Configuration File#

WordPress uses wp-config.php to connect to the database. Rename the sample config file:

cd /var/www/html/wordpress/
sudo cp wp-config-sample.php wp-config.php

Edit wp-config.php with your database details:

sudo nano wp-config.php

Update these lines with your MySQL database name, user, and password:

define( 'DB_NAME', 'wp_db' );         // Replace with your database name
define( 'DB_USER', 'wp_user' );       // Replace with your MySQL user
define( 'DB_PASSWORD', 'strong_password' );  // Replace with your user's password
define( 'DB_HOST', 'localhost' );     // Leave as localhost

Add Secure Keys#

WordPress uses security keys to encrypt cookies and data. Replace the default placeholder keys with unique ones. Visit WordPress Secret Key Generator to generate keys, then paste them into wp-config.php (replace the AUTH_KEY, SECURE_AUTH_KEY, etc., lines).

If you’re hosting multiple sites or want a custom domain, configure an Apache Virtual Host. Skip this if using the default /var/www/html directory.

Create a Virtual Host File#

sudo nano /etc/apache2/sites-available/wordpress.conf

Paste this configuration (replace yourdomain.com with your domain/IP):

<VirtualHost *:80>
    ServerName yourdomain.com       # Your domain or server IP
    ServerAlias www.yourdomain.com  # Optional: www subdomain
    DocumentRoot /var/www/html/wordpress
 
    <Directory /var/www/html/wordpress>
        Options Indexes FollowSymLinks
        AllowOverride All           # Enables WordPress permalinks
        Require all granted
    </Directory>
 
    ErrorLog ${APACHE_LOG_DIR}/wordpress_error.log
    CustomLog ${APACHE_LOG_DIR}/wordpress_access.log combined
</VirtualHost>

Enable the Virtual Host#

sudo a2ensite wordpress.conf  # Enable the site
sudo a2enmod rewrite          # Enable URL rewriting (required for permalinks)
sudo systemctl restart apache2  # Restart Apache to apply changes

Disable the default Apache site if needed:

sudo a2dissite 000-default.conf

Step 9: Finalize WordPress Installation via Web Interface#

Now, complete the setup through your browser:

  1. Visit your site: http://yourdomain.com or http://your_server_ip/wordpress.
  2. Select your language and click Continue.
  3. Enter your site details:
    • Site Title: e.g., “My Blog”
    • Username: Choose a secure admin username (avoid “admin”).
    • Password: Use a strong password (generate one with a password manager).
    • Your Email: For notifications and password resets.
  4. Click Install WordPress.

Log in with your admin credentials, and you’ll land on the WordPress dashboard!

Troubleshooting Common Issues#

  • Permission Errors: If WordPress can’t write files, recheck ownership:
    sudo chown -R www-data:www-data /var/www/html/wordpress/
  • PHP Extensions Missing: Check the PHP info page (recreate info.php temporarily) for missing extensions. Install them with sudo apt install php-extension-name.
  • MySQL Connection Issues: Verify database credentials in wp-config.php. Test the connection with:
    mysql -u wp_user -p wp_db  # Enter the user's password when prompted
  • Apache Not Starting: Check logs:
    sudo tail -f /var/log/apache2/error.log

Conclusion#

You’ve successfully installed WordPress on Ubuntu with Apache and MySQL! You now have a self-hosted WordPress site with full control over its configuration. Explore the dashboard to install themes, plugins, and start creating content. Remember to keep your server, WordPress, and plugins updated for security.

References#