DEV Community

Cover image for Deploying WordPress on Ubuntu VM with LAMP Stack: A Step-by-Step Guide
Joseph Ibeh
Joseph Ibeh

Posted on

Deploying WordPress on Ubuntu VM with LAMP Stack: A Step-by-Step Guide

Deploying a WordPress site on a local server is a fantastic way to build a secure development environment for web projects. In this guide, I'll walk you through setting up a Virtual Machine (VM) with the LAMP stack (Linux, Apache, MySQL, PHP) and installing WordPress on it. Follow these steps to get your WordPress site up and running!

Overview

WordPress is a widely-used open-source CMS known for its flexibility and ease of use. This guide covers the steps for setting up WordPress on an Apache server, configuring a MySQL database, and securing the installation.

Prerequisites

  • Basic knowledge of command-line operations
  • Installed Vagrant and VirtualBox
  • Familiarity with editing files in the command line

Step 1: Set Up Your Project Directory

  1. Open your terminal (Git Bash or other CLI) and create a directory for your project.

    mkdir wordpress
    cd wordpress
    

    This directory will house your WordPress files and VM configuration.

mkdir

  1. Initialize a Vagrant box:

    vagrant init ubuntu/focal64
    

    This command sets up a Vagrantfile, which will configure your VM.

Step 2: Configure the Vagrantfile

Edit the Vagrantfile to set up the network and memory for your VM:

  1. Open the file:

    vim Vagrantfile
    
  2. Make the following changes:

    • Uncomment config.vm.network "private_network" and set it to a static IP (e.g., 192.168.56.26).
    • Uncomment config.vm.network "public_network".
    • Uncomment config.vm.provider "virtualbox" and set vb.memory to 1600 MB.

config private network

config public network

config virtual box

config memory

  1. Save and close the file with Esc, then :wq.

Step 3: Start and Access the VM

  1. Start the VM:

    vagrant up
    

vagrant up

  1. SSH into the VM:

    vagrant ssh
    

vagrant ssh

  1. Switch to the root user:

    sudo -i
    
  2. Change the hostname to “wordpress”:

    vim /etc/hostname
    
  • Replace the existing name with wordpress, save and close, then type:

    hostname wordpress
    
  1. Log out and log back in to apply the hostname changes:

    exit
    vagrant ssh
    sudo -i
    

Step 4: Install the LAMP Stack

  1. Update the package list:

    sudo apt update
    
  2. Install Apache, MySQL, PHP, and other dependencies:

    sudo apt install apache2 ghostscript libapache2-mod-php mysql-server php php-bcmath php-curl php-imagick php-intl php-json php-mbstring php-mysql php-xml php-zip -y
    

Step 5: Download and Install WordPress

  1. Create a directory for WordPress and set permissions:

    sudo mkdir -p /srv/www
    sudo chown www-data: /srv/www
    
  2. Download the latest WordPress release and extract it:

    curl https://wordpress.org/latest.tar.gz | sudo -u www-data tar zx -C /srv/www
    

Step 6: Configure Apache for WordPress

  1. Open the Apache configuration file:

    vim /etc/apache2/sites-available/wordpress.conf
    
  2. Add the following content:

    <VirtualHost *:80>
        DocumentRoot /srv/www/wordpress
        <Directory /srv/www/wordpress>
            Options FollowSymLinks
            AllowOverride Limit Options FileInfo
            DirectoryIndex index.php
            Require all granted
        </Directory>
        <Directory /srv/www/wordpress/wp-content>
            Options FollowSymLinks
            Require all granted
        </Directory>
    </VirtualHost>
    
  3. Save and close the file: Esc :wq

  4. Enable the site and modules, and reload Apache:

    sudo a2ensite wordpress
    sudo a2enmod rewrite
    sudo a2dissite 000-default
    sudo service apache2 reload
    

Step 7: Set Up the Database for WordPress

  1. Connect to MySQL:

    sudo mysql -u root
    
  2. Run the following commands to create a database and user for WordPress (replace admin123 with your own password):

    CREATE DATABASE wordpress;
    CREATE USER wordpress@localhost IDENTIFIED BY 'admin123';
    GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER ON wordpress.* TO wordpress@localhost;
    FLUSH PRIVILEGES;
    
  3. Exit MySQL:

    quit;
    

Step 8: Configure WordPress to Connect to the Database

  1. Copy the sample configuration file replace with your password:

    sudo -u www-data cp /srv/www/wordpress/wp-config-sample.php /srv/www/wordpress/wp-config.php
    sudo -u www-data sed -i 's/database_name_here/wordpress/' /srv/www/wordpress/wp-config.php
    sudo -u www-data sed -i 's/username_here/wordpress/' /srv/www/wordpress/wp-config.php
    sudo -u www-data sed -i 's/password_here/admin123/' /srv/www/wordpress/wp-config.php
    
  2. Add security keys for your WordPress site. Open the wp-config.php file:

    sudo -u www-data vim /srv/www/wordpress/wp-config.php
    
  3. Replace the lines defining authentication keys with values from this WordPress API, save, and close. (ctrl+x followed by y then enter).

security keys

Step 9: Complete the Setup in the Browser

  1. To get the IP address of your VM:

    ip addr show
    
  2. Copy the IP address and paste it in your browser. You should see the WordPress setup page.

ip addr

wordpress page

continue

  1. Follow the on-screen instructions to finish setting up WordPress:
    • Enter your site title, username, password, and email.
    • Click "Install WordPress" and log in.

fill in details

success

deployed wordpress

Congratulations! now we have Deployed our WordPress.

This guide provides a comprehensive walkthrough for setting up a WordPress site on a local virtual machine. Here are the key takeaways:

Skills Gained

  • VM Setup: Configuring and provisioning an Ubuntu VM with Vagrant.
  • LAMP Stack Installation: Installing and managing Apache, MySQL, and PHP for server deployment.
  • Database Management: Creating and securing a MySQL database for WordPress.
  • Apache Configuration: Setting up virtual hosts and permissions for WordPress on Apache.
  • Security Practices: Securing the wp-config.php file with unique keys.

With this setup, you’re ready to explore and develop WordPress sites locally. Happy coding, and feel free to share your feedback or any questions!

Top comments (0)