This article follows on from the last article I wrote and continues the theme of installing the wordpress software on your server. In the last article we dealt with downloading the software and installing it on the server – this article deals with wordpress database creation; setting up the database for wordpress and editing the configuration file so that WordPress can connect to the database.
WordPress Database Creation and Configuration
As with the last article I’m going to make some assumptions but they are not huge assumptions. Since it is a requirement of WordPress that you have an SQL database and currently wordpress only runs with MySQL or MariaDB (which is a fork of MySQL), I’ll assume that you have MySQL installed on your server environment (or development laptop if that is what you are using). I’ll also assume that you have the credentials of an account which has permission to create databases and users.
Later, I’m going to write an article on setting up a digital ocean server from scratch so if you need help with that, come back in a couple of weeks.
WordPress Database Creation
The first thing we need to do is log into the MySQL server and for that I tend to use the MySQL command line client. The command to log into the database using the command line client would be
mysql -h localhost -u root -p Enter Password:
In this case I am using a MySQL server which is running on the local machine and logging in as the root user. The -p option means I will be asked to supply the password as soon as I issue the command (hence the Enter Password prompt).
It’s possible, although unlikely, that you have a MySQL server running on another machine in which case you would supply the hostname after the -h parameter.
If the command is successful and you connect, you will be greeted with the MySQL login command prompt.
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 744 Server version: 5.7.21-0ubuntu0.16.04.1 (Ubuntu) Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
The first thing we are going to do is create a database for our WordPress install and then a user and password to allow WordPress to use the database. Before we do that however I would suggest a bit of house keeping.
I’ve found over the years when I’ve created blogs or other sites, that the easiest thing in the world is to just go through the commands and set everything up with the full intention of making a note somewhere of the usernames and passwords you have used, only to find that you forget to do it. It’s even easy to convince yourself that you don’t need to make a note because all the information will be copied into the configuration file for WordPress and you can always read them from there. Well that may well be true, but I would still take the time to work out beforehand what username, password and database name you are going to use and write them somewhere securely before you start. A system like Lastpass is brilliant for this and will solve a lot of problems in the future if you have recorded this information.
Also be aware that your install of mysql could have been enabled to require passwords with high security, in which case you may need to ensure you use punctuation characters in the password and / or capitals, or mixed case and numbers.
Anyway, back to the database. The following shows the commands I use to create the database and user required to access WordPress, along with the expected response from MySQL.
mysql>create database my-site; Query OK, 1 row affected (0.04 sec) mysql>create user 'mysite_user'@'localhost' identified by 'my-secret-password'; Query OK, 0 rows affected (1.01 sec) mysql>grant all privileges on my-site.* to 'mysite-user'@'localhost'; Query OK, 0 rows affected (0.02 sec) mysql>flush privileges; Query OK, 0 rows affected (0.01 sec)
Obviously you would need to use the values for database (‘my-site’ in the example), username (‘mysite_user’ in the example) and password (‘my-secret-password’ in the example) which you have decided on.
As a check that the user and database connection works, it’s a good idea to disconnect from the database and try reconnecting with the new account.
mysql>\q Bye simon@simon-probook:/usr/local/sites/my-site$ mysql -h localhost -u mysite-user -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 747 Server version: 5.7.21-0ubuntu0.16.04.1 (Ubuntu) Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | my-site | +--------------------+ 2 rows in set (0.08 sec) mysql>
WordPress Database Configuration
With the new database and user set up we are now in a position to use the database, username and password information to complete the configuration of the wordpress software we installed in the first part of this series. To do this we need to go to the wordpress directory which got created when we untarred the wordpress install archive, and add the parameters to the configuration file.
On my install I’ve created a directory for my new site at
/usr/local/sites/my-site-name.co.uk so I cd to the wordpress directory within that directory
cd /usr/local/sites/my-site-name.co.uk/wordpress mv wp-config-sample.php wp-config.php nano wp-config.php
The configuration is held in a file called wp-config.php but that file isn’t supplied in the wordpress install. Instead there is a file called wp-config-sample.php which needs to be renamed and edited to make the install work.
In the last command above I’ve stared an editing command with the nano editor to change the parameters. I don’t particularly like nano, and personally I would use jed, but you can be pretty sure nano is installed on virtually every machine so it is safe to assume it is there.
Irrespective of the editor you use, the first section which needs to be altered is this one
// ** MySQL settings - You can get this info from your web host ** // /** The name of the database for WordPress */ define('DB_NAME', 'database_name_here'); /** MySQL database username */ define('DB_USER', 'username_here'); /** MySQL database password */ define('DB_PASSWORD', 'password_here'); /** MySQL hostname */ define('DB_HOST', 'localhost');
It is fairly self explanatory which information goes into which php define, but once that is added there are a couple more entries which should be altered.
Further down the file is another entry which defines some random strings used for generating security hashes for form submission, site cookies etc and should be replaced with some unique values. You don’t need to think of these yourself however, there is a generation service at https://api.wordpress.org/secret-key/1.1/salt/ which will generate a complete set of define statements ready to paste into the file in place of the existing default values.
The area of the file which needs to change is
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');
Finally, if the site you are setting up is purely for development there is one other entry which should be changed which is
/** * For developers: WordPress debugging mode. * * Change this to true to enable the display of notices during development. * It is strongly recommended that plugin and theme developers use WP_DEBUG * in their development environments. * * For information on other constants that can be used for debugging, * visit the Codex. * * @link https://codex.wordpress.org/Debugging_in_WordPress */ define('WP_DEBUG', false);
this should be set to true if the site is going to be used for development since it will allow useful warnings and debug messages to be displayed. Don’t set it to true for a production site however.
With those changes made the wp-config.php file can be saved and we are nearly ready to move to the next part of the install which is setting up a virtual web server configuration.
First however we should set the directory so it is readable by the web server software.
cd /usr/local/sites sudo chown www-data:www-data my-site-name -R
In the next part of this series I’ll cover setting up a virtual server configuration file for nginx and apache which are the two most popular web servers used on the Internet.