How to Install WordPress on Nginx / LEMP Ubuntu Server
| |Being a well-known blogging platform, WordPress has been used by hundreds thousands of users and having millions installations. Well, you knew that. Hence, I will not explain more about WordPress. The main objective of this guide is to tell you and many newbies out there on how to install WordPress, step by step with pictures, on an Ubuntu server with full LEMP stack which you have to do it manually since you have no hosting control on it. LEMP or LNMP stack stands for Linux, Nginx, MySQL and PHP. I have explained about how to build a ready-to-use Ubuntu server based on Nginx, MySQL, and PHP7 to host your website on it. I have also explained how to set up Nginx Server Blocks so it can host many websites / domains on it. This article is just a continuation of those previous articles.
REQUIREMENTS
- A SSH client like Bitvise or Putty and basic knowledge about how to use it.
- Basic knowledge about common Unix command to SSH to your server.
- A server or VPS with at least 256MB of RAM (OpenVZ) but 512MB or more is recommended if you are using KVM or Xen.
- Ubuntu 16.04 either 32-bit or 64-bit.
- About 30 minutes of your time.
- a cup of coffee or tea.
Confused on choosing which VPS provider to setup an Apache based hosting server? Read my recommended VPS providers or this list of top low end cloud server providers. Quick recommendation: Digital Ocean, Atlantic.net or Ramnode.
In this guide I am using a 512MB RAM Cloud VPS from Atlantic running Ubuntu 16.04 x64. Unfortunately, this plan is not available any more – as what I can see at their website. However, Atlantic.net provides solid and reliable servers so having the smallest cloud server plan (1GB RAM) is really worth is price and it is very suitable to host your production server.
PREREQUISITES
- Provision the Ubuntu server.
- Install full LEMP on it. Read – How to provision a cloud server at Atlantic.net and how to install full LEMP stack on Ubuntu.
- Do not forget to always replace domain.tld or servermom.net with your own domain name and replace x.x.x.x with your server IP address.
- Setup Nginx Server Block for your domain. Read – How to add new website and setup Nginx Server Block.
- In this tutorial I simply login as root. However, it is advised to login as another user with sudo privilege. Read – Basic Ubuntu Setup to build a production server. Therefore, please add sudo in every and each command.
I’ve explained how to install Nginx, PHP7, and MySQL on my previous post.
Therefore, I assumed at this point you have an Ubuntu server with Nginx, PHP7, and MySQL installed as well as you have created Nginx Server Block for your domain. Let’s get going..
Preparing Database
Step 1 – If you haven’t done it yet, login to your server as root or as a user with root privilege. Make sure you add sudo in front of any command below if you are logging in as sudoer.
Step 2 – Now login to MySQL as root user using password you have defined earlier (see my previous tutorial on full LEMP setup).
mysql -u root -p
Step 3 – Create new database by issuing command below.
CREATE DATABASE dbname DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Replace dbname with your own database name. Just make up a name but make sure you remember it or note it down. In this example I use servermomnet as my database name.
Step 4 – Now create new database user and password while granting it full access to the newly created database. That task can be done simply by issuing command below :
GRANT ALL ON dbname.* TO 'bloguser1'@'localhost' IDENTIFIED BY 'servermom12345';
Replace bloguser1 with your own database username and replace servermom12345 with your own password. Remember that you should not use the same password as the one you have been using as database root password. Example :
Step 5 – Finally, refresh database privileges and exit MySQL interface :
FLUSH PRIVILEGES; exit
Adjusting Nginx Configuration
Step 1 – Edit Nginx Server Block of your website that you have created earlier.
nano /etc/nginx/sites-available/domain.tld
example :
nano /etc/nginx/sites-available/servermom.net
Step 2 – Now put comment mark (#) in front of old configuration line (try_files $uri $uri/ =404;) and add new one below
try_files $uri $uri/ /index.php$is_args$args;
this will make sure that you can make use of WordPress pretty permalink / SEO-friendly permalink.
Step 3 – Next, you may need to add these lines inside that nginx server block file. This is not really necessary but it can tweak your site’s performance and score better in Google Page Speed score.
location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { log_not_found off; access_log off; allow all; } location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ { expires max; log_not_found off; }
Eventually, it will look like something like this
Do not forget to save changes (Control+O) then exit the editor (Control+X).
Step 4 – Test if nginx configuration you have adjusted is fine and then reload nginx.
nginx -t service nginx reload
Step 5 – Now install additional PHP modules necessary for WordPress to run properly :
apt-get install php-curl php-gd php-mbstring php-mcrypt php-xml php-xmlrpc
Remember to add sudo before the command above if you are logged in as sudoer.
The process may take for awhile. Just wait till it finished.
Step 6 – Finally, restart PHP-fpm service
service php7.0-fpm reload
Installing WordPress
Step 1 – Go to /tmp folder and download WordPress there :
cd /tmp curl -O https://wordpress.org/latest.tar.gz
Step 2 – Extract the newly downloaded WordPress package
tar -xzvf latest.tar.gz
Step 3 – Now copy WordPress config sample file and edit it:
cp wordpress/wp-config-sample.php wordpress/wp-config.php nano wordpress/wp-config.php
Step 4 – Now edit the details in MySQL Settings.
Before :
Put the MySQL database name, MySQL username, and password:
Do not quit Nano editor yet.
Step 5 – Scroll down the page a little bit until you see Salt section. Open up your web browser and open this url :
https://api.wordpress.org/secret-key/1.1/salt/
You’ll get random Salt codes. Copy that :
Step 6 – Now paste that code in wp-config.php file you are currently editing (delete the existing codes and put the new one):
Do not forget to save changes (Control+O) then exit the editor (Control+X).
Step 7 – Now create new directory / folder called upgrade
mkdir wordpress/wp-content/upgrade
Step 8 – Let’s move the whole WordPress files and folders to the web root directory you have created :
cp -a /tmp/wordpress/* /var/www/domain.tld/html chown -R www-data:www-data /var/www/domain.tld/html
Replace domain.tld with your own. Example
Step 9 – Next, issue following command line by line to setup proper file permission:
find /var/www/domain.tld/html -type d -exec chmod g+s {} \; chmod g+w /var/www/domain.tld/html/wp-content chmod -R g+w /var/www/domain.tld/html/wp-content/themes chmod -R g+w /var/www/domain.tld/html/wp-content/plugins
Again, replace all domain.tld parts in those command lines. Example
Step 10 – One thing that I almost forgot, re-edit wp-config.php file.
nano /var/www/domain.tld/html/wp-config.php
Step 11 – Add this line right after define DB_COLLATE (see screenshot):
define('FS_METHOD', 'direct');
This step is necessary to avoid WordPress from asking for FTP credentials when you are doing Plugins or Themes update. Save changes (Control+O) then exit nano editor (Control+X).
Completing Installation
Step 1 – Just open up your web browser again and proceed with regular WordPress install:
Choose the language you want and click the “Continue” button.
Step 2 – Fill in all required details and click the “Install WordPress” button.
Step 3 – That’s it. You may now login to your newly installed WordPress blog. Click on that Login button :
Congratulation. You have successfully installed WordPress on your Ubuntu server powered with Nginx, PHP7 fpm and MYSQL. This is the basic configuration of a fast webserver. You can still tweak its performance by adjusting nginx configuration like adding either server-based caching or script-based caching (WP Super Cache, W3 Total Cache, etc.).
Now test if you can use Pretty permalink and install new plugin without having to define FTP credentials.
Success! WordPress SEO / Pretty Permalink works!
Now go ahead install new plugin to test if the configuration is ok. You will see something like this indication that you can now install and update any plugin without having to be asked for FTP login details.
Congratulation and happy blogging.
Most common question : How many visitors this LEMP + WordPress configuration can handle? I will try to do a quick benchmark test in my next post. So, stay tuned. Subscribe to my newsletter or follow me on twitter.
great tutorial,will be awesome to see how install wordpress on lighttpd+free ssl from letsencrypt 🙂
Congratulations!!! Another Tutorial Amazing very well detailed.
Your website is my query basis for questions about servers, optimization of servers and open source hosting control panel.
Posts My suggestion: Server Optimization for High-Performance and Security Configuration Servers.
Sawiyati, Once again congratulations on the excellent content of your website.
Great! With your help i migrated my wordpress install to a fresh ubuntu ssd vps with nginx instead apache and gained a lot more power. Thank you!
The best tutorial ever, bro!
Works like a charm!
Thank you very much, excellent job! 😉
Just wondering, is the setup described in this article better than Mercury Vagrant ? https://github.com/wpengine/hgv
I already have a few Virtual servers, but for the same price as them i can get a dedicated server in same locating, with better specs, so i’m thinking about doing that.
Does configuration works on the same way as a VPS, and is it the same software and commands i’m using?
Also i guess i just can create folders, and run different tasks inside the folders?
Ex. one folder called webhost, managing all my websites and then one folder called teamspeak to host my teamspeak server and one folder called gameservers hosting my game servers?
Nice and detailed tutorial 🙂
Thanks for covering every single step for newbiews like me.