Table of Contents#
- Prerequisites
- Setting Up the LAMP Stack
- Creating a MySQL Database and User for WordPress
- Installing WordPress
- Configuring Apache for WordPress
- Completing the WordPress Setup via Browser
- Testing the Installation
- Troubleshooting Common Issues
- Conclusion
- References
Prerequisites#
Before starting, ensure you have:
- An Ubuntu 24.04 LTS server (physical, virtual, or cloud-based, e.g., AWS EC2, DigitalOcean Droplet).
- A user account with
sudoprivileges (to run administrative commands). - A stable internet connection (to download packages).
- (Optional) A domain name pointing to your server’s IP (for production; skip for local testing).
Setting Up the LAMP Stack#
A LAMP stack is the foundation for hosting WordPress. Let’s install each component step-by-step.
Step 1: Install Apache Web Server#
Apache is a popular open-source web server that handles HTTP requests and serves web content.
-
Update your system packages (always a good first step to ensure you have the latest software):
sudo apt update && sudo apt upgrade -y -
Install Apache:
sudo apt install apache2 -y -
Verify Apache is running:
sudo systemctl status apache2You should see
active (running)in the output. -
Enable Apache to start on boot:
sudo systemctl enable apache2 -
Configure the firewall (if using
ufw, Ubuntu’s default firewall) to allow HTTP (port 80) and HTTPS (port 443) traffic:sudo ufw allow 'Apache Full'Verify the rule with:
sudo ufw status -
Test Apache: Open a browser and navigate to your server’s public IP (e.g.,
http://your-server-ip). You’ll see Apache’s default Ubuntu page, confirming Apache is working.
Step 2: Install MySQL Database Server#
MySQL is a relational database management system (RDBMS) that stores WordPress data (posts, users, settings).
-
Install MySQL Server:
sudo apt install mysql-server -y -
Verify MySQL is running:
sudo systemctl status mysqlOutput should show
active (running). -
Secure the MySQL installation (critical for security):
Run the built-in security script to set a root password, remove anonymous users, and disable remote root login:sudo mysql_secure_installationFollow the prompts:
- Press
Yto enable the Validate Password Plugin (recommended). - Set a strong root password (store this securely!).
- Answer
Yto all remaining questions (remove anonymous users, disallow remote root login, remove test database, reload privileges).
- Press
-
Test MySQL access: Log into the MySQL shell as the root user:
sudo mysql -u root -pEnter the root password you set earlier. You’ll see the MySQL prompt (
mysql>), confirming access. Exit with:exit;
Step 3: Install PHP and Required Extensions#
PHP is a server-side scripting language that processes dynamic content (e.g., WordPress themes/plugins). WordPress requires PHP 7.4 or higher; Ubuntu 24.04 includes PHP 8.3 by default (fully compatible).
-
Install PHP and essential modules (including MySQL support and WordPress dependencies):
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 -ylibapache2-mod-php: Connects Apache to PHP.php-mysql: Allows PHP to communicate with MySQL.- Other modules: Required for WordPress features (e.g., image uploads, XML-RPC, internationalization).
-
Verify PHP installation:
Create a test PHP file in Apache’s web root (/var/www/html):sudo nano /var/www/html/info.phpAdd this code:
<?php phpinfo(); ?>Save and exit (
Ctrl+O,Enter,Ctrl+X). -
Test PHP: In a browser, visit
http://your-server-ip/info.php. You’ll see a PHP info page with details about your PHP configuration.Note: Delete
info.phpafter testing (it exposes sensitive server info):sudo rm /var/www/html/info.php -
Restart Apache to apply PHP changes:
sudo systemctl restart apache2
Creating a MySQL Database and User for WordPress#
WordPress needs a dedicated database and user to store its data. Using the root MySQL user is insecure, so we’ll create a limited-privilege user.
-
Log into MySQL as root:
sudo mysql -u root -p -
Create a WordPress database (replace
wordpress_dbwith your preferred name):CREATE DATABASE wordpress_db DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;utf8mb4supports all Unicode characters, including emojis.
-
Create a MySQL user (replace
wp_userandstrong_passwordwith your own):CREATE USER 'wp_user'@'localhost' IDENTIFIED BY 'strong_password'; -
Grant privileges to the user on the WordPress database:
GRANT ALL PRIVILEGES ON wordpress_db.* TO 'wp_user'@'localhost'; -
Flush privileges to apply changes:
FLUSH PRIVILEGES; -
Exit MySQL:
exit;
Installing WordPress#
Now, download and set up the WordPress files.
-
Navigate to Apache’s web root (or a subdirectory; we’ll use
/var/www/wordpressfor organization):cd /var/www -
Download the latest WordPress tarball from wordpress.org:
sudo wget https://wordpress.org/latest.tar.gz -
Extract the tarball:
sudo tar -xvzf latest.tar.gzThis creates a
wordpressdirectory with all core files. -
Set file ownership (critical for Apache to read/write WordPress files):
Apache runs as thewww-datauser, so set ownership towww-data:sudo chown -R www-data:www-data /var/www/wordpress/ -
Set file permissions (secure but accessible):
Directories should be755(read/execute for all, write for owner), files644(read for all, write for owner):sudo find /var/www/wordpress/ -type d -exec chmod 755 {} \; sudo find /var/www/wordpress/ -type f -exec chmod 644 {} \; -
Remove the downloaded tarball (cleanup):
sudo rm latest.tar.gz
Configuring Apache for WordPress#
To serve WordPress, we’ll create an Apache virtual host (VH) file. Virtual hosts let you host multiple sites on one server (optional but recommended for organization).
-
Create a virtual host file for WordPress:
sudo nano /etc/apache2/sites-available/wordpress.conf -
Add the following configuration (replace
your-domain.comwith your domain or server IP):<VirtualHost *:80> ServerAdmin [email protected] DocumentRoot /var/www/wordpress ServerName your-domain.com ServerAlias www.your-domain.com <Directory /var/www/wordpress> AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/wordpress_error.log CustomLog ${APACHE_LOG_DIR}/wordpress_access.log combined </VirtualHost>AllowOverride All: Enables.htaccessfiles (required for WordPress permalinks).ServerName/ServerAlias: Replace with your domain (or skip for local testing, usinglocalhost).
-
Save and exit (
Ctrl+O,Enter,Ctrl+X). -
Enable the virtual host and disable Apache’s default site:
sudo a2ensite wordpress.conf sudo a2dissite 000-default.conf -
Enable the
rewritemodule (required for WordPress permalinks):sudo a2enmod rewrite -
Test Apache configuration for syntax errors:
sudo apache2ctl configtestIf you see
Syntax OK, proceed. -
Restart Apache to apply changes:
sudo systemctl restart apache2
Completing the WordPress Setup via Browser#
Now, finalize WordPress installation through your browser.
-
Access the WordPress setup page:
Open a browser and navigate to your server’s IP or domain (e.g.,http://your-domain.comorhttp://localhostif testing locally). -
Select your language and click “Continue.”
-
Enter your database details:
- Database Name:
wordpress_db(the database you created). - Username:
wp_user(the MySQL user you created). - Password:
strong_password(the user’s password). - Database Host:
localhost(default for local MySQL). - Table Prefix:
wp_(default; change for security if desired).
Click “Submit.”
- Database Name:
-
Run the installation:
Click “Run the Installation” to proceed. -
Set up your WordPress site:
- Site Title: Your site’s name (e.g., “My Tech Blog”).
- Username: Choose an admin username (avoid “admin” for security).
- Password: Use a strong, unique password (store it securely!).
- Your Email: Admin email for notifications.
- Search Engine Visibility: Check “Discourage search engines from indexing this site” if testing (uncheck later for production).
Click “Install WordPress.”
-
Log in:
After installation, click “Log In” and enter your admin credentials. You’ll land on the WordPress Dashboard—your site is ready!
Testing the Installation#
Verify your WordPress site works:
- Visit the front end:
http://your-domain.com(you’ll see the default WordPress theme). - Create a test post: Go to Posts > Add New, write a title/content, and click “Publish.” View it on the front end to confirm it loads.
- Test permalinks: Go to Settings > Permalinks, select “Post name,” and save changes. Visit a post to ensure the URL (e.g.,
http://your-domain.com/test-post) works (no 404 errors).
Troubleshooting Common Issues#
1. Apache Fails to Restart#
- Check for syntax errors:
sudo apache2ctl configtest. - Ensure no other service uses port 80:
sudo lsof -i :80.
2. Database Connection Error#
- Verify MySQL credentials in
wp-config.php(located in/var/www/wordpress). - Ensure the MySQL user has privileges:
sudo mysql -u root -p -e "SHOW GRANTS FOR 'wp_user'@'localhost';"
3. WordPress Can’t Write Files (e.g., Plugins/Themes)#
- Confirm file ownership:
sudo chown -R www-data:www-data /var/www/wordpress/. - Check permissions:
sudo ls -la /var/www/wordpress/(directories should bedrwxr-xr-x, files-rw-r--r--).
4. PHP Modules Missing#
- Reinstall PHP modules:
sudo apt install php-curl php-gd ...(repeat the PHP install command).
Conclusion#
You’ve successfully installed WordPress on a LAMP stack in Ubuntu 24.04! You now have a secure, self-hosted WordPress site ready for customization. Next steps:
- Install a theme (Appearance > Themes).
- Add plugins (Plugins > Add New).
- Configure HTTPS with Let’s Encrypt (use
certbotfor free SSL). - Back up your site regularly (e.g., with the UpdraftPlus plugin).