Today’s guide is a step-by-step Installation of WordPress with Nginx on Ubuntu 18.04 / Debian 9 Linux system. WordPress is the most popular, advanced, feature-rich and open-source content management system written in PHP. WordPress stores its data in MySQL relational database.
Though wordpress is more known to be a blogging platform, it supports other types of web content management like media galleries, forums, mailing lists, and E-commerce. If you’re trying to find the best Content Management system for your site or blog, give WordPress a shot, you’ll probably love it.
Install WordPress with Nginx on Ubuntu 18.04 / Debian 9
Follow steps below to install Wordpress with Nginx on Ubuntu 18.04 / Debian 9.
Step 1: Install MariaDB / MySQL Database Server
You can choose to use MariaDB or MySQL database server. We’ll install MariaDB database server:
sudo apt update
sudo apt install mariadb-server
After the installation of the database server access root user shell:
sudo mysql -u root
Create a database and user for WordPress
CREATE USER 'wp_user'@'localhost' identified by 'StrongPassword';
CREATE DATABASE wp_db;
GRANT ALL PRIVILEGES ON wp_db.* TO 'wp_user'@'localhost';
FLUSH PRIVILEGES;
Don’t forget to replace StrongPassword
with your database user password.
Confirm if the user can connect to the database with the provided password:
$ mysql -u wp_user -p
Enter password: <ENTER PASSWORD>
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 34
Server version: 10.1.34-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| wp_db |
+--------------------+
2 rows in set (0.00 sec)
MariaDB [(none)]> QUIT
Bye
Step 2: Install PHP and required Extensions
The default PHP available on both Ubuntu 18.04 and Debian 9 is supported by WordPress. We’ll install PHP and required extensions such as fpm,mysql,zip
e.t.c.
sudo apt install php php-{fpm,pear,cgi,common,zip,mbstring,net-socket,gd,xml-util,mysql,gettext,bcmath}
Step 3: Install Nginx Web Server
Now that you have a database for WordPress created, we can install Nginx web server before downloading and configuring WordPress.
Install Nginx on Ubuntu 18.04 or Debian 9 by running below command:
sudo apt update
sudo apt install nginx
Step 4: Download and Install WordPress
Download the latest release of WordPress:
sudo apt install wget vim
wget wordpress.org/latest.tar.gz
Extract the archive file
tar xvf latest.tar.gz
Move the resulting directory to your Web Document Root
sudo mv wordpress /srv/myblog
Configure WordPress Database connection
cd /srv/myblog
sudo cp wp-config-sample.php wp-config.php
sudo vim wp-config.php
Edit wp-config.php
define('DB_NAME', 'wp_db');
define('DB_USER', 'wp_user');
define('DB_PASSWORD', 'StrongPassword');
Change ownership of /srv/myblog
to web user:
sudo chown -R www-data:www-data /srv/myblog
Step 5: Configure Nginx and Finish WordPress installation
We need to create a Virtual Host configuration file for our WordPress website.
sudo vim /etc/nginx/conf.d/myblog.conf
Add content like below:
##################################
# WORDPRESS NGINX CONFIGURATIONS
##################################
server {
listen 80;
root /srv/myblog;
server_name example.com;
access_log /var/log/nginx/wp_client_access.log;
error_log /var/log/nginx/wp_client_error.log;
location / {
index index.php index.html;
try_files $uri $uri/ /index.php?$args;
}
# Specify a charset
charset utf-8;
# GZIP
gzip off;
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
# Prevents hidden files (beginning with a period) from being served
location ~ /\. {
access_log off;
log_not_found off;
deny all;
}
###########
# SEND EXPIRES HEADERS AND TURN OFF 404 LOGGING
###########
location ~* ^.+.(xml|ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
access_log off;
log_not_found off;
expires max;
}
# Pass all .php files onto a php-fpm or php-cgi server
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_read_timeout 3600s;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 128k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
#fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
}
# ROBOTS
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# RESTRICTIONS
location ~* /(?:uploads|files)/.*\.php$ {
deny all;
}
}
Verify the configuration syntax:
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Restart Nginx Web server
sudo systemctl restart nginx
sudo systemctl enable nginx
Visit the WordPress installation wizard page on https://example.com
to finish installation.
1.
Select Language
2.
Provide Site Title, Admin username, Password and Email Address
3
Click Install WordPress button to finish WordPress Installation.
4.
Login to the WordPress Administration Dashboard with the set username and Password
You should get to WordPress Dashboard which looks like below.
Check our article on how to use Gmail SMTP with WordPress:
If you need WordPress Multisite Network Setup, refer to the guide below:
How To Setup WordPress Multisite Network installation
For Caddy Web Server, see:
How to Host WordPress website with Caddy Web Server