This is Part Two of this series which shows you how to setup WordPress in CentOS using Nginx web server. Part One of this series which can be found here started out by showing you how to install Nginx, MariaDB and PHP scripts.
If you followed the steps in Part One, then you should have these servers and scripts installed and waiting to be configured. The configurations of these servers and scripts will be detailed in this blog post.
If you haven’t seen part one of this series, please go and read it before continuing below. Without reading and following the first post you won’t be able to go any further.
Configuring Nginx Server
This first server we’ll want to configure is Nginx. It’s the web server and has the most work.
When you install Nginx (Engine-X) in CentOS, its configuration files and folders get installed in the /etc/nginx directory. In this directory, you’ll find Nginx main configuration file (nginx.conf) and the default site file in /etc/nginx/conf.d directory (default.conf).
The main config file for Nginx (nginx.conf) only has the bare minimum of configurations. This stripped down config file will work with just fine for most blogs or websites.
You can add and remove stuff as you like but do it with care. For most of our setup, here’s what we have in our nginx.conf file.
user nginx;worker_processes 2;error_log /var/log/nginx/error.log;pid /var/run/nginx.pid;events {worker_connections 2048;use epoll;multi_accept on;}http {include /etc/nginx/mime.types;default_type application/octet-stream;log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log /var/log/nginx/access.log main;## Cacheopen_file_cache max=1000 inactive=24h;open_file_cache_valid 24h;open_file_cache_min_uses 2;open_file_cache_errors on;## Timeoutsclient_header_timeout 3m;client_body_timeout 3m;send_timeout 3m;keepalive_timeout 120 120;## Size limitsclient_max_body_size 15m;client_header_buffer_size 32k;client_body_buffer_size 128k;large_client_header_buffers 64 8k;## Generalignore_invalid_headers on;keepalive_requests 100;limit_conn_zone $binary_remote_addr zone=addr:10m;recursive_error_pages on;reset_timedout_connection on;sendfile on;tcp_nopush on;tcp_nodelay on;server_tokens off;## Compressiongzip on;gzip_static on;gzip_comp_level 6;gzip_disable "msie6";gzip_buffers 32 8k;gzip_vary on;gzip_proxied any;gzip_min_length 0;gzip_http_version 1.0;gzip_types text/css text/javascript text/xml text/plain application/javascript application/x-javascript application/json application/xml application/rss+xml;output_buffers 10 128k;postpone_output 1500;fastcgi_cache_path /var/cache/nginx/fastcgi_temp levels=1:2 keys_zone=CZONE:15m inactive=60m;fastcgi_cache_key "$scheme$request_method$host$request_uri";fastcgi_cache_use_stale error timeout invalid_header http_500;include /etc/nginx/conf.d/*.conf;}
Next, edit the default site file at /etc/nginx/conf.d/default.conf and configure the WordPress site.
Again, you can add and remove stuff as you wish, however, do it with care. The main area to focus on is the root directory. This where you’ll put WordPress content.
Here’s how we configured our site’s file.
server {listen 80;server_name yoursite.com;root /var/www/wordpress;index index.php;try_files $uri $uri/ /index.php?$args;## faviconlocation = /favicon.ico {log_not_found off;access_log off;}## robotslocation = /robots.txt {allow all;log_not_found off;access_log off;}## Cachinglocation ~* \.(js|css|rss|png|jpg|jpeg|gif|ico|woff)$ {expires max;log_not_found off;access_log off;add_header Pragma public;add_header Cache-Control "public, must-revalidate, proxy-revalidate";}## No caching for logged in usersset $no_cache 0;if ($request_method = POST) {set $no_cache 1;}if ($query_string != "") {set $no_cache 1;}if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {set $no_cache 1;}if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {set $no_cache 1;}## PHP settingslocation ~ \.php$ {fastcgi_split_path_info ^(.+\.php)(/.+)$;fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;fastcgi_intercept_errors on;fastcgi_ignore_client_abort off;fastcgi_connect_timeout 60;fastcgi_send_timeout 90;fastcgi_read_timeout 90;fastcgi_buffer_size 128k;fastcgi_buffers 4 256k;fastcgi_busy_buffers_size 256k;fastcgi_temp_file_write_size 256k;fastcgi_cache_bypass $no_cache;fastcgi_no_cache $no_cache;fastcgi_cache CZONE;fastcgi_cache_valid 200 302 1h;fastcgi_cache_valid 301 1h;fastcgi_cache_valid any 1h;fastcgi_cache_min_uses 2;}## Securitylocation ~* /\.ht {deny all;access_log off;log_not_found off;}}
Save the default site config and you’re done with Nginx. The highlighted line in the file is the root directory. Put all WordPress scripts in that folder.
Configuring PHP-FPM
After setting up Nginx, you should turn your focus on PHP-FPM. This is a caching module that works with Nginx. Since we defined it in our Nginx’s setup, we should configure it as well.
PHP-FPM file is stored in the /etc/php-fpm.d/www.conf.
Open the file and make sure the lines below match your settings.
;listen = 127.0.0.1:9000listen = /var/run/php-fpm/php-fpm.sock
Save the file and you’re done. There are most setting to tweak, but for now, use change the line above and save the file.
Creating WordPress Database And User
After installing and setting up Nginx and PHP-FPM, you should turn your focus on creating WordPress Database and User. The database is used to store WordPress content and the user is used to connect to the database.
To access MariaDB, run the commands below.
mysql -u root –p
Once connected, run the commands below to create a WordPress database.
CREATE DATABASE wordpress;
Next, create WordPress database user.
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'password';
Finally, grant WordPress user rights to administer the database.
GRANT ALL ON wordpress.* TO 'wpuser'@'localhost';
That’s it! Quit and you’re don’t with the database side.
Downloading WordPress Content
Finally, download WordPress content and extract it to the root folder you specified in Nginx config settings. After that, rename wp-config-sample.php to wp-config.php.
Then edit wp-config.php and specify the database settings, including the name, user and password to connect to the database.
After saving it, connect to your site and it will prompt to you install WordPress. Do it.
That’s all.
Enjoy!