How to Install WordPress with HHVM and Nginx on CentOS 7

July 3, 2017 1 comments

Steps

HHVM (HipHop Virtual Machine) is an open source virtual machine for executing programs written in PHP and Hack language. HHVM has been developed by Facebook, it provides most features of the current PHP 7 version. To run HHVM on your server, you can use a FastCGI to connect HHVM with a Nginx or Apache web server, or you can use the web server built into HHVM called “Proxygen”.

In this tutorial, I will show you how to install WordPress with HHVM and Nginx as web server. I will use CentOS 7 as the operating system, so basic knowledge of CentOS is required.

Prerequisite

  • CentOS 7 – 64bit
  • Root privileges

Step 1 – Configure SELinux and add the Epel Repository

In this tutorial, we will use SELinux in enforcing mode, so we need the SELinux management tools installed on the system. We will use setools and setrobleshoot to manage SELinux policies.

By default, SELinux is enabled on CentOS 7, We can check that for with command below:

root@ashokkuikel ~]# sestatus
root@ashokkuikel ~]# getenforce

Check SELinux

You can see SELinux is enabled with enforcing mode.

Next, Install setools and setroubleshoot with the yum command.

root@ashokkuikel ~]# yum -y install setroubleshoot setools net-tools

When the installation has been completed, you can install the EPEL repository.

root@ashokkuikel ~]# yum -y install epel-release

Step 2 – Install Nginx

Nginx or engine-x is a lightweight web server with high performance and low memory consumption. On CentOS, we can use yum to install the Nginx packages. Make sure you are logged in as root user!

Install nginx with this yum command from the CentOS repository:

root@ashokkuikel ~]# yum -y install nginx

Now start Nginx and enable it to be started at boot time with the systemctl command:

root@ashokkuikel ~]# systemctl start nginx
root@ashokkuikel ~]# systemctl enable nginx

To ensure Nginx is running on our server, visit the server IP address with your browser, or use the curl command as shown below to get the results:

root@ashokkuikel ~]# curl <your_ip> my_ip i.e 192.168.1.110

I’ll cehck it with my web browser here:

Nginx is running

Step 3 – Install and Configure MariaDB

MariaDB is an open source database developed by the original MySQL developer Monty Widenius, it has been forked from the MySQL database but stays compatible with it in its major functions. In this step, we will install MariaDB and configure the root password for the MariaDB database. Then we will create a new database and new user that are required for our WordPress installation.

Install mariadb and mariadb-server:

root@ashokkuikel ~]# yum -y install mariadb mariadb-server

Start MariaDB and add the service to be automatically started at boot time:

root@ashokkuikel ~]# systemctl start mariadb
root@ashokkuikel ~]# systemctl enable mariadb

MariaDB has been started, and now we have to configure the root password for the mariadb/mysql database. Type in the command below to setup the MariaDB root password.

root@ashokkuikel ~]# mysql_secure_installation

Type in your new password for the MariaDB root user when requested.

Set root password? [Y/n] Y
New password:
Re-enter new password:

Remove anonymous users? [Y/n] Y
… Success!
Disallow root login remotely? [Y/n] Y
… Success!
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y

… Success!

The MariaDB root password has been configured. Now login to the MariaDB/MySQL shell and create a new database “wordpressdb” and a new user “wpuser” with password “wpuser@” for our WordPress installation. Choose a secure password for your installation!

Login to the MariaDB/MySQL shell:

root@ashokkuikel ~]# mysql -u root -p
TYPE YOUR PASSWORD

Create a new database and new user

MariaDB [(none)]> CREATE USER ‘master’@’localhost’ IDENTIFIED BY ‘password’;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> GRANT ALL PRIVILEGES ON  wpdb . * TO ‘master’@’localhost’;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

MariaDB has been installed and the new database for our WordPress installation has been created.

Step 4 – Install HHVM

For the HHVM installation, we need to install many dependencies. We can install HHVM from source by downloading the source from github, or installing prebuilt packages that are available on the internet. In this tutorial, I will install HHVM from prebuilt packages.

Install the dependencies for the HHVM installation

root@ashokkuikel ~]# yum -y install cpp gcc-c++ cmake git psmisc {binutils,boost,jemalloc,numactl}-devel {ImageMagick,sqlite,tbb,bzip2,openldap,readline,elfutils-libelf,gmp,lz4,pcre}-devel
lib{xslt,event,yaml,vpx,png,zip,icu,mcrypt,memcached,cap,dwarf}-devel
{unixODBC,expat,mariadb}-devel lib{edit,curl,xml2,xslt}-devel
glog-devel oniguruma-devel ocaml gperf enca libjpeg-turbo-devel openssl-devel
mariadb mariadb-server libc-client make

Then install the HHVM prebuilt packages from this site with the rpm command.

root@ashokkuikel ~]# rpm -Uvh http://mirrors.linuxeye.com/hhvm-repo/7/x86_64/hhvm-3.15.2-1.el7.centos.x86_64.rpm
root@ashokkuikel ~]# ln -s /usr/local/bin/hhvm /bin/hhvm

HHVM has been installed, check it with the command below:

root@ashokkuikel ~]# hhvm –version

In order to use the php command, we can set the hhvm command as php. So when you type ‘php’ on the shell, you will see the same result as from the hhvm command.

root@ashokkuikel ~]# sudo update-alternatives –install /usr/bin/php php /usr/bin/hhvm 60
php –version

Step 5 – Configure HHVM

In this step, we will configure HHVM. We will run hhvm as a systemd service. Instead of running it on a system port, we will run hhvm on a unix socket file which is faster.

Go to the systemd directory and create the hhvm.service file.

root@ashokkuikel ~]# cd /etc/systemd/system/
root@ashokkuikel ~]# vim hhvm.service

  1. Paste the service configuration belowinto that file.

[Unit]
Description=HHVM HipHop Virtual Machine (FCGI)
After=network.target nginx.service mariadb.service

[Service]
ExecStart=/usr/local/bin/hhvm --config /etc/hhvm/server.ini --user nginx --mode daemon -vServer.Type=fastcgi -vServer.FileSocket=/var/run/hhvm/hhvm.sock

[Install]
WantedBy=multi-user.target

2. Save the file and exit vim.

Next, go to the hhvm directory and edit the server.ini file.

root@ashokkuikel ~]# cd /etc/hhvm/
root@ashokkuikel ~]# vim server.ini

Replace the hhvm.server.port on line 7 with unix socket configuration below:

hhvm.server.file_socket = /var/run/hhvm/hhvm.sock

Save the file and exit th editor.

In the hhvm service file, we’ve defined that hhvm is running under the ‘nginx’ user, so we must change the owner of the socket file directory to the ‘nginx’ user. Then we must change the SELinux context of the hhvm directoryto allow access to the socket file.

root@ashokkuikel ~]# chown -R nginx:nginx /var/run/hhvm/
root@ashokkuikel ~]# semanage fcontext -a -t httpd_var_run_t “/var/run/hhvm(/.*)?”
root@ashokkuikel ~]# restorecon -Rv /var/run/hhvm

After rebooting the server, hhvm will not be running because there is no directory for the socket file, so we must create it automatically at the boot time.

Edit the rc.local file with vim.

root@ashokkuikel ~]# vim /etc/rc.local

Paste configuration below to the end of line.

mkdir -p /var/run/hhvm/
chown -R nginx:nginx /var/run/hhvm/
semanage fcontext -a -t httpd_var_run_t "/var/run/hhvm(/.*)?"
restorecon -Rv /var/run/hhvm

Save the file and exit vim. Make the file executable.

root@ashokkuikel ~]# chmod +x /etc/rc.local

Reload the systemd service, start hhvm and add it to be started at boot time.

root@ashokkuikel ~]# systemctl daemon-reload
root@ashokkuikel ~]# systemctl start hhvm
root@ashokkuikel ~]# systemctl enable hhvm

Make sure that there is no error. Check that hhvm is running under the socket file with the netstat command.

root@ashokkuikel ~]# netstat -pl | grep hhvm

Step 6 – Configure HHVM and Nginx

In this step, we will configure HHVM to run with the Nginx web server. We need to create a new hhvm configuration file in the Nginx directory.

Go to the /etc/nginx directory and create a hhvm.conf file.

root@ashokkuikel ~]# cd /etc/nginx/
root@ashokkuikel ~]# vim hhvm.conf

Paste the configuration below:

location ~ .(hh|php)$ {
    root /usr/share/nginx/html;
    fastcgi_keep_conn on;
    fastcgi_pass unix:/var/run/hhvm/hhvm.sock;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

Save and exit.

Next, edit the nginx.conf file and add the hhvm configuration include line.

root@ashokkuikel ~]# vim nginx.conf

Add the configuration to the server directive line 57.

include /etc/nginx/hhvm.conf;

Save and exit.

Then change the SELinux context of the hhvm configuration file.

root@ashokkuikel ~]# semanage fcontext -a -t httpd_config_t /etc/nginx/hhvm.conf
root@ashokkuikel ~]# restorecon -v /etc/nginx/hhvm.conf

Test the Nginx configuration and restart the service.

root@ashokkuikel ~]# nginx -t
root@ashokkuikel ~]# systemctl restart nginx

Make sure there is no error.

Step 7 – Create a Virtual Host with HHVM and Nginx

In this step, we will create a new virtual host configuration with Nginx and hhvm. I will use the domain name “natsume.co” for this example. Please use your own domain name and replace it in the configuration files and WordPress installation wherever it appears.

Go to the nginx conf.d directory where we will store virtual host file:

root@ashokkuikel ~]# cd /etc/nginx/conf.d/

Create the new configuration “natsume.conf” with vim:

root@ashokkuikel ~]# vim natsume.conf

Paste the virtual host configuration below:

server {
listen 80;
server_name natsume.co;

# note that these lines are originally from the "location /" block
root /var/www/ashok;
index index.php index.html index.htm;

location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
location = /50x.html {
root /var/www/ashok;
}

location ~ .php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/hhvm/hhvm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

Save and exit.

In our virtual host configuration, we’ve defined the web root directory for the domain name to be the “/var/www/ashok” directory. That directory does not exist yet, so we have to create it and change the ownership to the nginx user and group.

root@ashokkuikel ~]# mkdir -p /var/www/ashok
root@ashokkuikel ~]# chown -R nginx:nginx /var/www/ashok

Next, configure the SELinux context for the file and directory.

root@ashokkuikel ~]# semanage fcontext -a -t httpd_config_t “/etc/nginx/conf.d(/.*)?”
root@ashokkuikel ~]# restorecon -Rv /etc/nginx/conf.d

Finally, test the nginx configuration to ensure there is no error, then restart nginx:

root@ashokkuikel ~]# nginx -t
root@ashokkuikel ~]# systemctl restart nginx

Step 8 – Install WordPress

In step 5 we’ve created the virtual host configuration for our WordPress installation. Now we just have to download WordPress and edit the database configuration by using the database and user that we’ve created in step 3.

Go to the web root directory “/var/www/ashok”  and download WordPress with the wget command:

root@ashokkuikel ~]# cd /var/www/ashok
root@ashokkuikel ~]# wget wordpress.org/latest.tar.gz

Extract “latest.tar.gz”  and move all WordPress files and directories to the current directory:

root@ashokkuikel ~]# tar -xzvf latest.tar.gz
root@ashokkuikel ~]# mv wordpress/* .

Next, copy “wp-config-sample.php” file to “wp-config.php” and edit it with vim:

root@ashokkuikel ~]# cp wp-config-sample.php wp-config.php
root@ashokkuikel ~]# vim wp-config.php

Set DB_NAME to “wordpressdb”, DB_USER to “wpuser” and DB_PASSWORD to “wpuser@”.

define('DB_NAME', 'wordpressdb');
define('DB_USER', 'wpuser');
define('DB_PASSWORD', 'wpuser@');
define('DB_HOST', 'localhost');

Save and exit.

WordPress Configuration

Change the SELinux context for the WordPress directory.

root@ashokkuikel ~]# semanage fcontext -a -t httpd_sys_content_t “/var/www/ashok(/.*)?”
root@ashokkuikel ~]# restorecon -Rv /var/www/ashok

Now open a web browser and type in the domain name of your wordpress domain into the address bar, mine is “natsume.co”.

Choose English language and click on ‘Continue‘.

Wordpress Installation - Choose language

Fill in the site title and description with your info and click “Install WordPress”.

Wordpress Installation - Configure Admin and Site Title

Wait until the installation is finished. You will see the page below, click on “Log In” to log in to the WordPress admin dashboard:

Wordpress Install - Installation Success with HHVM Nginx on CentOS 7

Type in your admin user and password, then click “Log In” again.

Login to wordpress admin dashboard

Now you’re on the WordPress admin dashboard.

Wordpress Admin Dashboard

WordPress Home Page.

Wordpress Home Page Default

WordPress with Nginx and HHVM on CentOS 7 has been installed successfully.

Source Click here

Ashok kuikel

Ashok Kuikel is DevOps Engineer(Cloud Computing and Cyber Security), Entrepreneur working on Socio-Economic Development via Technology

He has been actively contributing as Joint Secretary of Federation of Computer Association of Nepal Kavre Chapter. Beside that, he is an official Global Peace Ambassador for Global Peace Chain, Nepal Chapter and Member of Internet Society, Nepal Chapter.

Above all, he enjoys learning about new trends and technologies and loves to share innovative ideas to contribute for the growth of the industry.

You can follow me on Social Media, GitHub, and via my Blog Channels.

One response to “How to Install WordPress with HHVM and Nginx on CentOS 7”

  1. ปั้มไลค์ says:

    Like!! Thank you for publishing this awesome article.

Leave a Reply

Articles and Tutorials

We love writing about WordPress and latest plugins tutorials, WooCommerce stats, and much more.