Learn How To Install WordPress On Ubuntu The Easy Way

Introduction

This guide will teach you how to install WordPress on Ubuntu using Nginx WebServer. You will first need to have an Nginx server up and running using a LEMP stuck (Linux, Nginx, MySql, PHP), and SSL certificate enabled. The best option (free), is by using a SSL certificate from Let’s Encrypt.

Let’s consider the scenario that you already configured your nginx-PHP-mysql installation on your server. For some, WordPress installation on Ubuntu or other Linux distributions might be a pain. Now it’s time to install WordPress and start writing some content!

Step 1-Preparing Site’s Root Directory Location On The Server

For consistency let’s continue to use the location as described to the nginx php mysql installation tutorial, and let’s assume your domain name is example.com and your site files will be hosted on html_public folder. This can be achieved as follows:

nikolas@admintuts:~$ mkdir /home/nikolas/example.com
nikolas@admintuts:~$ mkdir /home/nikolas/example.com/html_public

And navigate to the html_public folder:

nikolas@admintuts:~$ cd /home/nikolas/example.com/html_public
nikolas@admintuts:~/example.com/html_public$

So you created 2 directories. Now you are good to go continue downloading WordPress. The fastest way is described below:

nikolas@admintuts:~/example.com/html_public$ sudo wget https://wordpress.org/latest.tar.gz

Using the command ls, you can see that you now have latest.tar.gz. Now extract its contents:

nikolas@admintuts:~/example.com/html_public$ tar xzf latest.tar.gz

You now have a folder named “WordPress” inside your current directory, but you need to move it’s contents one level up (to your public_html directory). You do that with this command:

<code class="language-bash">nikolas@admintuts:~/example.com/html_public$ cp -a /home/nikolas/example.com/html_public/wordpress/. /home/nikolas/example.com/html_public

The -a option is an improved recursive option, preserving all file attributes, but also preserves symlinks. The . at end of the source path, is a specific cp syntax that allow to copy all files and folders, included hidden ones.

Lastly, there is only one thing left to be done. Delete the initial latest.tar.gz that was downloaded, and also the wordpress folder which it’s contents were copied:

nikolas@admintuts:~/example.com/html_public$ sudo rm latest.tar.gz
nikolas@admintuts:~/example.com/html_public$ sudo rm -rf wordpress

Now the root folder is clean, and the time has come to set up the MySql database.

Step 2 – MySql Database Configuration

There are only 3 things that must be done with the database. Create the database it self, add a user, and grant all privileges to that specific database. But for doing that, we must login as root user to Mysql like so:

nikolas@admintuts:~$ nikolas@admintuts:~$ sudo mysql -u root -p
[sudo] password for nikolas:
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 5
Server version: 5.7.27-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2019, 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>

If you are getting a wrong password error, follow the instruction on how to reset your root mysql password. The password generated in this example with a random password generator. Please keep in mind that you can no longer login as root from a web interface application such as PhpMyadmin. To be able to login from a web interface, you will have to create a new user, and grant all privileges to it.

mysql> CREATE DATABASE IF NOT EXISTS wordpressdb CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Query OK, 1 row affected (0.00 sec)

mysql> CREATE USER 'dbusername'@'localhost' IDENTIFIED BY 'Xw$j$Rz8Ke3UB5@7';
Query OK, 0 rows affected (0.00 sec)

mysql>  <span class="cm-keyword">GRANT</span> <span class="cm-keyword">ALL</span> <span class="cm-keyword">PRIVILEGES</span> <span class="cm-keyword">ON</span> <span class="cm-variable-2">`wordpressdb`</span>.* <span class="cm-keyword">TO</span> <span class="cm-string">'dbusername'</span><span class="cm-variable-2">@'localhost'</span> <span class="cm-keyword">WITH</span> <span class="cm-keyword">GRANT</span> <span class="cm-keyword">OPTION</span>;
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye

At this point, the database configuration is finished.

Step 3 – Configure WordPress Configuration File

There are 3 things that need to be done here our WordPress installation to function properly:

  1. Renaming the file wp-config-sample.php to wp-config.php
  2. Passing in our database credentials and settings.
  3. Replace the dummy values that you see above, with fresh fetched ones from WordPress Api.

These are the dummy values in wp-config.php

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');

Now, it’s time to get to work with some real values. Renaming wp-config.php:

nikolas@admintuts:~$ cd /home/nikolas/example.com/html_public
nikolas@admintuts:~/example.com/html_public$ sudo mv wp-config-sample.php wp-config.php
nikolas@admintuts:~/example.com/html_public$ sudo vi wp-config.php

Set the database connection settings, that was created during MySql configuration:

/** The name of the database for WordPress */
define('DB_NAME', 'wordpressdb' );

/** MySQL database username */
define('DB_USER', 'dbusername' );

/** MySQL database password */
define('DB_PASSWORD', 'Xw$j$Rz8Ke3UB5@7' );

/** MySQL hostname */
define('DB_HOST', 'localhost' );

/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8' );

/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', 'utf8_unicode_ci' );

/** File System Access Method */
define('FS_METHOD', 'direct');

And last but not least, curl WordPress.org to fetch authentication keys, and salts:

nikolas@admintuts:~$ curl -s https://api.wordpress.org/secret-key/1.1/salt/

It will look something like that:

define('AUTH_KEY',         'o-oPAcY+og;f|/VPcl$|u:%!CnN|[FVLabip-c&[Ys-FM 0=~s `t[ZA+$/(.~+I');
define('SECURE_AUTH_KEY',  '~x9Z!b!(ra$ w;JgxeTKL{ YUwS)C&/kF4@Oi1,PL5%y<a|:u_+9if3-[ #ax}|7');="" define('logged_in_key',="" 'yqyx*e*yce|om*bcpipps="" gp54rcdy!v8a82.3qai#8rjg67b#qkhn2xc;icvqs?');="" define('nonce_key',="" '5?="" z+s-fu|{cbb`v^{uzo%="" (-gfoc-a="-_e*AYU$H)bM|k]0|2pWarnxNStBVboE');" define('auth_salt',="" '.ly4;2]lo="yWUrN[UGv|uh=c?{B;" `:u;jzzme~s)_j+)k#m="" 1pz-e_x(9y)2|95');="" define('secure_auth_salt',="" '{ja0njdgw@+ic{j,tmi2(ykp2d#k2lk&)|y;*ndj@qol-="a&vgy:([9-qTqFEwM=');" define('logged_in_salt',="" '="D){fz_|[K+Hg-C%ON?$j7|!do)AozbpM" .vqll1nkiyx-fhz="oH_OH#+p_WC&">e');
define('NONCE_SALT',       'AJ|xfj}|jenS fpNK},f:{7T+s}0c&ALGLhsbA0.-LV^u;X<%e!*y36;H]LsMMxe');</a|:u_+9if3-[>

Save, and close the file with :wq in vi editor.

Step 4 – Change Permissions Of Root Directory

Now change the permissions of your user folder like so:

nikolas@admintuts:~/example.com/html_public$ sudo chown nikolas:nikolas -R /home/nikolas
nikolas@admintuts:~/example.com/html_public$ sudo chmod 755 -R /home/nikolas

Notice that the full location to our WordPress root directory was not stated. That’s because of the -R argument of the chown command, which means Recursive. All sub-directories and their contents will have the same permission characteristics.

For now on, if you visit the ip address you provided in your Nginx configuration, you should be able to follow the setup instructions through the web interface.

Automated WordPress Installation Script

For automation reasons, is a perfect idea to use an automated script to take care of WordPress installation. It creates the WordPress database, the user to use said database, and update wp-config.php with the credentials you specify. All you have to do after, is to visit your site to continue the web interface installation with just a few clicks.

my_user=system_user_name
my_domain=domain_name
wp_database_name=wordpress_database_name
db_username=wordpress_database_username
db_username_password='wordpress_database_password'
db_root_password='mysql_root_password'

sudo apt install curl coreutils -y
mkdir /home/${my_user}/${my_domain}
mkdir /home/${my_user}/${my_domain}/html_public
cd /home/${my_user}/${my_domain}/html_public
sudo wget https://wordpress.org/latest.tar.gz
sudo tar xzf latest.tar.gz
sudo cp -a /home/${my_user}/${my_domain}/html_public/wordpress/. /home/${my_user}/${my_domain}/html_public
sudo rm latest.tar.gz
sudo rm -rf wordpress
echo "Renaming wp-config-sample to wp-config.."
sudo mv wp-config-sample.php wp-config.php
echo "Aplying wp-config.php details.."
sudo sed -i "/define( 'DB_NAME', 'database_name_here' );/c\define( 'DB_NAME', '${wp_database_name}');" /home/${my_user}/${my_domain}/html_public/wp-config.php
sudo sed -i "/define( 'DB_USER', 'username_here' );/c\define( 'DB_USER', '${db_username}');" /home/${my_user}/${my_domain}/html_public/wp-config.php
sudo sed -i "/define( 'DB_PASSWORD', 'password_here' );/c\define( 'DB_PASSWORD', '${db_username_password}');" /home/${my_user}/${my_domain}/html_public/wp-config.php
sudo sed -i "/define( 'DB_COLLATE', '' );/c\define( 'DB_COLLATE', 'utf8_general_ci');" /home/${my_user}/${my_domain}/html_public/wp-config.php
echo "Aplying priviledges.."
sudo chown ${my_user}:${my_user} -R /home/${my_user}
sudo chmod 755 -R /home/nikolas
echo "Creating wordpress database and GRANT new user to it..."
sudo mysql -u root -p$db_root_password < wp-config.php
	rm salts xx00 xx01 xx02
  cd $THE_PWD
done
echo "✔︎✔︎✔︎ All Done! WP-CONFIG FULLY UPDATED!!! 💯"
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *