How to Install WordPress on a VPS
Installing WordPress on a Virtual Private Server (VPS) gives you greater control, customization, and scalability compared to shared hosting. This guide will walk you through the process, whether you’re using a Linux-based VPS with a LAMP stack (Linux, Apache, MySQL, PHP) or a similar environment.
1. Set Up Your Server Environment
a. Update Your System
Log in to your VPS via SSH and update your package lists:
sudo apt update && sudo apt upgrade -y
b. Install Apache, MySQL, and PHP (LAMP Stack)
For Ubuntu or Debian-based systems, run:
sudo apt install apache2 mysql-server php php-mysql libapache2-mod-php php-cli php-curl php-gd php-mbstring php-xml php-xmlrpc -y
Tip: If you prefer Nginx, install and configure it accordingly.
c. Secure MySQL
Run the security script to set a root password and remove insecure defaults:
sudo mysql_secure_installation
2. Create a Database for WordPress
a. Log into MySQL
sudo mysql -u root -p
b. Create the Database and User
Inside the MySQL shell, run:
CREATE DATABASE wordpress_db DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE USER 'wp_user'@'localhost' IDENTIFIED BY 'your_strong_password';
GRANT ALL ON wordpress_db.* TO 'wp_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Replace your_strong_password
with a secure password.
3. Download and Configure WordPress
a. Download WordPress
Navigate to the web root directory (commonly /var/www/html
) and download WordPress:
cd /var/www/html
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -xzf latest.tar.gz
sudo rm latest.tar.gz
This extracts WordPress into a folder called wordpress
.
b. Configure Permissions
Set the correct ownership and permissions:
sudo chown -R www-data:www-data /var/www/html/wordpress
sudo find /var/www/html/wordpress -type d -exec chmod 755 {} \;
sudo find /var/www/html/wordpress -type f -exec chmod 644 {} \;
c. Create the WordPress Configuration File
Copy the sample configuration file:
cd wordpress
sudo cp wp-config-sample.php wp-config.php
Edit wp-config.php
:
sudo nano wp-config.php
Replace the following lines with your database details:
define( 'DB_NAME', 'wordpress_db' );
define( 'DB_USER', 'wp_user' );
define( 'DB_PASSWORD', 'your_strong_password' );
define( 'DB_HOST', 'localhost' );
Save and exit (Ctrl+O, Enter, Ctrl+X).
4. Configure Apache (or Nginx) for WordPress
For Apache Users:
a. Create a Virtual Host File
Create a new Apache configuration file:
sudo nano /etc/apache2/sites-available/wordpress.conf
Insert the following configuration (adjust paths and domain as needed):
<VirtualHost *:80>
ServerAdmin admin@example.com
ServerName yourdomain.com
DocumentRoot /var/www/html/wordpress
<Directory /var/www/html/wordpress>
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/wordpress_error.log
CustomLog ${APACHE_LOG_DIR}/wordpress_access.log combined
</VirtualHost>
Save and exit.
b. Enable the Site and Rewrite Module
sudo a2ensite wordpress.conf
sudo a2enmod rewrite
sudo systemctl restart apache2
For Nginx Users:
a. Create a Server Block File
sudo nano /etc/nginx/sites-available/wordpress
Insert the following configuration:
server {
listen 80;
server_name yourdomain.com;
root /var/www/html/wordpress;
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/php7.4-fpm.sock; # Adjust PHP version if needed
}
location ~ /\.ht {
deny all;
}
}
Save and exit.
b. Enable the Server Block and Restart Nginx
sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/
sudo systemctl restart nginx
5. Complete the WordPress Installation
- Open Your Browser:
Visit your domain (e.g.,http://yourdomain.com
). - Follow the On-Screen Instructions:
Select your language, set up your site title, admin username, password, and email address. Complete the installation by clicking “Install WordPress.” - Log In:
Once installed, log into your WordPress dashboard athttp://yourdomain.com/wp-admin
.
Final Thoughts
Installing WordPress on a VPS gives you the flexibility and power to manage your website on your own terms. By following these steps, you’ll have a robust, self-hosted WordPress installation ready for customization and growth.
Need further assistance? Many hosting communities and forums can offer additional tips and troubleshooting advice to help you optimize your setup. Enjoy your new WordPress site on your VPS!