install and configure nginx and msyql for wordpress hosting in centos 7

Installing and configuring Nginx for wordpress in centos 7

This tutorials will be the how to tutorials on setting up   NGINX server for WordPress hosting.

Nginx also  pronounced by  “engine x” is a free open source light weight  web server as compared to other available web server . which focused on high performance, high concurrency and low memory usage with some other  additional features like load balancing, caching, access and bandwidth control, and  it has also  ability to integrate efficiently with a variety of applications which have helped to make nginx a good choice for modern website architectures.

 Installing mysql server

#yum install mariadb mariadb-server 
# systemctl restart mariadb
# systemctl status mariadb

Now let us setup mysql root password by issuing the mysql_secure_installation command

#mysql_secure_installation

Now let us bind our mysql server to our localhost server

#vi /etc/my.cnf
bind-address=127.0.0.1

Binding to localhost (127.0.0.1) will enable mysql server to listen only to localhost which is good for security practice

Now in second step we will install our favorite light-weight NGINX server

Installing NGINX in Centos 7

yum install nginx -y

now let us find the number of processor in our Server to allocate the worker-process to our nginx server

grep -c processor /proc/cpuinfo
2

The output is two, so we will setup nginx-processes to 2 in our nginx.conf configuration file

# vim /etc/nginx/nginx.conf
worker_processes 2;

default has value equal to auto,

Now stop apache if it is running as we can’t setup two web server at a same time as they listen to port 80 by default

#systemctl stop httpd 
#systemctl disable httpd

now let us test start and enable our nginx server in our centos 7 system

#nginx -t
#systemctl restart nginx
#systemctl enable nginx

so, the installation of nginx is complete, now let us install php-FPM, as we will use php as fastCGI using php-FPM which is also called fast CGI process manager

#yum install php-fpm php-mysql php-gd php-xml

Now let us tweak our php.ini file and set the following parameters

# vim /etc/php.ini
date.timezone = Asia/Kathmandu
memory_limit = 64M
expose_php=off

expose_php off will disable to php header information being displayed, pretty nifty security feature isn’t it ?

now change the fpm pool ownership,as this is owned by apache, as we are running nginx this should be changed

#vim /etc/php-fpm.d/www.conf

and set the following values

user=nginx
group=nginx

Now let us set the ownership for php-fpm log directory

# chown nginx:root -R /var/log/php-fpm

Now let us start php server

#systemctl restart php-fpm
#systemctl enable php-fpm

Ok folks, it is time to setup our Virtual host, also you can say it as virtual domain for different websites using nginx. Virtual host is necessary to setup up the multiple website using a single public IP within a single server or it may be on distributed server in case of proxying. For this we will make a directory called /srv/www/ and place all the virtualhost (websites) webfiles into this /srv/www directory with their domain name. For instance we have a website called openpy.com and want to allocate space for it we can simply create a directory for this website by issuing this command as

 /srv/www/openpy.com/{public_html, logs}

After creating required directory for virtual host we will create a server block in /etc/nginx/conf.d/example.com.conf which will handle the openpy.com and other virtualdomain

#vim /etc/nginx/conf.d/openpy.com.conf

and add the following lines to openpy.com.conf

server {
server_name openpy.com;
listen 80;
root /srv/www/openpy.com/public_html;
access_log /srv/www/openpy.com/logs/access.log;
error_log /srv/www/openpy.com/logs/error.log;
index index.php;

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

location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}
location ~ /\.ht {
deny all;
}
location ~ \.php {
try_files $uri = 404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}

sometimes selinux won’t give permission, let us disable selinux in our server for temporarily and hope all things go well.

#setenforce 0

warning: Disabling Selinux is not a good practice

To fix the selinux permission permanently, issue the following commands against /srv/www/ directory

setenforce 1
#semanage fcontext -a -t httpd_sys_content_t '/srv/www(.*)?';
#restorecon -vvRF /srv/www/

On changing the security context for /srv/www if you don’t find semanage command you can install it by issuing the following command


#yum  install policycoreutils-python

Setting up MariaDB Database to install wordpress using nginx in centos 7

As we have already installed our database server, so let us create a database for our wordpress website

 mysql -u root -p

Enter your root password,
Now let us create a database for our wordpress website

#create database wordpress;
#create user 'wordpress'@'localhost' identified by 'wordpress';
#grant all privileges on wordpress.* to 'wordpress'@'localhost';

Now Download wordpress and extract it and run the installer

# cd /srv/www/openpy.com/public_html
 #wget https://wordpress.org/latest.tar.gz
# tar -xvzf latest.tar.gz
#cd wordpress
# cp -arvp * ../
 cd .. && rm -rf wordpress latest.tar.gz

so to run the installer just open your firefox or any other browser and point your domain to it follow the on screen instruction provided by your wordpress installer

 http://openpy.com

That is all about installing configuring nginx and mysql(mariadb) in centos 7 to host webpress blog

Leave a Reply

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