How To Run WordPress On Localhost Ubuntu

Running WordPress on your localhost is an excellent way to test out ideas, themes, and plugins without affecting your live site. In this tutorial, we will demonstrate how to install and set up WordPress on a localhost using Ubuntu.

Prerequisites

Before proceeding, ensure you have the following installed on your Ubuntu system:

  • Apache web server
  • PHP
  • MySQL server

Step 1: Download WordPress

Download the latest version of WordPress from the official website. Once the download is complete, extract the files to your desired location. For example, you can extract it in the Apache web server’s document root directory:

sudo tar -xzvf latest.tar.gz -C /var/www/html/

Now, rename the extracted WordPress directory to a more appropriate name:

sudo mv /var/www/html/wordpress /var/www/html/mysite

Step 2: Set up the MySQL Database and User

WordPress requires a MySQL database to store its data. Log in to your MySQL server using the following command:

mysql -u root -p

Enter your MySQL root password and then create a new database:

CREATE DATABASE mysite_db;

Next, create a new MySQL user and grant them the necessary privileges:

CREATE USER 'mysite_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON mysite_db.* TO 'mysite_user'@'localhost';
FLUSH PRIVILEGES;

Exit MySQL by typing:

exit;

Step 3: Configure WordPress

Navigate to the WordPress directory you created earlier:

cd /var/www/html/mysite

Copy the sample configuration file and rename it:

sudo cp wp-config-sample.php wp-config.php

Edit the configuration file with your preferred text editor, such as nano:

sudo nano wp-config.php

Update the following lines with your database information:


    // ** MySQL settings - You can get this info from your web host ** //
    /** The name of the database for WordPress */
    define( 'DB_NAME', 'mysite_db' );

    /** MySQL database username */
    define( 'DB_USER', 'mysite_user' );

    /** MySQL database password */
    define( 'DB_PASSWORD', 'your_password' );

    /** MySQL hostname */
    define( 'DB_HOST', 'localhost' );
    

Save and close the file.

Step 4: Set Up Apache Virtual Host

Create a new Apache virtual host configuration file for your WordPress site:

sudo nano /etc/apache2/sites-available/mysite.conf

Add the following configuration:


    <VirtualHost *:80>
        ServerAdmin admin@example.com
        ServerName mysite.local
        DocumentRoot /var/www/html/mysite

        <Directory /var/www/html/mysite>
            AllowOverride All
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/mysite_error.log
        CustomLog ${APACHE_LOG_DIR}/mysite_access.log combined
    </VirtualHost>
    

Save and close the file. Then, enable the new site and disable the default one:


    sudo a2ensite mysite.conf
    sudo a2dissite 000-default.conf
    

Enable the Apache rewrite module and restart the Apache server:


    sudo a2enmod rewrite
    sudo systemctl restart apache2
    

Edit your hosts file to test your site locally:

sudo nano /etc/hosts

Add the following line to the file:

127.0.0.1 mysite.local

Save and close the file.

Step 5: Complete WordPress Installation

Now, open your web browser and navigate to http://mysite.local. Follow the on-screen instructions to complete the WordPress installation process.

Conclusion

You have successfully installed and set up WordPress on your localhost using Ubuntu. You can now develop and test your WordPress site locally before making any changes to your live site. Happy developing!