Install WordPress on Ubuntu 17.04 / 17.10 with Nginx MariaDB and PHP

WordPress, the most popular content management system (CMS) is easy to install and configure on Ubuntu. This brief tutorial shows students and new users how to quickly get it done on Ubuntu 17.04 / 17.10 systems with support for Nginx, MariaDB and PHP.

Few days ago we showed you how to install WordPress with Apache2, MariaDB and PHP support. That post can be found here. In today’s post, instead of using Apache2 as our webserver of choice, we’re going to be using Nginx webserver.

Either Apache2 or Nginx will work with WordPress.. so it’s up to you as to which webservers you want to install.

To get stated with installing WordPress on Ubuntu with Nginx support, please follow the steps below.

Step 1: Install Nginx

WordPress requires a webserver to function. Most WordPress installations will support Apache2, but for this post we’re going to be using Nginx webserver instead. To install Nginx, run the commands below.

sudo apt-get update
sudo apt-get install nginx

After running the commands above, the commands below can be used to stop, start and enable Nginx service to always start up when the server is started.

sudo systemctl stop nginx.service
sudo systemctl start nginx.service
sudo systemctl enable nginx.service

STEP 2: INSTALL MARIADB

WordPress also requires a database server to function.. and MariaDB database server is a great place to start. To install it run the commands below.

sudo apt-get install mariadb-server mariadb-client

After installing, the commands below can be used to stop, start and enable MariaDB service to always start up when the server boots.

sudo systemctl stop mariadb.service
sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service

After that, run the commands below to secure MariaDB server.

sudo mysql_secure_installation

When prompted, answer the questions below by following the guide.

  • Enter current password for root (enter for none): Just press the Enter
  • Set root password? [Y/n]: Y
  • New password: Enter password
  • Re-enter new password: Repeat password
  • Remove anonymous users? [Y/n]: Y
  • Disallow root login remotely? [Y/n]: Y
  • Remove test database and access to it? [Y/n]:  Y
  • Reload privilege tables now? [Y/n]:  Y

After that, continue below to install PHP.

STEP 3: INSTALL PHP AND RELATED MODULES

WordPress also requires PHP to function. To install PHP and related modules run the commands below

sudo apt-get install php-fpm php-common php-mbstring php-xmlrpc php-soap php-gd php-xml php-intl php-mysqlnd php-cli php-mcrypt php-ldap php-zip php-curl

Some of the PHP packages above may depend on Apache2 which may get Apache2 webserver installed. You’ll want to disable that since we’re already running Nginx. To disable Apache2, run the commands below.

sudo systemctl disable apache2.service

STEP 4: CREATE WORDPRESS DATABASE

Now that you’ve installed all the packages that are required, continue below to start configuring the servers. First run the commands below to create WordPress database.

Run the commands below to logon to the database server. When prompted for a password, type the root password you created above.

sudo mysql -u root -p

Then create a database called wpdatabase

CREATE DATABASE wpdatabase;

Create a database user called wpuser with new password

CREATE USER 'wpdatabase'@'localhost' IDENTIFIED BY 'new_password_here';

Then grant the wpuser account full access to the database.

GRANT ALL ON wpdatabase.* TO 'wpuser'@'localhost' IDENTIFIED BY 'user_password_here' WITH GRANT OPTION;

Finally, save your changes and exit.

FLUSH PRIVILEGES;
EXIT;

STEP 5: DOWNLOAD WORDPRESS LATEST RELEASE

Next, run the commands below to download WordPress latest release.

cd /tmp && wget https://wordpress.org/latest.tar.gz

Then run the commands below to extract the download file to Nginx default root.

sudo tar -zxvf latest.tar.gz -C /var/www/html

Change modify the directory permission.

sudo chown -R www-data:www-data /var/www/html/
sudo chmod -R 755 /var/www/html/

STEP 6: CONFIGURE NGINX

Finally, configure Apahce2 site configuration file for WordPress. This file will control how users access WordPress’ content. Run the commands below to create a new configuration file called wordpress

sudo nano /etc/nginx/sites-available/wordpress

Then copy and paste the content below into the file and save it. Replace the highlighted line with your own domain name and directory root location.

server {
    listen 80;
    listen [::]:80;
    root /var/www/html/wordpress;
    index  index.php index.html index.htm;
    server_name  example.com www.example.com;

    location / {
    try_files $uri $uri/ /index.php?$args;        
    }

    location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info  ^(.+\.php)(/.+)$;
    fastcgi_index            index.php;
    fastcgi_pass             unix:/var/run/php/php7.0-fpm.sock;
    include                  fastcgi_params;
    fastcgi_param   PATH_INFO       $fastcgi_path_info;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

}

Save the file and exit.

STEP 7: ENABLE THE WORDPRESS SITE

After configuring the VirtualHost above, enable it by running the commands below

sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/

STEP 8 : RESTART NGINX

To load all the settings above, restart Apache2 by running the commands below.

sudo systemctl restart nginx.service

Step 9: Configure WordPress

Now that Nginx is configured, run the commands below to create WordPress wp-config.php file.

sudo mv /var/www/html/wordpress/wp-config-sample.php /var/www/html/wordpress/wp-config.php

Then run the commands below to open WordPress configuration file.

sudo nano /var/www/html/wordpress/wp-config.php

Enter the highlighted text below that you created for your database and save.

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'wpdatabase');

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

/** MySQL database password */
define('DB_PASSWORD', 'user_password_here');

/** 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', '');

After that, open your browser and browse to your domain name to launch WordPress configuration wizard

ex. http://myexample.com

WordPress default setup page

Follow the on-screen wizard until you’ve successfully setup WordPress.

Enjoy!

You may also like the post below:

Installing WordPress on Ubuntu 17.04 / 17.10 with Apache2, MariaDB and PHP