This tutorial uses Linux Mint 18.
For development and testing, you’ll often want to run WordPress on a local machine. In Linux Mint we can get up and running quickly using a few packages and settings changes. It should be a similar process for Ubuntu or Debian.
Most of these commands need to be run as root or using
sudo -i if you want to enter many commands as root.
Apache Web Server
Apache handles incoming connections, and runs the WordPress PHP code. Install by running:
apt-get install apache2
This should start the apache service when completed. Run
ps ax, and look for lines like this:
4088 ? S 0:00 /usr/sbin/apache2 -k start
This shows you that the web server is running. It will also start again when your system reboots. When it is not running you can start it using:
service apache2 start
You can also
restart the service with this command.
We are going to add two Apache modules that will help us for local WordPress development.
a2enmod rewrite userdir service apache2 restart
The Rewrite module is a requirement for WordPress to define custom URL patterns.
The Userdir module allows regular users to host files in their home directories. By default, Apache will only serve files from
/var/www/html, which requires root permissions to modify.
userdir module active, files stored in
/home/username/public_html will be served at
Create an HTML file in your
public_html folder and confirm that it is being served correctly.
Install PHP 7, the Apache interface, and a few modules that we will need:
apt-get install php7.0 php7.0-mysql libapache2-mod-php7.0 php7.0-cli php7.0-cgi php7.0-gd
Apache will not run PHP code in a userdir by default, so we need to enable this manually. Edit the
php7.0.conf file here:
There is a section at the end we will comment out by adding
# to the beginnings of the lines. It should look like this in the end:
# To re-enable PHP in user directories comment the following lines # (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it # prevents .htaccess files from disabling it. #<IfModule mod_userdir.c> # <Directory /home/*/public_html> # php_admin_flag engine Off # </Directory> #</IfModule>
Restart Apache to reload the config files:
service apache2 restart
Now we want to confirm that PHP files are actually being run from our userdir.
Create this file:
Put this in the file:
<?php phpinfo(); ?>
Save the file and then vist
http://localhost/~yourname/info.php. You should see a large table of infomation generated by PHP. If the page is blank or showing an error, then the installation/configuration is not correct.
The last major piece is the MySQL database, where WordPress will store most of its data. First install MySQL on your system:
apt-get install mysql-client mysql-server
During the installation process, a screen will ask you to supply a password for the root MySQL user. By default the password is blank, and it is recommended to add a password.
You can improve the security of the installation by running this:
This command will ask you a series of questions to optionally enable security features. You probably don’t need to use them all, but I’d recommend a few things:
- Remove anonymous users
- Disallow root login remotely
- Remove test database
- Reload privilege tables
This tutorial is about setting up a local development environment, not a secure production environment. The advice above will improve the security of a default installation, but would not be sufficient for production.
Creating a MySQL user and database
We do not want WordPress to have root access to our entire database server. We will create a MySQL user and database specifically for WordPress.
Login to the MySQL console as root (this command must be ran as root or with
mysql -u root -p
This will ask you for the password you created when installing MySQL and will open a MySQL terminal with total access to the database server.
To create the database and user, enter the following commands using your own values (uppercase keywords are conventional but not required):
CREATE DATABASE 'wordpress'; CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'wppassword'; GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost'; FLUSH PRIVILEGES;
We are now ready to install WordPress itself.
Go to the WordPress website to download the source code. We will extract these files in our userdir:
cd ~/public_html # for zip unzip ~/Downloads/wordpress-4.8.1.zip # for tar.gz tar xzf ~/Downloads/wordpress-4.8.1.tar.gz
http://localhost/~username/wordpress/ you should see a setup page created by WordPress. It is possible for WordPress to do the final setup here, but it would require changing the permissions of the
Instead we will create the file manually:
cd ~/public_html/wordpress/ cp wp-config-sample.php wp-config.php vim wp-config.php
Edit the new
wp-config.php file and update the following values to match the database info:
/** The name of the database for WordPress */ define('DB_NAME', 'wordpress'); /** MySQL database username */ define('DB_USER', 'wpuser'); /** MySQL database password */ define('DB_PASSWORD', 'wppassword'); /** MySQL hostname */ define('DB_HOST', 'localhost');
Go back to
http://localhost/~username/wordpress/ and you will see a new form, asking for new user information. This will setup the admin user of your new site. Fill in the info and your WordPress installation is complete!