
Setting Up a WordPress Website on Ubuntu VPS
Setting Up a WordPress Website on Ubuntu VPS
Introduction
In this tutorial, we will walk through the process of setting up a WordPress website on a Virtual Private Server (VPS) using Ubuntu. We’ll use a LAMP stack (Linux, Apache, MySQL, PHP), and by the end, you’ll have a fully functional WordPress site ready to customize and populate with content.
Table of Contents
TogglePrerequisites
- A VPS running Ubuntu 20.04 or later.
- SSH access to the server.
- A domain name (optional) for your WordPress site.
- Basic familiarity with the command line.
Step 1: Connect to Your VPS
First, connect to your VPS using SSH. Open your terminal and run:
ssh username@your_server_ip
Replace username
with your actual username and your_server_ip
with your VPS’s IP address.
Step 2: Update Your System
Before installing anything, make sure your system is up to date:
sudo apt update && sudo apt upgrade -y
Step 3: Install Apache
Apache is a popular web server. To install it, run:
sudo apt install apache2 -y
After installation, enable and start the Apache server:
sudo systemctl enable apache2
sudo systemctl start apache2
You can verify that Apache is running by visiting http://your_server_ip
in your web browser. You should see the Apache2 Ubuntu Default Page.
Step 4: Install MySQL
Next, we need a database server to store WordPress data. We’ll install MySQL:
sudo apt install mysql-server -y
After installation, run the security script to set up the root password and secure your installation:
sudo mysql_secure_installation
Follow the prompts to create a root password and remove any unnecessary users or databases.
Step 5: Install PHP
WordPress is built on PHP, so we need to install PHP and some necessary extensions:
sudo apt install php php-mysql libapache2-mod-php php-curl php-xml php-mbstring php-zip -y
Step 6: Test PHP
To test if PHP is working, create a simple info.php
file in the web root directory:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
Visit http://your_server_ip/info.php
in your browser. You should see the PHP information page. Once you confirm it’s working, delete this file for security reasons:
sudo rm /var/www/html/info.php
Step 7: Create a MySQL Database for WordPress
Now, let’s create a database for your WordPress site. Log into MySQL:
sudo mysql -u root -p
Create a new database and user for WordPress:
CREATE DATABASE wordpress;
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Replace 'password'
with a strong password of your choice.
Step 8: Download WordPress
Next, we need to download the latest version of WordPress. First, navigate to the web root directory:
cd /var/www/html
Then, download and extract WordPress:
wget https://wordpress.org/latest.tar.gz
tar -xvzf latest.tar.gz
Now, move the contents into the web root:
sudo mv wordpress/* ./
sudo rm -rf wordpress latest.tar.gz
Step 9: Configure WordPress
WordPress needs to be configured to connect to the database. First, copy the sample configuration file:
sudo cp wp-config-sample.php wp-config.php
Now, edit the wp-config.php
file:
sudo nano wp-config.php
Find the following lines and update them with your database details:
define('DB_NAME', 'wordpress');
define('DB_USER', 'wpuser');
define('DB_PASSWORD', 'password');
You can also change the security keys in this file by generating them from the WordPress secret key generator. Replace the relevant lines in your wp-config.php
.
Step 10: Set Permissions
To ensure WordPress can write to the necessary directories, set the correct permissions:
sudo chown -R www-data:www-data /var/www/html
sudo find /var/www/html -type d -exec chmod 755 {} \;
sudo find /var/www/html -type f -exec chmod 644 {} \;
Step 11: Enable Apache Modules
WordPress requires the rewrite
module for permalinks to work. Enable this module:
sudo a2enmod rewrite
sudo systemctl restart apache2
Next, update the Apache configuration to allow .htaccess
overrides. Open the default configuration file:
sudo nano /etc/apache2/sites-available/000-default.conf
Find the <Directory /var/www/html>
section and change it to:
<Directory /var/www/html>
AllowOverride All
</Directory>
Save and exit, then restart Apache again:
sudo systemctl restart apache2
Step 12: Complete WordPress Installation
Open your web browser and navigate to http://your_server_ip
. You should see the WordPress installation page. Follow the on-screen instructions to set up your site, including choosing your site title, username, password, and email.
Step 13: Secure Your WordPress Site
After setting up WordPress, it’s essential to secure your installation:
- Keep WordPress Updated: Regularly update WordPress, themes, and plugins to avoid vulnerabilities.
- Install a Security Plugin: Consider installing a security plugin like Wordfence or Sucuri for added protection.
- Configure a Firewall: On your VPS, you can use UFW (Uncomplicated Firewall) to secure your server. For example, to allow only SSH, HTTP, and HTTPS traffic:
sudo ufw allow OpenSSH sudo ufw allow 'Apache Full' sudo ufw enable
- Disable Directory Listing: Edit the Apache configuration file to disable directory listing:
sudo nano /etc/apache2/apache2.conf
Add the following line:
Options -Indexes
Then restart Apache:
sudo systemctl restart apache2
- Regular Backups: Set up a backup plan using plugins or manual backups to ensure you can restore your site if something goes wrong.
Step 14: Set Up a Domain Name (Optional)
If you have a domain name, point it to your VPS’s IP address using your domain registrar’s DNS management tools. After the DNS propagates, you can access your site using the domain name instead of the IP address.
- Update your Apache configuration: If you are using a domain name, you may want to create a new configuration file for your site:
sudo nano /etc/apache2/sites-available/yourdomain.conf
Add the following content (replace
yourdomain.com
with your actual domain):<VirtualHost *:80> ServerAdmin [email protected] ServerName yourdomain.com ServerAlias www.yourdomain.com DocumentRoot /var/www/html <Directory /var/www/html> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
- Enable the new site and restart Apache:
sudo a2ensite yourdomain.conf sudo systemctl restart apache2
Conclusion
Congratulations! You’ve successfully set up a WordPress website on your Ubuntu VPS. You’ve learned how to install the LAMP stack, configure MySQL, download and configure WordPress, and even set up basic security measures.
Next Steps
- Customize Your Site: Explore the WordPress Dashboard to customize your site’s appearance, install themes, and add plugins.
- Content Creation: Start creating posts and pages to populate your website.
- SEO Optimization: Consider using SEO plugins like Yoast SEO to optimize your content for search engines.
Additional Resources
Feel free to reach out if you have any questions or need further assistance. Happy blogging!