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
-
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.
-
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:
-
Open the file:
vim Vagrantfile
-
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 setvb.memory
to1600
MB.
- Uncomment
- Save and close the file with
Esc
, then:wq
.
Step 3: Start and Access the VM
-
Start the VM:
vagrant up
-
SSH into the VM:
vagrant ssh
-
Switch to the root user:
sudo -i
-
Change the hostname to “wordpress”:
vim /etc/hostname
-
Replace the existing name with
wordpress
, save and close, then type:
hostname wordpress
-
Log out and log back in to apply the hostname changes:
exit vagrant ssh sudo -i
Step 4: Install the LAMP Stack
-
Update the package list:
sudo apt update
-
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
-
Create a directory for WordPress and set permissions:
sudo mkdir -p /srv/www sudo chown www-data: /srv/www
-
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
-
Open the Apache configuration file:
vim /etc/apache2/sites-available/wordpress.conf
-
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>
Save and close the file:
Esc
:wq
-
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
-
Connect to MySQL:
sudo mysql -u root
-
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;
-
Exit MySQL:
quit;
Step 8: Configure WordPress to Connect to the Database
-
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
-
Add security keys for your WordPress site. Open the
wp-config.php
file:
sudo -u www-data vim /srv/www/wordpress/wp-config.php
Replace the lines defining authentication keys with values from this WordPress API, save, and close. (ctrl+x followed by y then enter).
Step 9: Complete the Setup in the Browser
-
To get the IP address of your VM:
ip addr show
Copy the IP address and paste it in your browser. You should see the WordPress setup page.
- Follow the on-screen instructions to finish setting up WordPress:
- Enter your site title, username, password, and email.
- Click "Install WordPress" and log in.
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)