How to install nginx with PHP as FastCGI plus WordPress

Hello, it’s my first article in English at the blog. I hope it’s not the last. And I want to start from very interesting thing — Nginx server. I know that English part of Internet doesn’t have many manuals or instructions about Nginx. I want to correct this situation a little bit. This article consists of: what is Nginx, how to install Nginx with PHP, MySQL and FastCGI and how to use it with WordPress (WordPress is very popular and I use it, that’s why I chose it). In my next articles I’ll continue to write about Nginx, cuz it has too much functions and options for one article. But it’s not complicated. I hope it will be useful for you. So, let’s start.

What is nginx?

Nginx [engine x] — it’s HTTP-server and IMAP/POP3-proxy server written by Igor Sysoev. According to Netcraft in May 2009 nginx served or proxied 3.25% busiest sites. Nginx has been running for more than four years on many heavily loaded Russian sites like vkontakte.ru or rambler.ru. I think it’s enough. You can find a lot of theoretical information below in useful links.

How to install server from source?

If you use Debian-based distributive as usual you can use command:

sudo apt-get install nginx
sudo apt-get install nginx

But it will be not a fresh version and if you use another distributive you can’t use great Debian repository. For start you have to download the newest version from the official site by commands:

cd ~/user
mkdir server
cd server 
wget http://sysoev.ru/nginx/nginx-0.7.59.tar.gz
cd ~/user
mkdir server
cd server 
wget http://sysoev.ru/nginx/nginx-0.7.59.tar.gz

Unpack archives:

tar xvf nginx-0.7.59
cd nginx-0.7.59
tar xvf nginx-0.7.59
cd nginx-0.7.59

You must have C compiler to compile a program. If you don’t have please do the next:

sudo aptitude install build-essential
sudo aptitude install linux-headers-`uname -r`
sudo aptitude install build-essential
sudo aptitude install linux-headers-`uname -r`

Configure:

./configure
./configure

In the end, 100% you’ll have the next messages:

./configure: error: the HTTP rewrite module requires the PCRE library.
 
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.</path>
./configure: error: the HTTP rewrite module requires the PCRE library.

You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.</path>

It’s normal, you can’t have all the libraries on your computer. We can install it:

sudo aptitude install libpcre3 libpcre3-dev libpcrecpp0 libssl-dev zlib1g-dev
sudo aptitude install libpcre3 libpcre3-dev libpcrecpp0 libssl-dev zlib1g-dev

Start to compile it once again:

sudo make clean
sudo ./configure --with-http_ssl_module
sudo make 
sudo make install
sudo make clean
sudo ./configure --with-http_ssl_module
sudo make 
sudo make install

Now, everything will be ok and server will be installed in your system. If you want you can change some default options doing command «./configure» for example:

--prefix=<path></path>
--prefix=<path></path>

— to set the directory where the server files will be located. This directory will be used for all ways you’ll set by command «./configure» and in configuration file nginx.conf too. Default it’s /usr/local/nginx.

--sbin-path=<path></path>
--sbin-path=<path></path>

— to set the name of the Nginx executive file. This name is used just for stage of installing. Default it’s /usr/local/nginx/sbin/nginx.

--conf-path=<path></path>
--conf-path=<path></path>

— to set the name for the nginx.conf configuration file. In any time you can run Nginx with another configuration file which will be located in another directory with option «-c «. Default it’s /usr/local/nginx/conf/nginx.conf

--pid-path=<path></path>
--pid-path=<path></path>

— to set the name for PID file. But after installing of the server in any time you can change the name in the nginx.conf configuration file.
Default it’s /usr/local/nginx/logs/nginx.pid

--error-log-path=<path></path>
--error-log-path=<path></path>

— to set the name for the error log file. After installing you can change the name in the nginx.conf configuration file. Default it’s /usr/local/nginx/logs/error.log

--http-log-path=<path></path>
--http-log-path=<path></path>

— to set the name for the access.log file.
Access.log it’s the file for registration of requests from the server. Default it’s /usr/local/nginx/logs/access.log.

How do I manage the server?

If you did’t change the default directories when you were installing the server, you can run the server by command:

sudo /usr/local/nginx/sbin/nginx
sudo /usr/local/nginx/sbin/nginx

Check the server.

ps -ef | grep nginx
root     14999     1  0 16:44 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   15000 14999  0 16:44 ?        00:00:00 nginx: worker process      
booch    15084 29644  0 16:45 pts/0    00:00:00 grep nginx
ps -ef | grep nginx
root     14999     1  0 16:44 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   15000 14999  0 16:44 ?        00:00:00 nginx: worker process      
booch    15084 29644  0 16:45 pts/0    00:00:00 grep nginx

Great! Now you can open your browser and put http://localhost If server works you’ll see «Welcome»
Shutting down the server is possible by sending QUIT signal to the master process.

kill -QUIT <pid></pid>
kill -QUIT <pid></pid>

Upgrading the server in the work process is possible by sending USR2 signal to the master process.

kill -USR2 <pid></pid>
kill -USR2 <pid></pid>

If you have already changed some options in the nginx.conf and you want to apply it you have to send HUP signal to the master process.

kill -HUP <pid></pid>
kill -HUP <pid></pid>

Log rotation. All log files have to be renamed, after this USR1 signal has to be sent to the master process. The master process will open all opened files once again and set them as unprivileged user. All worker processes work under this user. After successful opening master process will close all opened files and will send messages to worker processes to reopen files too. They also will open new files and in the same time will close all old files. In result all old files will be ready for further processing, for example, to compress them.

kill -USR1 <pid></pid>
kill -USR1 <pid></pid>

Important Signals:

QUIT — normal shut down
TERM, INT — fast shut down
HUP — reconfiguration, update the changed time zone, launch of new worker processes with the new configuration, normal shut down of worker processes.
USR1 — to reopen log files
USR2 — to upgrade executive file
WINCH — normal shut down of worker processes

How to run web site on the server?

Let’s start to do our server. We have to create new user (www), new group (www) and new structure for the site.
Let’s begin from the group:

sudo groupadd www
sudo groupadd www

New user:

sudo useradd www -g www
sudo useradd www -g www

Add www user to www group:

sudo usermod -a -G www www
sudo usermod -a -G www www

Create password for www user:

sudo passwd www
sudo passwd www

Let’s create a structure for the web site. I think it will be like this:

sudo mkdir /home/www/
sudo mkdir -p /home/www/linuxspace.org/{log, private, public, backups, archives, stats}
sudo mkdir /home/www/
sudo mkdir -p /home/www/linuxspace.org/{log, private, public, backups, archives, stats}

log — directory for log files
private — private data
public — directory for the site
backups — directory for backup data
stats — directory for stats
archives — for archives

Please, be attentive with the rights. For all directories it’s 644 (rw+r+r), and for public it’s 744 (rwx+r+r).
And now you have to correct configuration file nginx.conf

sudo nano /usr/local/nginx/conf/nginx.conf
sudo nano /usr/local/nginx/conf/nginx.conf

Write there:

#user
user  www;
 
#numbers of work process
worker_processes  2;
 
#address of PID file
pid     /home/www/linuxspace.org/nginx.pid;
 
events 
{
    worker_connections  1024;
}
 
http 
{
    include       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"';
 
    sendfile        on;
    tcp_nopush     on;
 
    #keepalive_timeout  0;
    keepalive_timeout  65;
 
    #compression 
    gzip on;
    gzip_min_length  1100;
    gzip_buffers     4 8k;
    gzip_types       text/plain;
    gzip_comp_level 3;
    gzip_proxied any;
 
    #configure the virtual host
server {
        #port
        listen       *:90;
 
        #name of server
        server_name  localhost;
 
        #coding
        charset utf-8;
 
        #it's general directory, when will be site
    root /home/www/linuxspace.org/public;
 
        #LOGS
    #------------------------------------------------------------------
        access_log  /home/www/linuxspace.org/log/localhost.access.log;
        error_log /home/www/linuxspace.org/log/error.log;
        access_log  /home/www/linuxspace.org/log/access.log;
        #------------------------------------------------------------------
 
        #files which server will read in the general directory
        location / {
            root   /home/www/linuxspace.org/public;
            index  index.html index.htm index.php;
        }
         
        error_page  404              /404.html;
 
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
 
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
 
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
       }
 
 
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
 
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
 
 
    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;
 
    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
 
    #    ssl_session_timeout  5m;
 
    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
    #    ssl_prefer_server_ciphers   on;
 
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}
#user
user  www;
 
#numbers of work process
worker_processes  2;
 
#address of PID file
pid     /home/www/linuxspace.org/nginx.pid;

events 
{
    worker_connections  1024;
}
 
http 
{
    include       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"';
 
    sendfile        on;
    tcp_nopush     on;
 
    #keepalive_timeout  0;
    keepalive_timeout  65;
 
    #compression 
    gzip on;
    gzip_min_length  1100;
    gzip_buffers     4 8k;
    gzip_types       text/plain;
    gzip_comp_level 3;
    gzip_proxied any;
 
    #configure the virtual host
server {
        #port
        listen       *:90;
 
        #name of server
        server_name  localhost;
 
        #coding
        charset utf-8;
 
        #it's general directory, when will be site
	root /home/www/linuxspace.org/public;
 
        #LOGS
	#------------------------------------------------------------------
        access_log  /home/www/linuxspace.org/log/localhost.access.log;
        error_log /home/www/linuxspace.org/log/error.log;
        access_log  /home/www/linuxspace.org/log/access.log;
        #------------------------------------------------------------------
 
        #files which server will read in the general directory
        location / {
            root   /home/www/linuxspace.org/public;
            index  index.html index.htm index.php;
        }
         
        error_page  404              /404.html;
 
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
 
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
 
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
       }
 
 
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
 
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
 
 
    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;
 
    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
 
    #    ssl_session_timeout  5m;
 
    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
    #    ssl_prefer_server_ciphers   on;
 
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}

It’s enough to run the html site on the server. But, please, don’t delete another options, you’ll need it in the future.

Remember, if you want to use another nginx.conf file, you can run server using option «-c»:

sudo /usr/local/nginx/sbin/nginx -c /home/user/nginx.conf
sudo /usr/local/nginx/sbin/nginx -c /home/user/nginx.conf

Let’s run the server:

sudo /usr/local/nginx/sbin/nginx
sudo /usr/local/nginx/sbin/nginx

If everything is ok, you’ll see the server is in the process:

ps -ef | grep nginx
root      7146     1  0 02:19 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
www     7147  7146  0 02:19 ?      00:00:00 nginx: worker process      
www     7148  7146  0 02:19 ?      00:00:00 nginx: worker process 
ps -ef | grep nginx
root      7146     1  0 02:19 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
www     7147  7146  0 02:19 ?      00:00:00 nginx: worker process      
www     7148  7146  0 02:19 ?      00:00:00 nginx: worker process 

Great. It works. The master process has root rights and worker processes have rights from the www user. So, now you can put html page index.html into the /home/www/linuxspace.org/public directory. Then, please, open browser and print http://localhost You’ll see your site.

How to install Nginx with PHP as FastCGI, MySQL and WordPress

So, now you know how to install and manage the Nginx server and how to configure nginx.conf configuration file. But it’s not enough. If you want to have a real server with any real site like WordPress or Drupal, you need to install PHP and run it in FastCGI mode. Also you need to install MySQL data base and WordPress. How to do it, I’ll write farther.

Installing PHP

sudo aptitude install php5-common php5-dev php5-mysql php5-sqlite php5-tidy php5-xmlrpc php5-xsl php5-cgi php5-mcrypt php5-curl php5-gd php5-mhash php5-pspell php5-snmp php5-sqlite libmagick9-dev php5-client
sudo aptitude install php5-common php5-dev php5-mysql php5-sqlite php5-tidy php5-xmlrpc php5-xsl php5-cgi php5-mcrypt php5-curl php5-gd php5-mhash php5-pspell php5-snmp php5-sqlite libmagick9-dev php5-client

Installing MySQL

sudo aptitude install mysql-server mysql-client libmysqlclient15-dev
sudo aptitude install mysql-server mysql-client libmysqlclient15-dev

Installing FastCGI
You need to take spawn-cgi out from lighttpd server, but, pay attention at the server version.
If you download last 1.4.23 server version and compile it, it will be impossible to find
spawn-cgi inside. Why? Cuz this is the answer from the official site:

Important changes
* Fix workaround for incorrect path info/scriptname if fastcgi prefix is ”/” (fixes #729)
* Finally removed spawn-fcgi
* Fix bug with FastCGI request id overflow under high load; just use always id 1 as we don’t use multiplexing. (thx jgray)
* Workaround broken operating systems: check for trailing ’/’ in filenames (fixes #1989)

That’s why you need to download an older version, lighttpd 1.4.19 is good:

wget http://www.lighttpd.net/download/lighttpd-1.4.19.tar.bz2
wget http://www.lighttpd.net/download/lighttpd-1.4.19.tar.bz2

Uncompress it:

tar jxvf lighttpd-1.4.19.tar.bz2
cd lighttpd-1.4.19
sudo ./configure
tar jxvf lighttpd-1.4.19.tar.bz2
cd lighttpd-1.4.19
sudo ./configure

If you don’t have all libraries, you’ll see the next error:
configure: error: pcre-config not found, install the pcre-devel package or build with –without-pcre

Installing packages:

sudo aptitude install libpcre3-dev libbz2-dev
sudo aptitude install libpcre3-dev libbz2-dev

And once again:

sudo ./configure
sudo ./configure
sudo make
sudo make

After this you have to move spawn-fcgi in /usr/bin/ directory:

sudo cp src/spawn-fcgi /usr/bin/spawn-fcgi
sudo cp src/spawn-fcgi /usr/bin/spawn-fcgi

Create executive script:

sudo nano /usr/bin/php-fastcgi
sudo nano /usr/bin/php-fastcgi

And put next lines inside:

#!/bin/sh
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www -g www -f /usr/bin/php5-cgi
#!/bin/sh
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www -g www -f /usr/bin/php5-cgi

Where:
/usr/bin/spawn-fcgi -a 127.0.0.1 — executive command for localhost
-p 9000 — number of port
-u www -g www — user www and group www
-f /usr/bin/php5-cgi — path to php5-cgi

Create executive script for FastCGI:

sudo nano /etc/init.d/init-fastcgi
sudo nano /etc/init.d/init-fastcgi

And put next lines inside:

#!/bin/bash
PHP_SCRIPT=/usr/bin/php-fastcgi
RETVAL=0
case "$1" in
start)
$PHP_SCRIPT
RETVAL=$?
;;
stop)
killall -9 php5-cgi
RETVAL=$?
;;
restart)
killall -9 php5-cgi
$PHP_SCRIPT
RETVAL=$?
;;
*)
echo "Usage: php-fastcgi {start|stop|restart}"
exit 1
;;
esac
exit $RETVAL
#!/bin/bash
PHP_SCRIPT=/usr/bin/php-fastcgi
RETVAL=0
case "$1" in
start)
$PHP_SCRIPT
RETVAL=$?
;;
stop)
killall -9 php5-cgi
RETVAL=$?
;;
restart)
killall -9 php5-cgi
$PHP_SCRIPT
RETVAL=$?
;;
*)
echo "Usage: php-fastcgi {start|stop|restart}"
exit 1
;;
esac
exit $RETVAL

Change rights for scripts:

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

And start script init-fastcgi. This script will run PHP in cgi mode that is good for us:

sudo /etc/init.d/init-fastcgi start
sudo /etc/init.d/init-fastcgi start

Check the process:

ps -ef | grep cgi
www      32573     1  2 04:04 ?        00:00:00 /usr/bin/php5-cgi
www      32575 32573  0 04:04 ?        00:00:00 /usr/bin/php5-cgi
www      32576 32573  0 04:04 ?        00:00:00 /usr/bin/php5-cgi
www      32577 32573  0 04:04 ?        00:00:00 /usr/bin/php5-cgi
www      32578 32573  0 04:04 ?        00:00:00 /usr/bin/php5-cgi
www      32579 32573  0 04:04 ?        00:00:00 /usr/bin/php5-cgi
ps -ef | grep cgi
www      32573     1  2 04:04 ?        00:00:00 /usr/bin/php5-cgi
www      32575 32573  0 04:04 ?        00:00:00 /usr/bin/php5-cgi
www      32576 32573  0 04:04 ?        00:00:00 /usr/bin/php5-cgi
www      32577 32573  0 04:04 ?        00:00:00 /usr/bin/php5-cgi
www      32578 32573  0 04:04 ?        00:00:00 /usr/bin/php5-cgi
www      32579 32573  0 04:04 ?        00:00:00 /usr/bin/php5-cgi

It will run automaticly if you put next command:

sudo update-rc.d init-fastcgi defaults
sudo update-rc.d init-fastcgi defaults

Ok, now PHP works as FastCGI and you have to corret nginx.conf to configure your nginx server to work with PHP. So please, open your nginx.conf and correct it like I did:

#user
user  www;
#numbers of worker processes
worker_processes  2;
 
#path to nginx.pid
pid     /home/www/linuxspace.org/nginx.pid;
 
events 
{
    worker_connections  1024;
}
 
http 
{
    include       mime.types;
    default_type  application/octet-stream;
 
    #log format
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                     '$status $body_bytes_sent "$http_referer" '
                     '"$http_user_agent" "$http_x_forwarded_for"';
 
    sendfile        on;
    tcp_nopush     on;
 
    #keepalive_timeout  0;
    keepalive_timeout  65;
 
    #compression
    gzip on;
    gzip_min_length  1100;
    gzip_buffers     4 8k;
    gzip_types       text/plain;
    gzip_comp_level 3;
    gzip_proxied any;
 
#configure virtual server
server {
 
        #port
        listen       80;
 
        #server name 
        server_name  localhost;
 
        #coding
        charset utf-8;
 
       #main directory where is site 
    root /home/www/linuxspace.org/public;
 
        #LOGS
    #------------------------------------------------------------------
        access_log  /home/www/linuxspace.org/log/localhost.access.log;
        error_log /home/www/linuxspace.org/log/error.log;
        access_log  /home/www/linuxspace.org/log/access.log combined;
        #------------------------------------------------------------------
        #location
        location / {
            root   /home/www/linuxspace.org/public;
            index  index.html index.htm index.php;
        }
 
        error_page  404              /404.html;
 
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        # location ~\.php$ {
        #   proxy_pass   http://127.0.0.1;
        #                   }
 
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #PHP + CGI + some parametrs
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /home/www/linuxspace.org/public/$fastcgi_script_name;
            include        fastcgi_params;
            fastcgi_param  QUERY_STRING     $query_string;
            fastcgi_param  REQUEST_METHOD   $request_method;
            fastcgi_param  CONTENT_TYPE     $content_type;
            fastcgi_param  CONTENT_LENGTH   $content_length;
 
        }
 
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
       }
 
 
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
 
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
 
 
    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;
 
    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
 
    #    ssl_session_timeout  5m;
 
    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
    #    ssl_prefer_server_ciphers   on;
 
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
 
}
#user
user  www;
#numbers of worker processes
worker_processes  2;

#path to nginx.pid
pid     /home/www/linuxspace.org/nginx.pid;

events 
{
    worker_connections  1024;
}

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

    #log format
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                     '$status $body_bytes_sent "$http_referer" '
                     '"$http_user_agent" "$http_x_forwarded_for"';

    sendfile        on;
    tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #compression
    gzip on;
    gzip_min_length  1100;
    gzip_buffers     4 8k;
    gzip_types       text/plain;
    gzip_comp_level 3;
    gzip_proxied any;

#configure virtual server
server {

        #port
        listen       80;

        #server name 
        server_name  localhost;

        #coding
        charset utf-8;

       #main directory where is site 
	root /home/www/linuxspace.org/public;

        #LOGS
	#------------------------------------------------------------------
        access_log  /home/www/linuxspace.org/log/localhost.access.log;
        error_log /home/www/linuxspace.org/log/error.log;
        access_log  /home/www/linuxspace.org/log/access.log combined;
        #------------------------------------------------------------------
        #location
        location / {
            root   /home/www/linuxspace.org/public;
            index  index.html index.htm index.php;
        }

        error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        # location ~\.php$ {
        #   proxy_pass   http://127.0.0.1;
        #                   }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #PHP + CGI + some parametrs
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /home/www/linuxspace.org/public/$fastcgi_script_name;
            include        fastcgi_params;
            fastcgi_param  QUERY_STRING     $query_string;
            fastcgi_param  REQUEST_METHOD   $request_method;
            fastcgi_param  CONTENT_TYPE     $content_type;
            fastcgi_param  CONTENT_LENGTH   $content_length;

        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
       }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

Save and close file.
Restart your server and put in directory /home/www/linuxspace.org/public file test.php:

sudo nano /home/www/linuxspace.org/public
sudo nano /home/www/linuxspace.org/public

Put there:

<html>
<head>
 
</head>
<body>
<p>This is an HTML line
</p><p>
< ?php
echo “This is a PHP line”;
phpinfo();
?>
</p></body></html>
<html>
<head>

</head>
<body>
<p>This is an HTML line
</p><p>
< ?php
echo “This is a PHP line”;
phpinfo();
?>
</p></body></html>

Save it.
Please, open browser and put http://localhost:80
You have to see full information about your PHP. If you don’t see it something is wrong. Please, write once again the article and correct mistake.

Continue… Download WordPress and put it in the directory /home/www/linuxspace.org/public:

wget http://ru.wordpress.org/wordpress-2.8-ru_RU.zip
unzip wordpress-2.8-ru_RU.zip
cd wordpress
sudo mv * /home/www/linuxspace.org/public/
wget http://ru.wordpress.org/wordpress-2.8-ru_RU.zip
unzip wordpress-2.8-ru_RU.zip
cd wordpress
sudo mv * /home/www/linuxspace.org/public/

Put http://localhost once again in browser and you’ll see Welcome to WordPress Window, but you can’t continue installing yet, cuz you don’t have data base. Create it.

Start MySQL:

sudo /etc/init.d/mysql start
sudo /etc/init.d/mysql start

Configure MySQL:

sudo /usr/bin/mysql_secure_installation
sudo /usr/bin/mysql_secure_installation

Install the administrative tables:

sudo /usr/bin/mysql_install_db
sudo /usr/bin/mysql_install_db

Go into MySQL:

mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 68
Server version: 5.0.75-0ubuntu10.2 (Ubuntu)
 
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
 
mysql>
mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 68
Server version: 5.0.75-0ubuntu10.2 (Ubuntu)
 
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
 
mysql>

And create table for WordPress:

mysql> CREATE DATABASE `wordpress` CHARACTER SET utf8 COLLATE utf8_general_ci;
mysql> CREATE DATABASE `wordpress` CHARACTER SET utf8 COLLATE utf8_general_ci;

Where:
db_name — data base name

Check it out:

mysql> SHOW CREATE DATABASE `wordpress`;
mysql> SHOW CREATE DATABASE `wordpress`;
mysql> SHOW CREATE DATABASE `wordpress`;
+----------+----------------------------------------------------------------+
| Database | Create Database                                                |
+----------+----------------------------------------------------------------+
| my_db    | CREATE DATABASE `my_db` /*!40100 DEFAULT CHARACTER SET utf8 */ | 
+----------+----------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> SHOW CREATE DATABASE `wordpress`;
+----------+----------------------------------------------------------------+
| Database | Create Database                                                |
+----------+----------------------------------------------------------------+
| my_db    | CREATE DATABASE `my_db` /*!40100 DEFAULT CHARACTER SET utf8 */ | 
+----------+----------------------------------------------------------------+
1 row in set (0.00 sec)

Finish work with MySQL:

mysql>QUIT
mysql>QUIT

Ok, restart MySQL and continue installing WordPress using for field DB in WordPRess name your data base. In few minutes WordPress will installed and you can start to write you blog. I think it nice to use this server and in next articles I’ll tell how to use nginx with Apache server. Thank you and bye!

Useful links:

http://wiki.nginx.org/Main
http://www.nginx.eu/
http://sysoev.ru/en/
http://blog.kovyrin.net/2006/04/04/nginx-small-powerful-web-server/lang/en/

A lot of thanks to my personal redactor Anna