Simple guide for setting up a virtual host and MySQL for WordPress development. This is tailored for Ubuntu/Debian-based distributions.


Steps to Set Up Virtual Host and MySQL for WordPress Development on Linux


1. Install Required Software

  1. Update your system:
sudo apt update && sudo apt upgrade -y
  1. Install Apache, MySQL, and PHP:
sudo apt install apache2 mysql-server php libapache2-mod-php php-mysql

2. Set Up MySQL Database

  1. Secure MySQL installation:
sudo mysql_secure_installation
  • Set a root password (if prompted).
  • Answer Y (yes) to all security questions.
  1. Log in to MySQL:
sudo mysql -u root -p
  1. Create a database for WordPress:
CREATE DATABASE wordpress_db;
  1. Create a MySQL user and grant privileges:
CREATE USER 'wordpress_user'@'localhost' IDENTIFIED BY 'your_password';
   GRANT ALL PRIVILEGES ON wordpress_db.* TO 'wordpress_user'@'localhost';
   FLUSH PRIVILEGES;
   EXIT;

3. Configure Virtual Host

  1. Create a directory for your WordPress site:
sudo mkdir -p /var/www/yourdomain.com
  1. Set permissions:
sudo chown -R $USER:$USER /var/www/yourdomain.com
  1. Create a virtual host configuration file:
sudo nano /etc/apache2/sites-available/yourdomain.com.conf
  1. Add the following content:
<VirtualHost *:80>
       ServerName yourdomain.com
       DocumentRoot /var/www/yourdomain.com

       <Directory /var/www/yourdomain.com>
           AllowOverride All
       Directory>

       ErrorLog ${APACHE_LOG_DIR}/error.log
       CustomLog ${APACHE_LOG_DIR}/access.log combined
   VirtualHost>
  1. Enable the site and reload Apache:
sudo a2ensite yourdomain.com.conf
   sudo systemctl reload apache2

4. Install WordPress

  1. Download WordPress:
cd /tmp && wget https://wordpress.org/latest.tar.gz
  1. Extract WordPress to your site directory:
sudo tar -xvzf latest.tar.gz -C /var/www/yourdomain.com --strip-components=1
  1. Set permissions:
sudo chown -R www-data:www-data /var/www/yourdomain.com

5. Configure WordPress

  1. Open your browser and go to http://yourdomain.com.
  2. Follow the WordPress setup wizard:
    • Database Name: wordpress_db
    • Username: wordpress_user
    • Password: your_password
    • Database Host: localhost
    • Table Prefix: wp_ (default)
  3. Complete the installation.

6. Test Your Setup

  • Visit http://yourdomain.com to see your WordPress site.
  • Log in to the WordPress admin dashboard at http://yourdomain.com/wp-admin.

Optional: Local Domain Setup

If you’re working locally, edit your /etc/hosts file to map yourdomain.com to 127.0.0.1:

  1. Open the hosts file:
sudo nano /etc/hosts
  1. Add this line:
127.0.0.1   yourdomain.com
  1. Save and exit.

Summary

  • Installed Apache, MySQL, and PHP on Linux.
  • Created a MySQL database and user.
  • Set up a virtual host for your domain.
  • Installed and configured WordPress.

You’re now ready for WordPress development on Linux! 🚀