Install WordPress on Ubuntu 18.04 LTS with Nginx, MariaDB and PHP-FPM

I recently installed Ubuntu 18.04 LTS on VMware Workstation 14 Pro as guest machine for testing purposes… For those who want to learn how to install WordPress on the upcoming Ubuntu 18.04 LTS, the steps below show you how.

WordPress is an open source software that anyone can use to create powerful and dynamic websites based on PHP. Many large and popular websites online run WordPress.

This brief tutorial will show students and new users how to install WordPress on Ubuntu 18.04 LTS with Nginx, MariaDB and PHP-FPM support.

When  you’re ready to get WordPress setup on Ubuntu 18.04, continue with the steps below:

STEP 1: PREPARE AND UPDATE UBUNTU

It’s good to always update Ubuntu servers before installing packages… to update Ubuntu, run the commands below.

sudo apt update && sudo apt dist-upgrade && sudo apt autoremove

After updating Ubuntu, continue below with installing required packages for WordPress to work.

STEP 2: INSTALL Nginx WEB SERVER

After updating Ubuntu, run the commands below to install Nginx HTTP Server..

sudo apt install nginx

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

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

STEP 3: INSTALL MARIADB DATABASE SERVER

MariaDB database server is rapidly overtaking MySQL in the open source and Linux communities… MariaDB is the default database server on majority of Linux distributions… and WordPress requires a database server.. run the commands below to install MariaDB.

sudo apt 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 and create a new root password.

sudo mysql_secure_installation

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

  • Enter current password for root (enter for none): Just press 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

STEP 4: INSTALL PHP-FPM AND RELATED MODULES

Now that Nginx and MariaDB are installed, run the commands below to install PHP-FPM and related PHP modules on the new server. This is a good list of PHP modules to install.

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

After installing PHP, run the commands below to open PHP-FPM default configuration file.

sudo nano /etc/php/7.2/fpm/php.ini

Then scroll down the lines in the file and change the following lines below and save.

post_max_size = 100M
memory_limit = 256M
max_execution_time = 360
upload_max_filesize = 100M
date.timezone = America/Chicago

STEP 5: CREATE A BLANK WORDPRESS DATABASE

At this point, all the required WordPress packages and and servers are installed. The new server  is now ready to host WordPress… On the new server, create a blank WordPress database. WordPress will use this empty database to store its content.

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 blank database called WP_database  you can use the same database name from the old server.

CREATE DATABASE WP_database;

Create a database user called wp_user with new password.  You can use the same username and password from the old server.

CREATE USER 'wp_user'@'localhost' IDENTIFIED BY 'type_password_here';

Then grant the user full access to the database.

GRANT ALL ON WP_database.* TO 'wp_user'@'localhost' IDENTIFIED BY 'type_user_password_here' WITH GRANT OPTION;

Finally, save your changes and exit.

FLUSH PRIVILEGES;
EXIT;

STEP 6: DOWNLOAD WORDPRESS LATEST RELEASE

Next, visit WordPress site and download the latest…. or run the commands below to do that for you.

cd /tmp && wget https://wordpress.org/latest.tar.gz
tar -zxvf latest.tar.gz
sudo mv wordpress /var/www/html/wordpress

Then run the commands below to set the correct permissions for WordPress root directory.

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

STEP 7: CONFIGURE WORDPRESS

Next, run the commands below to create WordPress wp-config.php file. This is the default configuration file for WordPress.

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

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

/** MySQL database password */
define('DB_PASSWORD', 'new_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', '');

Save the file and you’re done.

STEP 8: CONFIGURE THE NEW WORDPRESS SITE

Next, configure the WordPress site configuration file on the server. 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 example.com with your own domain name.

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

     client_max_body_size 100M;

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

    location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass             unix:/var/run/php/php7.2-fpm.sock;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

Save the file and exit.

STEP 9: 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/

Now all is configured… run the commands below to reload Nginx web server and PHP-FPM settings.

sudo systemctl restart nginx.service
sudo systemctl restart php7.2-fpm.service

After restarting Nginx, open your browser and browse to the server IP address or hostname. If everything was setup correctly, you should see WordPress default configuration wizard.

http://example.com

WordPress default setup page

Follow the on-screen instructions until you’re successfully configured WordPress. When you’re done, login to the admin dashboard and configure WordPress settings.

Congratulations! You’ve just successfully installed WordPress.

You may also like the post below:

Upgrade Ubuntu 16.04 LTS to Ubuntu 18.04 LTS Server

13 Replies to “Install WordPress on Ubuntu 18.04 LTS with Nginx, MariaDB and PHP-FPM

  1. Im geting this error when installing your php command: ” Package ‘php-mcrypt’ has no installation candidate”

  2. Thanks for the tutorial.

    You have a typo here
    [quote]sudo systemctl restart php7.1-fpm.service[/quote]

    should be “sudo systemctl restart php7.2-fpm.service”

  3. When i try to create a database (with this: CREATE DATABASE WP_database;) i get this error:
    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that2
    How do i do?

  4. STEP 9: ENABLE THE WORDPRESS SITE
    [quote]sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/[/quote]
    ln: target ‘/etc/nginx/sites-enabled/’ is not a directory: No such file or directory

  5. When I tried adding another domain I got a 521 error to the second domain…what can I do?

  6. I followed this, but I get a 403 forbidden or the default nginx page when I navigate to the www server IP address. How can I fix this?

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.