Skip to content

8 search results for "drupal"

Drupal 6 JavaScript and jQuery

Anyone who has been paying attention knows that I have been doing a ton of reading lately on Drupal 6. I admit it, I have a tendency to get interested in something and then consume every bit of information about it that I can find. I recently read a book on pure JavaScript because I noticed that Drupal version 6 ships with a Drupal JavaScript library (drupal.js) as well as a fairly recent and powerful library that is gaining followers rapidly called jQuery.

I have been waiting for this book to come out for months, since I first heard that it was being written. Matt Butcher and Packt Publishing released Drupal 6 JavaScript and jQuery last month. Now that I have had a chance to read it, I must say that it does not fail to please.

What I wanted to learn was why Drupal chose to include JavaScript in the Drupal core, what it is used to accomplish and how. I am very familiar with HTML and CSS, and am reasonably adept at reading and modifying PHP when necessary, so all the other main parts of Drupal are built using tools I know and understand. I wanted to find out how and why I would want to take advantage of the stated and implied benefits of including the free and open source jQuery library in sites I administer and design using Drupal.

The books starts off with a very quick introduction to Drupal. It is obvious that the intended audience is not newcomers, and that this is not a basic introduction level text. It is assumed that anyone reading this is familiar with web site creation and administration, web hosting basics, and knows how to install a Drupal site that uses the default theme. You get a quick tour of the main core features of Drupal and some nice web browser tools for developers. Then we get to the meat of the book.

Starting in chapter two we learn how to work with JavaScript in Drupal by writing a short js program and discovering how to include it in a node by incorporating a link to the script in a theme. This is a clear and great beginning.

Chapter three is dedicated completely to the jQuery library, what it is capable of doing, and how to use it the Drupal way instead of reinventing the wheel. The basic point here is that it is a lot easier to use a library to perform complex tasks than it is to write a complete JavaScript function, which would then need to be adapted for different browsers, and it is easy to call those library functions within Drupal because the Drupal developers have chosen to create ways to do so that are even easier than calling those library functions would be in a standard flat HTML/CSS page or some other content management systems. There are useful examples throughout such as how to make a set of announcements display one at a time at the top of your page, fading in and out, so that you can give several tidbits of information to your site guests without requiring all of the space on your page. Cool stuff.

The book contains useful information on how to use the drupal.js library, written by the Drupal developer community, to do many tasks, such as the sample collapsing blocks project that lets guests choose whether to allow information to be displayed or to shrink boxes so that there is more space on a page. We learn how to use js to create beautiful effects and help with site theming, how it can be used to translate your site into other language for guests, and even how to build your own modules.

I have only scratched the surface of the book. As I admitted in my post on the JavaScript book, I am still learning the language. By intent, jQuery is a huge time saver and makes incorporating client side functions into your site easier than using the language alone, and the way the Drupal community has chosen to incorporate that library and write their own in addition makes it still easier. I am glad I picked up the book. Now, if you will excuse me, I need to go play.

Using Drupal

I have read a lot of Drupal books as well as online materials. This particular book is one I was looking forward to for a long time. It was just released in December 2008 and is from O’Reilly Press. I have never made it a secret that I find O’Reilly’s books to be consistently good, and usually the best in any category in which they have offerings available. This book lived up to my expectations.

Using Drupal is easily the best book I have read so far for helping a person who knows nothing about the content management system (but who has a base familiarity with web hosting) figure out how the software works, how to add and configure features, and how to make powerful and useful web sites.

The book begins with a very clear, yet detailed introduction to Drupal; what it is, how it works, and the history behind how the project was conceived and the evolution of web site development over the years. It then continues with a long chapter that gives a crash course in each of the unique aspects of the Drupal system: content management, modules, access control, roles and permissions, taxonomy, theming and more are introduced with clear and useful examples.

After this is where the book steps out of the crowd and into a league of its own. Starting with chapter three we are served a series of well written chapters, each describing how to use a specific module or set of modules to do something wonderful with Drupal that is useful, interesting, and powerful. We learn how to use the Content Construction Kit (CCK) module with the Views module to build useful forms for displaying data in a way that is appealing and easily understood by site visitors. We are shown how to create multilingual sites, sites with product reviews, an internal wiki for group collaboration, an online store, and much more. All have clear descriptions of the process, the options available, and great examples.

One of my favorite chapters is number six, which discusses managing workflow. Perhaps you have a busy site with lots of contributors and a few people who are ultimately responsible for posting the created content after it is reviewed. This chapter describes how to make this process much more efficient than I would have done it originally by using actions and triggers with the Workflow module to have newly created content automatically enter an approval queue while notifying the editor responsible about its existence and still allowing the original user access to modify the content while it waits for approval. This allows you to see content in various states, perhaps approve or simply make notes on it, and not waste your reviewer’s time forcing them to check whether content is waiting for review or not by giving them automatic notifications (which may be configured many ways). That is a great idea that I would not have thought of or figured out by myself, and it is just one of the well thought out and elegant designs this book presents.

I still have one more Drupal book en route, and another that I want to buy. If I think they are worth sharing, I’ll post a review of each of them in the coming weeks.

Drupal Multimedia

Anyone following this blog for a while will notice that I have been reading a lot of Drupal books recently. I have a big project that I am working on for someone, and I want to do a good job for them. While I run Drupal on matthewhelmke.com, it is a very simple implementation solely to list my recently published work. I also ran Drupal on a site for my business, but that has since closed.

Anyway, this new site has and will incorporate some features that are new to me. Some are being carried over from an older version of the website that someone else created, and others are completely new to the site. Several involve media, both audio and video.

In the past, I have embedded photo galleries into Drupal installations, especially using Gallery2, and I have had fun figuring out how to embed non-photo media formats into gallery software like Coppermine. That was back in the days when Drupal 4 was currently supported, so after some searching, I decided it would be more fun to figure out everything using Drupal 6 and contributed modules, without making major modifications to massage in other software.

I started reading the main Drupal website, scouring the list of current modules for version 6. There is a lot of great stuff there, almost too much to sift through. I saw that the publisher of the Drupal 6 book and Drupal 6 Themes book that I had read also had one on multimedia, so I picked it up. I’m still looking at other Drupal books, too, including one that isn’t yet published but is being written by a fellow Ubuntu member, Emma Hogbin. If any seem worth mentioning once I read them, I’ll certainly give them a shout out later.

Okay, on to the book I’ve just finished reading, Drupal Multimedia.

Like the other two Drupal books I have read by this publisher, this book is relatively short, coming in at about 240 pages. It is also focused on only one part of using Drupal, which means that you don’t have to sift through a lot of information you are not going to use. This is definitely not a basic, “here’s how to install and get started easily” sort of text, but more of a precise guide to using Drupal to serve various forms of media files in a way that can be styled easily to fit in with your overall site theme, and that does not require the use of non-Drupal software or modules. In other words, my initial thought was that this book would be exactly what I needed for this phase of my current project.

The book assumes the reader is capable of installing Drupal on a server and assumes you have a base installation up and running perfectly before beginning. It then starts with a quick introduction to the building blocks of Drupal: nodes, regions and blocks, themes, and modules. It discusses how to use CCK to create custom content types and fields, views to set up how they will be displayed, and breezes through the important parts of theming these additions to match the overall look and feel of your site. That is all in chapter one.

The next several chapters are filled with easy to use plans for installing and configuring various modules to deal with images, galleries, thumbnails, slideshows, audio and video. The author quite clearly describes how to get each of these media to fit in and work with your site, exactly how you would like it to work.

I am still in the planning stage of this particular site’s upgrade. This was a good time to read through this book, because it gives me a chance to quickly and easily figure out what I can do for this project, without needing extra time for research or requiring big modifications, and enables me to discuss it intelligently and confidently with those making the decisions. If you have a similar project, the book is worth a look.

Now, if anyone knows how to run iTunes on Linux so that I can begin working on the details of making a podcast feed (from the Drupal Audio module) available in iTunes, I would be grateful. This is to fulfill a special request from the site owner, and I’ve figured out the feed from the Audio module, but without a way to run iTunes, I can’t test it without borrowing someone else’s computer, and that’s not a good thing. I will, but I really don’t want to do a virtual OS installation just for this (and pay for the licensing of a proprietary OS I won’t use for anything else).

Drupal 6 Site Builder Solutions

Those of you who read this blog regularly know that I like Drupal. I use WordPress on this site and think it is fabulous, but for sites that want to do more to connect with readers or customers, I think Drupal is one of the easiest and most powerful solutions out there for creating a quality site with as little difficulty and pain as possible. I personally administer three sites built on Drupal and have built others.

I am in the process of making a new site for someone with a nice looking, but aging site that was built on Drupal 4.7, which is no longer supported. I am looking through the site’s functionality and theme and have upgraded both to use Drupal 5, but only to be sure that it has current security updates while I make plans with the owner for a complete redesign.

While researching ideas and functionality to present for use in the new site, I ran across a brand new book from Packt Press called Drupal 6 Site Builder Solutions that I found enjoyable, and very useful. Now, I already know what I am doing with Drupal. I know how to read the project’s documentation to find and configure modules and themes, and I know how to get help if I need it. Even so, this book gave me some great ideas that I am going to share with the owner of the site I am working on and that I hope to use.

The entire book centers around creating a website for a fictional company, The Good Eatin’ Bistro. You start out with a discussion of the basic needs for the company, then begin with the basics of creating a site suitable to the customer’s needs. As the book progresses, you learn how to use some well-known and some other less-known modules to add interesting and useful functionality to the site that makes information easy to find and interesting to view.

Do you want a site that allows all members of a project you are involved in to have individual blogs, with a calendar to display events, and a map? No problem. Have a restaruant and want to put your menu online in a way that looks good, and is easily changed. Easy. Are you interested in creating a newsletter that people viewing your site can sign up to recieve occasionally? You can. All this and a ton more are in the book.

I think one of the most useful parts of the book for most people will be the last chapter, which talks about how to find out how to add functionality not discussed in the book, where to get help, and has some great tips on things that not everyone thinks about like hosting, hardware, and working with developers and artists.

One thing that crossed my mind is sharing the book with the owner of the site I mentioned. He won’t understand the technical details, but this book is written in a way that I believe will make what I am doing very clear and make it much easier for him to talk with me about his hopes, plans and desires. For that reason alone, the book is valuable to me, and it might be to others as well who are not actually interested in creating a site themselves, but who need to know enough detail to be able to discuss site building intelligently with a developer, designer or team of people working to create a site for them.

The book does not cover theming, but focuses solely on enabling features, enabling and configuring new functionality to a site built on Drupal 6. If you are interested in theming Drupal 6, I recommend a book I reviewed last month on the topic.

Drupal 6 Themes

I have used Drupal to administer sites for years. It is flexible, powerful, and relatively easy to use. The one area of Drupal where I have been weak is theming. Generally, I have used contributed themes, maybe modifying colors, logos, and other simple things.

The one or two times I felt ambitious and tried to read through the documentation in order to learn how to create my own theme from scratch, I either got distracted by life, or found myself getting tired of the search and wishing I had all of the information I needed in one place to learn how to create a theme.

This week, I have been reading a book on precisely this topic called Drupal 6 Themes.

The book has impressed me. It is well-written, using clear language, useful diagrams and figures, and a logical progression of ideas. It starts with the basics, talking about what a theme is and defining its components. Then, it moves into the details of modifying the default themes. Up to this point, I didn’t encounter anything new to me, but I was only up to chapter two.

Starting with chapter three, the book reveals and clearly describes each of the files and elements that make up a theme, using both the default PHPTemplate engine as well as other options. Later, the book teaches how to use and master this template engine to create your own custom theme, including how to create custom looks for specific pages, modules, and more.

In between the two, you learn how to download ready-made themes, contributed by the Drupal user and developer community, and modify them for personal use, knowing how and where to look for usage restrictions to avoid problems.

Finally, the book ends with some very useful appendices that show where to find every css file and what it affects, and other useful tooks and kits for developers.

This book has saved me a lot of time, and is well done. If you are responsible for a site powered by Drupal, you may find it useful as well.

The Manga Guide to Databases

I have used relational databases for years. I’ve used them to store mailing lists, email account data for postfix, blog and forum data, and more. They are convenient and powerful time savers. Most of what I have learned has been indirectly learned while studying something else; documentation for a computer programming language like PHP or Python, a book on website design for commerce, or documentation and code for an open source project like WordPress or Drupal. As a result, my knowledge is adequate for simple tasks and queries, but I’m nowhere near ready to be a database admin. What I know is incomplete, adequate for my actual needs, but with gaping holes in my knowledge.

Until this week, I was comfortable with this fact.

Since I read and enjoyed the other manga guides in this series that I have read, I picked up a copy of The Manga Guide to Databases. I wanted to see whether the book made the subject interesting and whether it taught the subject well.

The short answer is that I found the systematic and foundational introduction to database design clear, interesting, and enjoyable–so much so that I have ordered a few more books on database theory and design and SQL for further study (stay tuned, I may review them later). Contrast that with the indirect introductions I have previously encountered that made me want to ignore the topic, except for the aspects vital to my task(s) at hand.

The Manga Guide to Databases uses a somewhat silly, but pleasant story with well drawn artwork to ease the reader into a complicated subject that requires paying a bit of attention to for comprehension. It begins with the assumption that the reader knows nothing about the topic, so it would be perfect as a base level introductory text, especially for high school aged readers (or those of us who are a bit older, but who still enjoy a bit of whimsy).

We start with the question “What is a database?” and a great description of how and why they are useful. We move into a basic definition of relational databases with a very brief mention of other types of databases that exist. The fun continues with a chance to design a database around the model of entities and relationships.

Once the foundation is laid with a conceptual understanding of databases and their design, structured query language (SQL) is introduced. I was thrilled to discover this wasn’t product specific language, but rather standard ANSI, at least as far as I can tell (okay, I know a little more than I let on earlier, but I still consider myself a database/SQL novice). That is great, because it means that whatever is learned here should apply anywhere, whether using MySQL, PostgreSQL, Oracle, Access, Microsoft SQL Server, or whatever, so long as the product conforms to the standard. This certainly isn’t a complete SQL introductory text, but it is enough to get a person started understanding the basic concepts and how to operate a database.

Ultimately, the book was a success. I wouldn’t kid anyone into thinking that reading and understanding this book would make a person capable of real database administration, but it does give a clear and solid foundation for further study, and in my case has whet my appetite for going back to fill in some personal knowledge gaps that I have been content to let exist for a very long time.

Learning JavaScript

I recently became the maintainer for a site that uses a bit of JavaScript. Surprisingly, I have no previous experience writing or maintaining anything in JavaScript, so I needed to get up to speed, if only to know what is going on. To help me out, I picked up a copy of Shelley Powers’ new book, Learning JavaScript.

I am familiar with Shelley Powers’ work, having read Unix Power Tools, a book that holds a special place on my shelf for its usefulness and depth. She is obviously someone who knows what she is doing.

This book states in the preface that

Readers of this book should be familiar with web page technology, including Cascading Style Sheets (CSS) and HTML/XHTML. Previous programming experience isn’t required, though some sections may require extra review if you have no previous exposure to programming.

That sums up the only negative thing I might be tempted to say about the book. Usually, O’Reilly’s “Learning” series books are excellent resources for complete newcomers and are the sort of books that I might point a novice toward. While this book is designed for the JavaScript novice, it is not the best resource for a programming/web creation novice. If you can’t follow or understand the following quoted sentences, this book isn’t for you. If you can, this book does an excellent job of covering the basics of JavaScript in depth and may end up being the only text you need on the topic.

JavaScript has just three primitive data types: string, numeric, and boolean. Each is differentiated from the others by the type of value it contains: string, numeric, and boolean, respectively.

My first exposure to programming was in 1981, using BASIC on a Radio Shack TRS-80 Color Computer. Over the years, I have studied (and often forgotten, but can certainly read a bit and understand the concepts of) LISP, C, Bash scripting, Perl, Python, and more. Lately I have spent more time using PHP and enjoying it, since that is the language of things like WordPress, Drupal, vBulletin and other commonly used CMS and interactive web site software. The two sentences quoted above seem perfectly clear to me, but I can imagine what they must seem like to someone with no experience with programming languages. So, now you have been warned. Let’s get to the good stuff for those who understand the jargon.

This book is clear, with enough detail to help you understand what is happening without bogging you down in the minutiae. The reader is expected to see how and why each facet of the language would be useful, so the examples given are simple and seem to be designed to help her get the feel for usage without pretending to be a cookbook of programming recipes, although some seem quite useful as they are such as Chapter 6’s code for browser detection, which allows you to modify content and/or how it is displayed based on the web browser being used to view your site.

The text covers everything you are likely to need while using JavaScript, and more importantly for me, most everything a person is likely to stumble across when reading existing code. You get a solid introduction to data types and variables, operators, statements, objects, and functions. This is built upon with chapters on debugging and cross-browser compatibility. This new second edition (just released earlier in 2009) has several updates and changes from the previous version including a wonderful comparison of the benefits of generating and processing XML data using Ajax versus using JSON.

If you are considering using JavaScript on a website that includes the dynamic creation of web pages, or if you have inherited one that you are now responsible for maintaining, this book would be a good resource to help you start to understand one of the more common languages used for doing so. This is especially true if you have any prior experience with any other programming language. If you want a cookbook, scripted-style, “do this, now do that” sort of beginner’s guide to making a site that holds your hand through the whole process, this is not the book you want.

A short how-to for switching from Apache to nginx

Note: this post is outdated. Use at your own risk.

I am running my sites on a VPS from Slicehost and have had a very good experience. When I started, I set everything up using Apache 2, since that is what I am most familiar and adept with using. Apache works well, but likes more memory than I have in my server. This caused me to use my swap far too much.

I worked with the Apache configuration, finally coming up with the changes below for my /etc/apache2/apache2.conf file, which minimized the swapping, but wasn’t quite enough for me to be happy. Also, if more than one or two people were browsing the site at a time, it forced everything to go much more slowly, because it now got backed up in a queue instead of being served quickly (this was by design, to save memory and prevent swapping). That was not acceptable. Here you can see the changes I had made to get it to run without swapping all the time in a lean-memory environment. I have removed everything but the necessary changes I had made, just to save space here. Apache is so well documented, you should be able to figure the file out as needed using readily available info, if you need to.

Timeout 30
KeepAlive On
MaxMemFree 262144
MaxKeepAliveRequests 2
KeepAliveTimeout 1
<IfModule mpm_prefork_module>
StartServers 2
MinSpareServers 1
MaxSpareServers 3
MaxClients 4
MaxRequestsPerChild 5
</IfModule>
<IfModule mpm_worker_module>
StartServers 2
MaxClients 5
MinSpareThreads 2
MaxSpareThreads 5
ThreadsPerChild 2
MaxRequestsPerChild 3
</IfModule>
HostnameLookups Off

I did some reading and decided to try nginx as a replacement for httpd. The Slicehost folks also have some great how-tos (not only on nginx) and helpful people in their forums, and my friend, Ryan, has a helpful post as well.

There are lots of how-tos around for installing from scratch on a server. Most are good. I am focusing on migrating from Apache2 to nginx. For that, I will assume you have your site set up and running on Apache, your DNS is set properly and pointing to the server, etc.

First, make sure your system is up to date, especially with all security updates. Then, install nginx, either from source or from your Linux distribution’s package repositories. I chose the latter. You also want to install the cgi version of php5. On my Ubuntu 8.10 server, I did this:

sudo aptitude update && sudo aptitude safe-upgrade

followed by:

sudo aptitude install nginx php5-cgi

I made a couple of modifications to my /etc/nginx/nginx.conf file. Here is what I have. My server has two dual core processors, hence the 4 worker processes. I lowered the keep-alive timeout, added the gzip config, and made a couple more simple changes.

user www-data;
worker_processes 4;

error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

access_log /var/log/nginx/access.log;

sendfile on;
tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 3;
tcp_nodelay off;

gzip on;
gzip_comp_level 2;
gzip_proxied any;
gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml
application/xml+rss text/javascript;

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}

After that, I used the php-fastcgi script I found here and put it in /etc/init.d/php-fastcgi.

#! /bin/sh
### BEGIN INIT INFO
# Provides:          php-fastcgi
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start and stop php-cgi in external FASTCGI mode
# Description:       Start and stop php-cgi in external FASTCGI mode
### END INIT INFO

# Author: Kurt Zankl <kz@xon.uni.cc>

# Do NOT "set -e"

PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="php-cgi in external FASTCGI mode"
NAME=php-fastcgi
DAEMON=/usr/bin/php-cgi
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

# If the daemon is not enabled, give the user a warning and then exit,
# unless we are stopping the daemon
if [ "$START" != "yes" -a "$1" != "stop" ]; then
log_warning_msg "To enable $NAME, edit /etc/default/$NAME and set START=yes"
exit 0
fi

# Process configuration
export PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS
DAEMON_ARGS="-q -b $FCGI_HOST:$FCGI_PORT"

do_start()
{
# Return
#   0 if daemon has been started
#   1 if daemon was already running
#   2 if daemon could not be started
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null || return 1
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --background --make-pidfile --chuid $EXEC_AS_USER --startas $DAEMON -- $DAEMON_ARGS || return 2
}

do_stop()
{
# Return
#   0 if daemon has been stopped
#   1 if daemon was already stopped
#   2 if daemon could not be stopped
#   other if a failure occurred
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE > /dev/null # --name $DAEMON
RETVAL="$?"
[ "$RETVAL" = 2 ] && return 2
# Wait for children to finish too if this is a daemon that forks
# and if the daemon is only ever run from this initscript.
# If the above conditions are not satisfied then add some other code
# that waits for the process to drop all resources that could be
# needed by services started subsequently.  A last resort is to
# sleep for some time.
start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
[ "$?" = 2 ] && return 2
# Many daemons don't delete their pidfiles when they exit.
rm -f $PIDFILE
return "$RETVAL"
}

case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
do_start
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
restart|force-reload)
log_daemon_msg "Restarting $DESC" "$NAME"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
exit 3
;;
esac

Then, I set the ownership and permissions appropriately.

sudo chmod u+x /etc/init.d/php-fastcgi

and

sudo chown 0.0 /etc/init.d/php-fastcgi

and set it to run at boot

sudo update-rc.d php-fastcgi defaults 21 23

Most how-to articles tell you here to create the directories to hold your website(s). Since I am doing this on a site that was already running Apache, my sites already existed in /home/myusername/public_html/sitename/public. Note where yours is, because you will need that info.

Ngnix works similarly to Apache for multiple domains. With each you can set up more than one domain on an IP address using virtual domains. If you only have one domain, the setup is easier, but outside the scope of this article. Apache2 puts the sites in directories in /etc/apache2/sites-available for setup, with symbolic links to there for active sites from /etc/apache2/sites-enabled. Nginx can do the same, using /etc/nginx/sites-available and /etc/nginx/sites-enabled.

Make a virtual host (vhost) file for each domain/site you plan to use in /etc/nginx/sites-available. Here’s a sample, based on this domain, but with sensitive info changed:

/etc/nginx/sites-available/matthewhelmke.net

server {
listen  80;
server_name  www.matthewhelmke.net;
rewrite ^/(.*) http://matthewhelmke.net/$1 permanent;
}

server {
listen  80;
server_name  matthewhelmke.net;

access_log  /home/myusername/public_html/matthewhelmke.net/log/access.log;
error_log   /home/myusername/public_html/matthewhelmke.net/log/error.log;

location / {
root   /home/myusername/public_html/matthewhelmke.net/public;
index  index.html index.htm index.php;

# this serves static files that exist without running other rewrite tests
if (-f $request_filename) {
expires 30d;
break;
}

# this sends all non-existing file or directory requests to index.php
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php?q=$1 last;
}

}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  /home/myusername/public_html/matthewhelmke.net/public$fastcgi_script_name;
include fastcgi_params;
}
}

The first server section rewrites all requests for the domain that use www in front to the same url without the www. The second server section is where all the fun happens. Both are listening on port 80, the standard for http. The second tells nginx where to put the log files (in a special log directory I created for each domain, in each domain’s directory in my /home directory).

The location section is especially important. It tells where to find the root directory for my site, in my case, in a special directory called public. Then, I have two special rewrites. These allow you to use permalinks aka clean urls aka pretty urls aka SEO urls with your site. Once these lines are in the vhost file, you will discover that both WordPress and Drupal allow you to adjust the settings in their admin panels to use them as you would like. I haven’t tried it with other types of content management systems, but I think it would probably work. Without these lines, you have to use the standard url formats.

The other incredibly important bit is that last section. It tells nginx to send all php files to the fastcgi script to be interpreted. Without this, you won’t get php to work.

Once you get your vhost file set up, create a link to it from the sites-enabled directory so that nginx will know to use it.

cd /etc/nginx/sites-enabled/ && sudo ln -s ../sites-available/yourdomain.com

Then, to test it out, start the fast-cgi script, turn off Apache, and start nginx.

/etc/init.d/php-fastcgi start

/etc/init.d/apache2 stop

/etc/init.d/nginx start

If everything is set correctly, you should now be able to view your site using nginx. If it isn’t, you can always stop nginx and start apache2 again to keep your site up while you try to figure out the problem. Once it is up, tested thoroughly, and you are happy, you can remove Apache if you like.

Note: I had some trouble with the fast-cgi script at first, having copied one that had special characters put in it. That happens sometimes when you post code with WordPress. I’m trying to prevent it here by using code tags, but they reset any time there is a blank line, so you may need to cut/paste in sections. If you have problems, and all you did was cut & paste, look at this first as the likely cause.