Table of Contents#
- Prerequisites
- Step 1: Update System Packages
- Step 2: Install the LAMP Stack
- Step 3: Create a MySQL Database and User for WordPress
- Step 4: Download and Configure WordPress
- Step 5: Configure Apache for WordPress
- Step 6: Complete WordPress Installation via Web Interface
- Troubleshooting Common Issues
- Conclusion
- References
Prerequisites#
Before starting, ensure you have:
- An Ubuntu 18.04 LTS server (physical, virtual, or cloud-based, e.g., AWS EC2, DigitalOcean Droplet).
- A non-root user with
sudoprivileges (see Ubuntu’s guide to creating a sudo user). - A stable internet connection.
- (Optional) A registered domain name pointing to your server’s IP address (for production use).
Step 1: Update System Packages#
First, update your system’s package index and upgrade existing packages to their latest versions. This ensures compatibility and security:
sudo apt update && sudo apt upgrade -yIf prompted, confirm package upgrades by pressing Y.
Step 2: Install the LAMP Stack#
WordPress requires a web server (Apache), database (MySQL), and PHP to run. We’ll install these components (the LAMP stack) first.
2.1 Install Apache Web Server#
Apache is the most popular web server. Install it with:
sudo apt install apache2 -yVerify Apache is running:
sudo systemctl status apache2You should see active (running) in the output.
Next, configure the firewall (ufw) to allow HTTP (port 80) and HTTPS (port 443) traffic. Ubuntu 18.04 uses ufw by default.
Check firewall status:
sudo ufw statusIf inactive, enable it:
sudo ufw enableAllow Apache traffic:
sudo ufw allow 'Apache Full' # Allows both HTTP (80) and HTTPS (443)Verify the rule:
sudo ufw statusYou should see Apache Full listed under allowed services.
2.2 Install MySQL Database Server#
MySQL stores WordPress data (posts, users, settings). Install MySQL Server:
sudo apt install mysql-server -yAfter installation, secure MySQL with the mysql_secure_installation script. This removes default vulnerabilities:
sudo mysql_secure_installationFollow the prompts:
- Set a root password: Choose a strong password and confirm it.
- Remove anonymous users: Press
Y. - Disallow root login remotely: Press
Y. - Remove test database: Press
Y. - Reload privilege tables: Press
Y.
2.3 Install PHP and Extensions#
WordPress is built on PHP. Install PHP and required extensions (e.g., php-mysql for MySQL integration):
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 -yVerify PHP installation by creating a test file in Apache’s web root (/var/www/html):
sudo nano /var/www/html/info.phpAdd this PHP code:
<?php phpinfo(); ?>Save (Ctrl+O, Enter) and exit (Ctrl+X).
Test PHP by visiting http://your_server_ip/info.php in a browser. You’ll see a PHP info page with details about your installation.
Important: Delete the test file after verification (it exposes server details):
sudo rm /var/www/html/info.phpStep 3: Create a MySQL Database and User for WordPress#
WordPress needs a dedicated MySQL database and user. Follow these steps:
-
Log into MySQL as the root user:
sudo mysql -u root -pEnter the root password you set earlier.
-
Create a database for WordPress (name it
wordpressor your preferred name):CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -
Create a MySQL user (e.g.,
wpuser) and grant it full access to the database. Replaceyour_strong_passwordwith a secure password:CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'your_strong_password'; GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost'; -
Flush privileges to apply changes:
FLUSH PRIVILEGES; -
Exit MySQL:
EXIT;
Step 4: Download and Configure WordPress#
4.1 Download WordPress#
WordPress is updated frequently, so we’ll download the latest version using wget.
First, navigate to Apache’s web root:
cd /var/www/htmlDownload the latest WordPress tarball:
sudo wget https://wordpress.org/latest.tar.gzExtract the tarball:
sudo tar -xvzf latest.tar.gzThis creates a wordpress directory in /var/www/html.
4.2 Set File Permissions#
Apache (run by the www-data user) needs read/write access to the WordPress directory. Set ownership:
sudo chown -R www-data:www-data /var/www/html/wordpress/Set directory permissions (restrictive but functional):
sudo chmod -R 755 /var/www/html/wordpress/Create an uploads directory for media files (WordPress may auto-create this, but it’s safer to set permissions explicitly):
sudo mkdir /var/www/html/wordpress/wp-content/uploads
sudo chown -R www-data:www-data /var/www/html/wordpress/wp-content/uploads/4.3 Configure WordPress Settings#
WordPress uses wp-config.php to connect to the database. Copy the sample config file:
cd /var/www/html/wordpress
sudo cp wp-config-sample.php wp-config.phpEdit wp-config.php with your MySQL database details:
sudo nano wp-config.phpFind these lines and replace them with your database credentials (from Step 3):
define( 'DB_NAME', 'wordpress' ); // Your database name
define( 'DB_USER', 'wpuser' ); // Your MySQL user
define( 'DB_PASSWORD', 'your_strong_password' ); // Your MySQL password
define( 'DB_HOST', 'localhost' ); // Leave as localhostNext, add security keys to protect your site. WordPress provides a tool to generate these. Visit https://api.wordpress.org/secret-key/1.1/salt/ in your browser, copy the generated keys, and replace the AUTH_KEY, SECURE_AUTH_KEY, etc., lines in wp-config.php.
Example keys (do not use these—generate your own):
define( 'AUTH_KEY', 'put your unique phrase here' );
define( 'SECURE_AUTH_KEY', 'put your unique phrase here' );
define( 'LOGGED_IN_KEY', 'put your unique phrase here' );
define( 'NONCE_KEY', 'put your unique phrase here' );
define( 'AUTH_SALT', 'put your unique phrase here' );
define( 'SECURE_AUTH_SALT', 'put your unique phrase here' );
define( 'LOGGED_IN_SALT', 'put your unique phrase here' );
define( 'NONCE_SALT', 'put your unique phrase here' );Save (Ctrl+O) and exit (Ctrl+X).
Step 5: Configure Apache for WordPress#
5.1 Create an Apache Virtual Host#
A virtual host lets you host multiple sites on one server. If using a domain (e.g., yourdomain.com), create a virtual host file. If not, skip to Step 5.2 (WordPress will be accessible at http://your_server_ip/wordpress).
Create a virtual host file (replace yourdomain.com with your domain):
sudo nano /etc/apache2/sites-available/yourdomain.com.confPaste this configuration (update ServerName, ServerAlias, and DocumentRoot):
<VirtualHost *:80>
ServerName yourdomain.com # Your domain
ServerAlias www.yourdomain.com # Optional: www subdomain
DocumentRoot /var/www/html/wordpress # Path to WordPress directory
<Directory /var/www/html/wordpress/>
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/yourdomain.com_error.log
CustomLog ${APACHE_LOG_DIR}/yourdomain.com_access.log combined
</VirtualHost>AllowOverride All enables WordPress permalinks (pretty URLs).
Save and exit.
5.2 Enable the Virtual Host and Rewrite Module#
Enable the new virtual host:
sudo a2ensite yourdomain.com.confDisable the default Apache site (optional but recommended for production):
sudo a2dissite 000-default.confEnable the rewrite module (required for permalinks):
sudo a2enmod rewriteRestart Apache to apply changes:
sudo systemctl restart apache2Step 6: Complete WordPress Installation via Web Interface#
Now, finalize WordPress setup through your browser.
6.1 Access the Installation Page#
- If using a domain: Visit
http://yourdomain.com. - If not using a domain: Visit
http://your_server_ip/wordpress.
6.2 Run the Installation Wizard#
- Select Language: Choose your preferred language and click Continue.
- Site Information:
- Site Title: Enter your site name (e.g., “My Blog”).
- Username: Create an admin username (avoid “admin” for security).
- Password: Use a strong password (WordPress will suggest one).
- Your Email: Enter a valid email for password resets.
- Search Engine Visibility: Check “Discourage search engines from indexing this site” if building a test site.
- Click Install WordPress.
6.3 Log In#
After installation, click Log In and enter your admin credentials. You’ll be directed to the WordPress dashboard (wp-admin), where you can customize your site.
Troubleshooting Common Issues#
Issue 1: Permissions Errors#
If you see “Permission denied” errors, recheck file ownership:
sudo chown -R www-data:www-data /var/www/html/wordpress/Issue 2: PHP Extensions Missing#
WordPress may warn about missing extensions (e.g., mbstring). Install them with:
sudo apt install php-mbstring php-xml php-zip -y
sudo systemctl restart apache2Issue 3: MySQL Connection Error#
If WordPress can’t connect to the database, verify wp-config.php has the correct credentials (DB name, user, password).
Issue 4: Permalinks Not Working#
If “404 Not Found” errors occur when using permalinks, ensure rewrite module is enabled and AllowOverride All is set in your Apache config (Step 5.1).
Conclusion#
You’ve successfully installed WordPress on Ubuntu 18.04! You now have a functional CMS to build websites, blogs, or e-commerce stores.
Next Steps:
- Install a theme (Appearance → Themes).
- Add plugins (Plugins → Add New) for features like SEO (Yoast SEO), security (Wordfence), or e-commerce (WooCommerce).
- Secure your site with HTTPS (use Let’s Encrypt for free SSL certificates).