{"id":1689,"date":"2015-02-28T14:00:21","date_gmt":"2015-02-28T11:00:21","guid":{"rendered":"http:\/\/joseph.zikusooka.com\/?p=1689"},"modified":"2015-03-01T00:10:54","modified_gmt":"2015-02-28T21:10:54","slug":"how-to-configure-nginx-for-use-with-wordpress-and-other-cms-based-websites","status":"publish","type":"post","link":"https:\/\/joseph.zikusooka.com\/?p=1689","title":{"rendered":"How to configure nginx for use with WordPress and other CMS based websites"},"content":{"rendered":"<p>This is the second part of my Nginx tutorial. The <a title=\"Nginx configuration\" href=\"http:\/\/joseph.zikusooka.com\/?p=1681\" target=\"_blank\">first part<\/a> covered the installation process. In this post, I will show you how to modify the default configuration and get your server ready to serve CMS based websites like WordPress, Drupal, Joomla, Gallery etc<\/p>\n<p>Before delving into the configuration of nginx, it is important to ensure that all the prerequisite software is installed and properly configured. Like all of my tutorials, I am using Linux and specifically Fedora 20.<\/p>\n<p><strong>PHP<\/strong><\/p>\n<p>Install the following php and related modules:<\/p>\n<p><code>yum install php php-fpm php-pecl-apcu php-pdo php-mcrypt php-common php-mysqlnd php-process php-gd php-pear php-pear-DB php-pgsql php-xml php-cli php-xmlrpc php-mbstring php-pecl-igbinary php-pecl-memcache php-pecl-memcached php-pecl-mongo php-pecl-jsonc<\/code><\/p>\n<p>Edit the php configuration file:<\/p>\n<p><code>vim \/etc\/php.ini<\/code><\/p>\n<p><em>I recommend setting the following parameters in php.ini:<\/em><\/p>\n<p>short_open_tag = On<br \/>\ndate.timezone = Africa\/Kampala (or your time zone)<\/p>\n<p>Edit the php-fpm configuration file:<\/p>\n<p><code>vim \/etc\/php-fpm.d\/www.conf<\/code><\/p>\n<p><em>Change the following options in www.conf:<\/em><\/p>\n<p>[www]<br \/>\nlisten = 127.0.0.1:9000<br \/>\n;listen.allowed_clients = 127.0.0.1<br \/>\nuser = nginx<br \/>\ngroup = nginx<\/p>\n<p>Change permissions of the php sessions directory:<\/p>\n<p><code>chown nginx:nginx \/var\/lib\/php<\/code><\/p>\n<p>Now enable and start php-fpm service:<\/p>\n<p><code>systemctl enable php-fpm.service &amp;&amp; systemctl start php-fpm.service<\/code><\/p>\n<p><strong>OPENSSL<\/strong><\/p>\n<p>Install openssl if you plan on securing your server; which you should!<br \/>\n<code><br \/>\nyum install openssl openssl-libs openssl-devel<\/code><\/p>\n<p>Add ssl directory where certificate and key will be stored:<\/p>\n<p><code>mkdir \/etc\/nginx\/ssl<\/code><\/p>\n<p>Generate self-signed SSL certificate and key for your webserver:<\/p>\n<p><code>openssl req -new -x509 -out \/etc\/nginx\/ssl\/cert.pem -key \/etc\/nginx\/ssl\/cert.key -days 365<\/code><\/p>\n<p><strong>SPAWN-FCGI<\/strong><\/p>\n<p>Install spawn-fcgi, a simple program for spawning FastCGI processes:<\/p>\n<p><code>yum install spawn-fcgi<\/code><\/p>\n<p>Edit the environment file for spawn-fcgi:<\/p>\n<p><code>vim \/etc\/sysconfig\/spawn-fcgi<\/code><\/p>\n<p>Make the following changes:<\/p>\n<p><code>FCGI_SOCKET=\/var\/run\/fcgiwrap.socket<br \/>\nFCGI_PROGRAM=\/usr\/sbin\/fcgiwrap<br \/>\nFCGI_USER=nginx<br \/>\nFCGI_GROUP=nginx<br \/>\nFCGI_EXTRA_OPTIONS=\"-M 0700\"<br \/>\nOPTIONS=\"-u $FCGI_USER -g $FCGI_GROUP -s $FCGI_SOCKET -S $FCGI_EXTRA_OPTIONS -F 1 -P \/var\/run\/spawn-fcgi.pid -- $FCGI_PROGRAM\"<\/code><\/p>\n<p>Enable and start spawn-fcgi.service:<\/p>\n<p><code>systemctl enable spawn-fcgi.service &amp;&amp; systemctl start spawn-fcgi.service<\/code><\/p>\n<p><strong>FCGIWRAP<\/strong><\/p>\n<p>Install fcgiwrap, a simple FastCGI wrapper for CGI scripts:<\/p>\n<p><code>cd \/usr\/src<\/code><\/p>\n<p>Download fcgiwrap sources:<\/p>\n<p><code>git clone https:\/\/github.com\/gnosek\/fcgiwrap.git<br \/>\ncd \/usr\/src\/fcgiwrap<br \/>\nautoreconf -i<br \/>\n.\/configure --prefix=\/usr<br \/>\nmake &amp;&amp; make install<\/code><\/p>\n<p><strong>CONFIGURATION<\/strong><\/p>\n<p>Nginx&#8217;s configuration files are located under \/etc\/nginx. And as specified by the installation configure command in the previous post, the main configuration file is: \/etc\/nginx\/nginx.conf. This is where I will make most of my changes.<\/p>\n<p><code>vim nginx.conf<\/code><\/p>\n<p>Add the following to nginx.conf and save it:<\/p>\n<p><code>user nginx;<br \/>\nworker_processes 1;<br \/>\n#<br \/>\nevents {<br \/>\nworker_connections 1024;<br \/>\n}<br \/>\n#<br \/>\nhttp {<br \/>\ninclude mime.types;<br \/>\ninclude conf.d\/*.conf;<br \/>\ninclude sites-enabled\/*;<br \/>\ndefault_type application\/octet-stream;<br \/>\nlog_format main '$remote_addr - $remote_user [$time_local] \"$request\" '<br \/>\n'$status $body_bytes_sent \"$http_referer\" '<br \/>\n'\"$http_user_agent\" \"$http_x_forwarded_for\"';<br \/>\nsendfile on;<br \/>\nkeepalive_timeout 65;<br \/>\n#<br \/>\nserver {<br \/>\nlisten 80;<br \/>\nserver_name localhost;<br \/>\nroot html;<br \/>\nindex index.html index.htm index.php;<br \/>\nautoindex on;<br \/>\naccess_log \/var\/log\/nginx\/localhost.access.log main;<br \/>\n#<br \/>\nlocation \/ {<br \/>\n}<br \/>\n#<br \/>\nerror_page 500 502 503 504 \/50x.html;<br \/>\n#<br \/>\nlocation = \/50x.html {<br \/>\n}<br \/>\n#<br \/>\nlocation ~ \\.php$ {<br \/>\ninclude includes\/php_params;<br \/>\n}<br \/>\n#<br \/>\nlocation ~* \\.(cgi|chi)$ {<br \/>\ninclude includes\/cgi_params;<br \/>\n}<br \/>\n# Add xmlrpc scgi support<br \/>\n#<br \/>\nlocation ~ ^\/RPC2$ {<br \/>\nscgi_pass localhost:5000;<br \/>\ninclude scgi_params;<br \/>\n}<br \/>\n}<br \/>\n#<br \/>\n# HTTPS server<br \/>\n#<br \/>\nserver {<br \/>\nlisten 443 ssl;<br \/>\nserver_name localhost;<br \/>\nroot html;<br \/>\nindex index.html index.htm index.php;<br \/>\n#<br \/>\nssl_certificate ssl\/cert.pem;<br \/>\nssl_certificate_key ssl\/cert.key;<br \/>\nssl_session_cache shared:SSL:1m;<br \/>\nssl_session_timeout 5m;<br \/>\nssl_ciphers HIGH:!aNULL:!MD5;<br \/>\nssl_prefer_server_ciphers on;<br \/>\n#<br \/>\nlocation \/ {<br \/>\n}<br \/>\n#<br \/>\nlocation ~ \\.php$ {<br \/>\ninclude includes\/php_params;<br \/>\n}<br \/>\n#<br \/>\nlocation ~* \\.(cgi|chi)$ {<br \/>\ninclude includes\/cgi_params;<br \/>\n}<br \/>\n}<br \/>\n}<\/code><\/p>\n<p><em>To get details on what these directives exactly mean and the syntax used above, visit the <a title=\"nginx docs\" href=\"http:\/\/nginx.com\/resources\/admin-guide\/web-server\/\" target=\"_blank\">Nginx documentation<\/a><\/em><\/p>\n<p><em>NOTES:<\/em><\/p>\n<p><em>Nginx is pretty picky when it comes to Syntax, so:<\/em><\/p>\n<p><em>&#8211; The location directive is placed within a server directive.<br \/>\n&#8211; Also, all directives must end with a semicolon.<\/em><\/p>\n<p>Like other similar programs in UNIX, you can split the configuration file into several files using the parameter: include as shown in the above file. This makes maintenance and packaging of these files easier.<\/p>\n<p>Create other configuration directories:<\/p>\n<p><code>mkdir \/etc\/nginx\/{conf.d,includes,sites-enabled}<\/code><\/p>\n<p>Add the following files to the includes directory:<\/p>\n<p><code>vim \/etc\/nginx\/includes\/php_params<\/code><\/p>\n<p><code>gzip off;<br \/>\nfastcgi_pass unix:\/var\/run\/fcgiwrap.socket;<br \/>\nfastcgi_index index.cgi;<br \/>\n#<br \/>\nfastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;<br \/>\nfastcgi_param QUERY_STRING $query_string;<br \/>\nfastcgi_param REQUEST_METHOD $request_method;<br \/>\nfastcgi_param CONTENT_TYPE $content_type;<br \/>\nfastcgi_param CONTENT_LENGTH $content_length;<br \/>\n#<br \/>\nfastcgi_param SCRIPT_NAME $fastcgi_script_name;<br \/>\nfastcgi_param REQUEST_URI $request_uri;<br \/>\nfastcgi_param DOCUMENT_URI $document_uri;<br \/>\nfastcgi_param DOCUMENT_ROOT $document_root;<br \/>\nfastcgi_param SERVER_PROTOCOL $server_protocol;<br \/>\nfastcgi_param HTTPS $https if_not_empty;<br \/>\n#<br \/>\nfastcgi_param GATEWAY_INTERFACE CGI\/1.1;<br \/>\nfastcgi_param SERVER_SOFTWARE nginx\/$nginx_version;<br \/>\n#<br \/>\nfastcgi_param REMOTE_ADDR $remote_addr;<br \/>\nfastcgi_param REMOTE_PORT $remote_port;<br \/>\nfastcgi_param SERVER_ADDR $server_addr;<br \/>\nfastcgi_param SERVER_PORT $server_port;<br \/>\nfastcgi_param SERVER_NAME $server_name;<br \/>\n#<br \/>\n# PHP only, required if PHP was built with --enable-force-cgi-redirect<br \/>\nfastcgi_param REDIRECT_STATUS 200; <\/code><br \/>\n<code>vim \/etc\/nginx\/includes\/cgi_params<\/code><\/p>\n<p><code>fastcgi_pass 127.0.0.1:9000;<br \/>\nfastcgi_index index.php;<br \/>\n#<br \/>\nfastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;<br \/>\nfastcgi_param QUERY_STRING $query_string;<br \/>\nfastcgi_param REQUEST_METHOD $request_method;<br \/>\nfastcgi_param CONTENT_TYPE $content_type;<br \/>\nfastcgi_param CONTENT_LENGTH $content_length;<br \/>\n#<br \/>\nfastcgi_param SCRIPT_NAME $fastcgi_script_name;<br \/>\nfastcgi_param REQUEST_URI $request_uri;<br \/>\nfastcgi_param DOCUMENT_URI $document_uri;<br \/>\nfastcgi_param DOCUMENT_ROOT $document_root;<br \/>\nfastcgi_param SERVER_PROTOCOL $server_protocol;<br \/>\nfastcgi_param HTTPS $https if_not_empty;<br \/>\n#<br \/>\nfastcgi_param GATEWAY_INTERFACE CGI\/1.1;<br \/>\nfastcgi_param SERVER_SOFTWARE nginx\/$nginx_version;<br \/>\n#<br \/>\nfastcgi_param REMOTE_ADDR $remote_addr;<br \/>\nfastcgi_param REMOTE_PORT $remote_port;<br \/>\nfastcgi_param SERVER_ADDR $server_addr;<br \/>\nfastcgi_param SERVER_PORT $server_port;<br \/>\nfastcgi_param SERVER_NAME $server_name;<br \/>\n#<br \/>\n# PHP only, required if PHP was built with --enable-force-cgi-redirect<br \/>\nfastcgi_param REDIRECT_STATUS 200; <\/code><\/p>\n<p><strong>VIRTUAL HOSTS<\/strong><\/p>\n<p>Now let&#8217;s configure a virtual host. In this tutorial, I use example.com as the domain name.<\/p>\n<p><code>vim \/etc\/nginx\/sites-enabled\/example.com <\/code><\/p>\n<p><code> server {<br \/>\nlisten 80;<br \/>\n#<br \/>\nserver_name zikusooka.com www.zikusooka.com;<br \/>\nroot html\/vhost.example;<br \/>\nindex index.html index.htm index.php;<br \/>\n#<br \/>\nlocation \/ {<br \/>\n}<br \/>\n#<br \/>\nlocation ~ \\.php$ {<br \/>\ninclude includes\/php_params;<br \/>\n}<br \/>\n#<br \/>\nlocation ~* \\.(cgi|chi)$ {<br \/>\ninclude includes\/cgi_params;<br \/>\n}<br \/>\n}<\/code><\/p>\n<p><strong>WEB CONTENT<\/strong><\/p>\n<p>Add an index file to the main server&#8217;s root directory, for example:<br \/>\n<code><br \/>\nvim \/usr\/share\/nginx\/html\/index.html<\/code><\/p>\n<p>Add the following and save (This is just for testing, any HTML web page should work):<\/p>\n<p><code> &lt;HTML&gt;<br \/>\n&lt;B&gt;Hello World. Welcome to my Nginx web server!&lt;\/B&gt;<br \/>\n&lt;HTML&gt;<br \/>\n<\/code><\/p>\n<p>Create the document root directory for your virtual host:<\/p>\n<p><code>mkdir \/usr\/share\/nginx\/html\/example.com<\/code><\/p>\n<p>Add your document root in the above created directory. For Example create a file named index.php.<\/p>\n<p><code>vim \/usr\/share\/nginx\/html\/example.com\/index.php<\/code><\/p>\n<p>Add the following and save (This is just to test php, so any simple php page should work):<\/p>\n<p><code>&lt;?<br \/>\nphpinfo();<br \/>\n?&gt;<\/code><\/p>\n<p><strong>TESTING<\/strong><\/p>\n<p>Finally, restart nginx web server:<\/p>\n<p><code>systemctl start nginx<\/code><\/p>\n<p>If all goes well, you should now be able to browse html, php, and cgi pages on your server. You can then proceed to installing your WordPress or other content management system.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is the second part of my Nginx tutorial. The first part covered the installation process. In this post, I will show you how to modify the default configuration and get your server ready to serve CMS based websites like WordPress, Drupal, Joomla, Gallery etc Before delving into the configuration of nginx, it is important [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":3,"footnotes":""},"categories":[19],"tags":[],"class_list":["post-1689","post","type-post","status-publish","format-standard","hentry","category-tutorial"],"_links":{"self":[{"href":"https:\/\/joseph.zikusooka.com\/index.php?rest_route=\/wp\/v2\/posts\/1689","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/joseph.zikusooka.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/joseph.zikusooka.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/joseph.zikusooka.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/joseph.zikusooka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1689"}],"version-history":[{"count":5,"href":"https:\/\/joseph.zikusooka.com\/index.php?rest_route=\/wp\/v2\/posts\/1689\/revisions"}],"predecessor-version":[{"id":1694,"href":"https:\/\/joseph.zikusooka.com\/index.php?rest_route=\/wp\/v2\/posts\/1689\/revisions\/1694"}],"wp:attachment":[{"href":"https:\/\/joseph.zikusooka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/joseph.zikusooka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/joseph.zikusooka.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}