Permalink
Browse files

bring nginx/wordpress article up-to-date

  • Loading branch information...
1 parent b358982 commit a141f2c0343371a43e273f7ebcbdec417bc42789 @elasticdog committed Oct 30, 2010
@@ -2,67 +2,68 @@
layout: post
title: 'HOWTO: Install WordPress on Nginx'
---
-I recently moved all of my sites from a cheap shared host to a shiny new VPS(Virtual Private Server) at "SliceHost":http://www.slicehost.com/, and couldn't be happier. Running your own VPS means a great amount of flexibility since you have full root access to the server and get to configure everything exactly how you want it. That said, it also means that if you want to maintain high performance, you have to keep your resource usage to a minimum.
+I recently moved all of my sites from a cheap shared host to a shiny new VPS(Virtual Private Server) at "Linode":http://www.linode.com/, and couldn't be happier. Running your own VPS means a great amount of flexibility since you have full root access to the server and get to configure everything exactly how you want it. That said, it also means that if you want to maintain high performance, you have to keep your resource usage to a minimum.
"Apache":http://httpd.apache.org/ is a very well-establish web server that can handle just about any situation. Unfortunately, that flexibility comes at the cost of size and relatively high demands on server resources. "Nginx":http://nginx.net/ ("engine x") is a lightweight web server/reverse proxy that is very efficient and perfect for hosting WordPress. Read on to see how that can be done...
h2(#step-one). Step One: FastCGI
-First off, Nginx does not provide FastCGI for you (FastCGI is what your web server uses to interact with WordPress's PHP(PHP: Hypertext Preprocessor) code), so you've got to have a way to spawn your own FastCGI processes. My preferred method is to use the @spawn-fcgi@ program provided by the web server "lighttpd":http://www.lighttpd.net/. You _can_ use PHP's built-in FastCGI manager @php-cgi@ to "do the same thing":http://blog.kovyrin.net/2006/05/30/nginx-php-fastcgi-howto/, but it's not as straight-forward. Plus, if you learn how to use @spawn-fcgi@, you can easily adapt it for use with other web applications requiring FastCGI.
+First off, Nginx does not provide FastCGI for you (FastCGI is what your web server uses to interact with WordPress's PHP(PHP: Hypertext Preprocessor) code), so you've got to have a way to spawn your own FastCGI processes. My preferred method is to use the @spawn-fcgi@ program provided by the same people that develop the web server "lighttpd":http://www.lighttpd.net/. You _can_ use PHP's built-in FastCGI manager @php-cgi@ to "do the same thing":http://blog.kovyrin.net/2006/05/30/nginx-php-fastcgi-howto/, but it's not as straight-forward. Plus, if you learn how to use @spawn-fcgi@, you can easily adapt it for use with other web applications that require FastCGI.
h3. Install spawn-fcgi
-To download and install @spawn-fcgi@, run the following commands. Don't worry, all of the building happens in your current directory...nothing else will be installed on your machine.
+To download and install @spawn-fcgi@, either use your native package manager (this would be the recommended approach), or run the following commands to install the package from source. All of the building happens in your current directory and the binary will be installed under @/usr/local/bin/@ with a man page under @/usr/local/share/man/@.
+{% assign pkg = 'spawn-fcgi-1.6.3' %}
{% highlight bash-session %}
-$ wget http://www.lighttpd.net/download/lighttpd-1.4.18.tar.bz2
-$ tar -xvjf lighttpd-1.4.18.tar.bz2
-$ cd lighttpd-1.4.18/
+$ wget http://www.lighttpd.net/download/{{ pkg }}.tar.bz2
+$ tar xvjf {{ pkg }}.tar.bz2
+$ cd {{ pkg }}/
$ ./configure
$ make
-$ sudo cp src/spawn-fcgi /usr/bin/spawn-fcgi
+$ sudo make install
{% endhighlight %}
-__==NOTE==: If you're following the steps above verbatim, you will need to have root privileges in order to copy the binary to its final location...everything else should work fine as a normal user. To gain root privileges, the program @sudo@ was used in the example above; you may or may not have access to @sudo@ on your machine.__
+__==NOTE==: If you're following the steps above verbatim, you will need to have root privileges in order to perform the final instalation step...everything else should work fine as a normal user. To gain root privileges, the program @sudo@ was used in the example above; you may or may not have access to @sudo@ on your machine.__
-After @spawn-fcgi@ has been copied to the desired location, you can safely remove the build directory and original source file:
+After @spawn-fcgi@ has been installed, you can safely remove the build directory and original source file:
{% highlight bash-session %}
$ cd ..
-$ rm -rf lighttpd-1.4.18/
-$ rm lighttpd-1.4.18.tar.bz2
+$ rm -rf {{ pkg }}/
+$ rm {{ pkg }}.tar.bz2
{% endhighlight %}
h3. Run spawn-fcgi
This part will be fairly distribution-specific, but I'll provide the basic command that you'll need. What you want to do is find a way to run this command as part of your init scripts so the processes will be spawned automatically when you reboot your server.
-bc. /usr/bin/spawn-fcgi -f /usr/bin/php-cgi -a 127.0.0.1 -p 53217 -P /var/run/fastcgi-php.pid
+bc. /usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 53217 -P /var/run/fastcgi-php.pid -- /usr/bin/php-cgi
-* @-f@ → the filename of the fcgi-application; in our case we want "php-cgi", which is provided by your distribution's PHP package. If you don't know where to find it, try running @which php-cgi@ on the command line.
-* @-a@ → the IP address to bind the processes to; in our case we want the localhost
-* @-p@ → the port number to bind the processes to; pick whatever you want that won't cause a conflict (technically it would be best to pick a random number between 49152 and 65535), just make sure to remember the number and use that same port for your Nginx configuration file later on
-* @-P@ → the location where to save the process id file; you can use this file to easily kill the processes later
+* @-a <address>@ &rarr; the IP address to bind the processes to; in our case we want the localhost
+* @-p <port>@ &rarr; the port number to bind the processes to; pick whatever you want that won't cause a conflict (technically it would be best to pick a random number between 49152 and 65535), just make sure to remember the number and use that same port for your Nginx configuration file later on
+* @-P <path>@ &rarr; the location where to save the process id file; you can use this file to easily kill the processes later
+* @-- <fcgiapp>@ &rarr; the absolute path of the fcgi-application to spawn; in our case we want "php-cgi", which is provided by your distribution's PHP package. If you don't know where to find it, try running @which php-cgi@ on the command line.
-For better security, you can also spawn the processes as a non-privileged user by specifying the user/group with the @-u@ and @-g@ flags respectively. For more information on all the available options, run @spawn-fcgi -h@ on the command line.
+For better security, you can also spawn the processes as a non-privileged user by specifying the user/group with the @-u@ and @-g@ flags respectively. For more information on all the available options, run @spawn-fcgi -h@ or @man spawn-fcgi@ on the command line.
-If you're interested in seeing the complete init script that I wrote for use with "Arch Linux":http://www.archlinux.org/, you can download it here: "fastcgi-php":http://elasticdog.com/code/2008/02/fastcgi-php
+If you're interested in seeing a complete example init script that I wrote for use with "Arch Linux":http://www.archlinux.org/, you can download it here: "fastcgi-php":http://elasticdog.com/code/2008/02/fastcgi-php
h2(#step-two). Step Two: Complete 83.33% of the Famous 5-Minute Install
-Next you should download the WordPress files and extract them to their final location on your server. Simply follow steps 1-5 of the "Famous 5-Minute Install":http://codex.wordpress.org/Installing_WordPress#Famous_5-Minute_Install (the 6th and final step requires that your web server be up and running properly, so we'll do it later). This guide will assume that you extracted the WordPress core files here: @/srv/www/nginx/domain.com/@
+Next you should download the WordPress files and extract them to their final location on your server. Simply follow steps 1-5 of the "Famous 5-Minute Install":http://codex.wordpress.org/Installing_WordPress#Famous_5-Minute_Install (the 6th and final step requires that your web server be up and running properly, so we'll do it later). This guide will assume that you extracted the WordPress core files here: @/srv/http/nginx/example.com/@
h2(#step-three). Step Three: Nginx Configuration
-To get the web server up and running properly, the file you need to edit is called "nginx.conf" and is installed in different places depending on your Linux distribution. If you install Nginx from source, the default location is @/usr/local/nginx/conf/nginx.conf@, however yours may be somewhere else.
+To get the web server up and running properly, the file you need to edit is called "nginx.conf" and is installed in different places depending on your Linux distribution. If you install Nginx from source, the default location is @/usr/local/nginx/conf/nginx.conf@, however yours may be somewhere else like @/etc/nginx/conf/nginx.conf@.
Once you find that file, open it with your favorite text editor and add a server declaration that looks something like this (I'll cover what each part means after posting the code):
{% highlight nginx %}
server {
- listen 12.34.56.78:80; # your server's public IP address
- server_name domain.com; # your domain name
- root /srv/http/nginx/domain.com; # absolute path to your WordPress installation
+ listen 192.0.32.10:80; # your server's public IP address
+ server_name example.com; # your domain name
+ root /srv/http/nginx/example.com/; # absolute path to your WordPress installation
try_files $uri $uri/ /index.php;
@@ -74,11 +75,11 @@ server {
}
{% endhighlight %}
-div(sourcelink). Download Source Code: "nginx.conf.txt":http://elasticdog.com/code/2008/02/nginx-conf.txt
+div(sourcelink). Download the Source Code: "nginx.conf.txt":/code/2008/02/nginx-conf.txt
-You will need to edit the configuration above using your own information. The first part is merely the server declaration where you define what your server's publicly available IP address is and what domain name that address is associated with.
+You will need to edit the configuration above using your own information. The first part is merely the server declaration where you define what your server's publicly available IP address is, and what domain name that address is associated with. Next we add some default settings which point Nginx to the absolute path of the WordPress installation on your server.
-Next we add some default settings which point Nginx to the absolute path of the WordPress installation on your server. After that, the @try_files@ declaration automatically checks for the existence of files in order, and returns the first file that is found. The key part here is that WordPress uses the "Front Controller" design pattern, meaning that any request for a file that does not exist on the server should be handled by the main index.php file. So that's exactly what we do...if the requested URI(Uniform Resrouce Identifier) exists on our local filesystem (images, css stylesheets, etc.), then we'll serve it up directly...if the requested URI does not exist on the filesystem, we forward that request on to index.php.
+After that, the @try_files@ directive automatically checks for the existence of files in order, and returns the first file that is found. The key part here is that WordPress uses the "Front Controller" design pattern, meaning that any request for a file that does not exist on the server should be handled by the main index.php file. So that's exactly what we do...if the requested URI(Uniform Resrouce Identifier) exists on our local filesystem (images, stylesheets, etc.), then we'll serve it up directly; if the requested URI does not exist on the filesystem, we forward that request on to index.php.
Last, we add a location block that tells Nginx to dynamically forward PHP requests to the FastCGI processes we "spawned earlier":#step-one. You just have to make sure that the port number matches what we did above. That's it!
View
@@ -266,3 +266,8 @@ ul.posts {
padding: 16px 0 16px 80px;
}
+.sourcelink {
+ font-size: 14px;
+ margin-top: 0;
+ text-align: right;
+}
@@ -3,33 +3,68 @@
. /etc/rc.conf
. /etc/rc.d/functions
+DAEMON=fastcgi-php
+
+SPAWNER='/usr/local/bin/spawn-fcgi'
+SPAWNER_ARGS='-a 127.0.0.1 -p 53217 -u http -g http -- /usr/bin/php-cgi'
+
+get_pid() {
+ pidof -o %PPID $DAEMON
+}
+
case "$1" in
start)
- stat_busy "Starting FastCGI for PHP"
- /usr/bin/spawn-fcgi -f /usr/bin/php-cgi -u nobody -g nobody -a 127.0.0.1 -p 53217 -P /var/run/habari-ed.pid &>/dev/null
- if [ $? -gt 0 ]; then
- stat_fail
+ stat_busy "Starting $DAEMON daemon"
+
+ PID=$(get_pid)
+ if [ -z "$PID" ]; then
+ [ -f /var/run/$DAEMON.pid ] && rm -f /var/run/$DAEMON.pid
+ # RUN
+ $SPAWNER $SPAWNER_ARGS &> /dev/null
+ #
+ if [ $? -gt 0 ]; then
+ stat_fail
+ exit 1
+ else
+ echo $(get_pid) > /var/run/$DAEMON.pid
+ add_daemon $DAEMON
+ stat_done
+ fi
else
- add_daemon fastcgi-php
- stat_done
+ stat_fail
+ exit 1
fi
;;
+
stop)
- stat_busy "Stopping FastCGI for PHP"
- kill $(cat /var/run/fastcgi-php.pid) &>/dev/null
- if [ $? -ne 0 ]; then
+ stat_busy "Stopping $DAEMON daemon"
+ PID=$(get_pid)
+ # KILL
+ [ ! -z "$PID" ] && kill $PID &> /dev/null
+ #
+ if [ $? -gt 0 ]; then
stat_fail
+ exit 1
else
- rm_daemon fastcgi-php
+ rm -f /var/run/$DAEMON.pid &> /dev/null
+ rm_daemon $DAEMON
stat_done
fi
;;
+
restart)
$0 stop
- sleep 1
+ sleep 3
$0 start
;;
- *)
- echo "usage: $0 {start|stop|restart}"
+
+ status)
+ stat_busy "Checking $DAEMON status";
+ ck_status $DAEMON
;;
+
+ *)
+ echo "usage: $0 {start|stop|restart|status}"
esac
+
+exit 0
@@ -1,7 +1,7 @@
server {
- listen 12.34.56.78:80; # your server's public IP address
- server_name domain.com; # your domain name
- root /srv/http/nginx/domain.com; # absolute path to your WordPress installation
+ listen 192.0.32.10:80; # your server's public IP address
+ server_name example.com; # your domain name
+ root /srv/http/nginx/example.com/; # absolute path to your WordPress installation
try_files $uri $uri/ /index.php;

0 comments on commit a141f2c

Please sign in to comment.