How to Install WordPress with NGINX PHP7 PHPMyAdmin

Published in

on

Install WordPress on NGINX PHP7 with PHPMyAdmin Ubuntu16.04

Here is this article we are going to see how we can install WordPress on NGINX with latest and super fast PHP7 with PHPMyAdmin on Ubuntu 16.04 which is the newest Debian/Ubuntu distribution as of now. We will use MariaDB in replacement of MySQL for database uses and will also install Postfix service to basic internal contact form support. Here is a quick list of what we are going to use…

  • Debian 9 / Ubuntu 16.04 LTS
  • NGINX (Latest version 1.9.15)
  • PHP7.0-FPM (Lightweight PHP)
  • MariaDB (Latest version 10.0.24)
  • PHPMyAdmin (For easy database access and management)
  • Postfix (Basic: Needed for contact forms to work)

Let’s set up a VPS or Dedicated machine which is running on Ubuntu 16.04 LTS, I will suggest LTS only because they have a long-term support and stable enough to secure your machine. I personally like DigitalOcean as it is best and leading cloud hosting company based on Fast SSD hard drive which makes things get processed super fast.

Update the Server for Security Patches

sudo apt-get update

Install NGINX

sudo apt-get install nginx

Install PHP7

sudo apt-get install php7.0-fpm php7.0-cli php7.0-common php7.0-curl php7.0-gd php7.0-mbstring php7.0-mcrypt php7.0-mysql php7.0-xml php7.0-xmlrpc php7.0-zip
sudo phpenmod mbstring
sudo phpenmod mcrypt

Now here we need to secure the PHP7.0 since it has a small loop for hackers (don’t be panic) by default for which we need to edit PHP.ini file. We can use any text editor like nano or vim. In our case, we are using my favorite editor nano.

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

Now find “cgi.fix_pathinfo” without quotation mark. Use Ctrl+W to activate the search function in nano editor. Now you will see that its value is set to 1 by default like this cgi.fix_pathinfo=1, so change its value to 0 like this: cgi.fix_pathinfo=0 and save it via Ctrl+O.

Install MariaDB

sudo apt-get install mariadb-client mariadb-server

Since we need to tough the security loopholes let’s make MySQL more secure…

sudo mysql_secure_installation

In order to log into MariaDB to secure it, we’ll need the current
password for the root user. If you’ve just installed MariaDB, and
you haven’t set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on…

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
… Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y
… Success!

Normally, root should only be allowed to connect from ‘localhost’. This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y
… Success!

By default, MariaDB comes with a database named ‘test’ that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y
– Dropping test database…
ERROR 1008 (HY000) at line 1: Can’t drop database ‘test’; database doesn’t exist
… Failed! Not critical, keep moving…
– Removing privileges on test database…
… Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
… Success!

Cleaning up…

All done! If you’ve completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

Now here we have successfully installed MariaDB. Since managing database from command line interface is time-consuming we are going to install PHPMyAdmin here which will let you manage the database from a Graphical User Interface.

Install PHPMyAdmin

sudo apt-get install phpmyadmin
sudo service nginx restart

While setup you will be asked to choose web server (Apache or Lighttpd) where we will choose none (use tab to navigate) and select Okay. Then we will be asked for configuring database we will choose Yes there and then we will be asked for a password for accessing PHPMyAdmin.

Since root user is disabled in this setup we need to create another root user which we can use to login in PHPMyAdmin, here is how to set up a new root user.

mysql -u root -p
CREATE USER 'user'@'localhost' IDENTIFIED BY 'userPassword';
GRANT ALL PRIVILEGES ON *.* TO 'user'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
exit

Here ‘user‘ can be any user you want to choose and use a strong password for that user.

PHPMyAdmin will be accessible from http://IP_or_DomainName/phpmyadmin just make sure that you have pointed your domain name in advance while accessing PHPMyAdmin by the domain name.

Install Postfix

sudo apt-get install postfix

Without Postfix we won’t be able to interact with email functions like forget password email and contact us forms so we need to install one client software and postfix is one which we used here and while setup we do not have to configure anything here just hit okay and let all the value set to default.

Configure NGINX’s Server Blocks

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

Now delete everything using control+k and paste the following configuration which I have made easy to use.

server {
    listen 80 default_server;
    root /var/www/html;
    index index.php index.html index.htm;
    server_name atulhost.com www.atulhost.com;
location / {
    try_files $uri $uri/ /index.php?$args;
    }
location ~ \.php$ {
    try_files $uri =404;
    fastcgi_index index.php;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    }
location ~* \.(js|css|png|jpg|jpeg|gif|ico|eot|otf|ttf|woff)$ {
    add_header Access-Control-Allow-Origin *;
    add_header Cache-Control "public, max-age=31536000, immutable";
    access_log off; log_not_found off;
    }
location = /robots.txt { access_log off; log_not_found off; }
location ~ /\. { deny all; access_log off; log_not_found off; }
location /phpmyadmin {
    root /usr/share/; index index.php;
    location ~ ^/phpmyadmin/(.+\.php)$ {
        try_files $uri =404;
        fastcgi_index index.php;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
    location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$
        { root /usr/share/; }
    }
}

Do not forget to change the domain name from the above configurations. I have used non-www as well as www version of the domain name because we can set anyone default in WordPress itself in Settings » General under WordPress Address (URL) and Site Address (URL) sections.

At the end restart all the services once.

sudo service nginx restart
sudo service php7.0-fpm restart
sudo service mysql restart

Now the most difficult past…

Install WordPress

Now the WordPress Download… and further configs.

cd /var/www/html
sudo wget https://wordpress.org/latest.zip
sudo apt-get install zip
sudo unzip latest.zip
cd wordpress
sudo mv * /var/www/html
cd ..
sudo rm -rf wordpress
sudo chown -R www-data:www-data /var/www/

Now we have WordPress files ready and now need to connect it with a Database.

  1. Open PHPMyAdmin in a browser and create a database over there. Enter into that database and move to Privileges Tab and click on Add User.
  2. In login information give a username to the database, set host to localhost and give a password. (Better you use a Password Generator option there)
  3. Not down Database name, Username, and Password. (We need to add them in wp-config.php file)
  4. Under Database for user section Check/Tick on Grant all privileges on database “DATABASE_NAME”.
  5. Now click on Go. (Do not check/tick anything under Global privileges section)

Now we have a database ready. Let’s connect WordPress to Database.

sudo mv wp-config-sample.php wp-config.php
sudo nano wp-config.php

Edit database name, username, and password; once done save the file. Now go ahead in run WordPress setup from the browser via IP or Domain Name if you have pointed that in advanced.

Finally, we have installed WordPress on NGINX PHP7 with PHPMyAdmin on latest Ubuntu 16.04.

Recommended Tutorials

I hope you liked our work if you have any doubt or query regarding this setup let me know by leaving them in the comment section below.

Leave a Reply

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

Responses to “How to Install WordPress with NGINX PHP7 PHPMyAdmin”

  1. Thu Thuat Avatar
    Thu Thuat

    Best article, thanks so much, hope you will write more tut about VPS config, thanks

  2. reddit rules Avatar
    reddit rules

    Beautiful setup tutorial. Thanks for this. It’s a fast, concise process. Well done.

  3. waqas Avatar
    waqas

    Great work. Thank you. Well-documented tutorial. Do you plan to write about wordpress caching for blazing site. I need it for thousands of user online at the same time and creating posts and ajaxing through site?

    1. Atul Avatar

      Hi Waqas, yes I am planning to continue on server side caching solutions here (probably FastCGI Cache/NGINX MicroCache), it is easy to use and works out of the box.

  4. waqas Avatar
    waqas

    I tried this one and the “WordPress on NGINX with FastCGI Cache in Ubuntu” as well. The second is slightly better. so I’m going to stick with older versions.

    1. Atul Avatar

      Welcome back Waqas, Thanks for your feedback. I am glad that you tried our both setup. First of all I want to tell you that FastCGI uses a server side cache mechanism, that means the load on PHP is too less even under heavy load. That’s why you can use this a robust setup, whereas PHP7 is bit new here it will take some more time to get things on quick install.

  5. suniel Avatar
    suniel

    Nice post. I set up my WordPress blog in localhost. But I am getting problem with the permalinks. Whenever I change my permalinks I get a 404 error. It would be great if you could help me with that.

    Here is my current default configuration,

    location / {
            try_files $uri $uri/ /index.php?q=$uri&$args;
    }
    1. Atul Avatar

      Try below code and your issue will be resolved.

      location / {
       try_files $uri/ /index.php?$args;
      }
  6. Abhisek Padhi Avatar
    Abhisek Padhi

    Hi Atul, Good work, but I’d like to point out few bad practices. No offence, but since this post is geared towards newbies, the post contains lots of bad practices and misconceptions. If I start listing them, there would be many, and I’d just be ranting. So, please research on good practices first, because seasoned sysadmins will notice what is wrong with your post, as I can, but newcomers who are starting to use a *nix system to host wordpress will take your words for granted. For starters I can point that – you are leaving phpmyadmin open to whole world, which in itself is a huge blunder. Similarly, lots of bad practices can be found in this post.

    1. AtulHost Avatar

      Hi Abhishek, I appreciate that you pointed out one important thing which is really important. In this tutorial we made it open and easily accessible for users. However you can rename, move or password protect the PHPMyAdmin directory, as it is a shortcut folder located at the root directory where WordPress is installed. If you want to password protect the directory just follow this small step.

      Drop a .credential file in the root directory. Make sure that this file should encrypted using htpasswd method. The file must contains username and password like htpasswd format like username:password. Follow this htpasswd generator tool to generate encrypted password.

      Add the following line inside your server { … } block.

      location /phpmyadmin { auth_basic “Restricted”; auth_basic_user_file /var/www/html/.credential; }

      All set just reload/restart NGINX and try to accessing the PHPMyAdmin it will ask you credentials first. I hope this will be helpful.

  7. Andy Avatar
    Andy

    Well, just follow your step, but can’t finished the php installation, things changed?

    1. AtulHost Avatar

      1. Are you on Ubuntu 16.04 and above?
      2. Have you apt-get update the system once?