Tutorial: Installing WordPress on LEMP and Ubuntu 16.04

If you’ve been following along with our previous tutorials on setting up SSH authentication and then installing a basic LEMP (Linux, Nginx, MySQL, PHP) stack on Ubuntu 16.04, you’re completely set up start putting content online.
WordPress is a popular choice for those who want to set up a blog, an online portfolio, or even a basic ecommerce site (with the help of some plugins). Because of its popularity, WordPress has a strong support and developer community, with plenty of more specific resources and tutorials on different customizations.
For this tutorial, we’ll cover the configuration necessary to loop WordPress in with MySQL, Nginx, and your PHP implementation, up to the point where you perform the “Famous 5-Minute Install.”
Prerequisites
- A regular (non-root) account with sudo privileges. See our SSH keys tutorial for more information.
- A functioning LEMP stack. See our LEMP tutorial for details.
Step 1: Setting up MySQL
Because WordPress uses MySQL to store information about users and posts, you need to configure a database, followed by a user that will access that database. Start by logging into the MySQL root account.
$ mysql -u root -p
Then, create a database for WordPress to connect to. You can use whatever naming convention you would like instead of wordpress
.
mysql> CREATE DATABASE wordpress;
Query OK, 1 row affected (0.00 sec)
Next, create the user that will access the information inside of this new database. Feel free to change the wordpressuser
to something that fits your needs. A strong, secure password is important here.
mysql> CREATE USER [email protected] IDENTIFIED BY "<b>password<b>";
Query OK, 0 rows affected (0.00 sec)
Then, you need to give this new user access to the database you created.
mysql> GRANT ALL PRIVILEGES ON wordpress.* TO [email protected];
Query OK, 0 rows affected (0.00 sec)
You’ll need to flush the privileges so that you can connect WordPress to the new database.
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
Then exit the MySQL shell.
mysql> exit
Step 2: Configuring Nginx for WordPress
You’ll now need to configure Nginx to deliver traffic to the future WordPress installation. Start by creating a new server block for Nginx.
$ sudo nano /etc/nginx/sites-available/wordpress
Then you can paste the following into this new file and save it. If you have a domain name for this project, be sure to change the configuration at the server_name
line. If not, you can comment it out by placing a #
in front of the line: # server_name domain.com
.
server {
listen 80;
root /var/www/wordpress;
index index.php index.html index.htm;
server_name domain.com;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ .php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
log_not_found off;
access_log off;
allow all;
}
location ~* .(css|gif|ico|jpeg|jpg|js|png)$ {
expires max;
log_not_found off;
}
}
This is a fairly standard nginx+Wordpress server block to get you up and running. It doesn’t come with caching or gzip bells and whistles, but those are beyond the general scope of this tutorial. The WordPress Codex has some additional options if you’re interested.
Be sure to test the configuration to ensure that it will function correctly.
$ sudo nginx -t
You will also want to disable the default server block that we created in the LEMP tutorial, and then enable the new WordPress server block by creating a link between the sites-available
and sites-enabled
directories.
$ sudo rm /etc/nginx/sites-enabled/default
$ sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/wordpress
If you don't see any errors, you can reload nginx.
$ sudo systemctl reload nginx
Downloading, configuring, and installing WordPress
Begin by creating a new server root directory for the WordPress installation, and then cd
into it.
$ sudo mkdir /var/www/wordpress
To download WordPress, you’ll need to be in a directory your user can write into. What better than your home directory? Then you can start the download.
$ cd ~
$ curl -O https://wordpress.org/latest.tar.gz
Next, extract the .tar.gz
file.
$ tar xzvf latest.tar.gz
Now you can copy all the files to the server root.
$ sudo cp -a ~/wordpress/. /var/www/wordpress
The last step is configuring WordPress. First, you need to copy the example configuration file, and then make some basic edits. Start by cd
ing into the server root.
$ cd /var/www/wordpress
$ cp wp-config-sample.php wp-config.php
$ nano wp-config.php
Your first priority will be establishing a connection to the MySQL database you set up. Change the wordpress
, wordpressuser
, and password
values to what you established earlier.
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'wordpress');
/** MySQL database username */
define('DB_USER', 'wordpressuser');
/** MySQL database password */
define('DB_PASSWORD', 'password');
Next, you need to change the authentication keys and salts to unique strings. This will improve the overall security of your WordPress installation. Start by using WordPress’ salt generator to get unique strings.
$ curl -s https://api.wordpress.org/secret-key/1.1/salt/
define('AUTH_KEY', 'UNIQUE SALT DISPLAYED HERE');
define('SECURE_AUTH_KEY', 'UNIQUE SALT DISPLAYED HERE');
define('LOGGED_IN_KEY', 'UNIQUE SALT DISPLAYED HERE');
define('NONCE_KEY', 'UNIQUE SALT DISPLAYED HERE');
define('AUTH_SALT', 'UNIQUE SALT DISPLAYED HERE');
define('SECURE_AUTH_SALT', 'UNIQUE SALT DISPLAYED HERE');
define('LOGGED_IN_SALT', 'UNIQUE SALT DISPLAYED HERE');
define('NONCE_SALT', 'UNIQUE SALT DISPLAYED HERE');
You can simply copy this entire block of code into your wp-config.php
file and delete the dummy section, which looks like the following:
define('AUTH_KEY', 'put your unique phrase here');
define('SECURE_AUTH_KEY', 'put your unique phrase here');
define('LOGGED_IN_KEY', 'put your unique phrase here');
define('NONCE_KEY', 'put your unique phrase here');
define('AUTH_SALT', 'put your unique phrase here');
define('SECURE_AUTH_SALT', 'put your unique phrase here');
define('LOGGED_IN_SALT', 'put your unique phrase here');
define('NONCE_SALT', 'put your unique phrase here');
Once you’ve followed these steps, you can save the file.
The last step is configuring permissions for the server root. By giving permissions to the www-data
user, you’ll be able to get automatic updates and more.
$ sudo chown -R www-data:www-data /var/www/wordpress
$ sudo usermod -a -G www-data www-data
Finally, you can navigate to your server’s IP address to begin the famous 5-minute WordPress installation.
http://YOUR-SERVER-IP/wp-admin
If you see the following page, you’re ready to go. Congratulations!
If you see a large warning stating Error Establishing a Database Connection
, you should head into your wp-config.php
file again and double-check that you entered your MySQL database, user, and password correctly into that file.
In a future post, we’ll cover how to secure the connection with a Let’s Encrypt SSL certificate to ensure that all the traffic on your site is encrypted.
Like what you saw? Subscribe to our weekly newsletter.
You're subscribed!