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:
1 | 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:
1 2 3 4 | cd ~/user mkdir server cd server wget http://sysoev.ru/nginx/nginx-0.7.59.tar.gz |
Unpack archives:
1 2 | 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:
1 2 | sudo aptitude install build-essential sudo aptitude install linux-headers-`uname -r` |
Configure.
1 | ./configure |
In the end, 100% you’ll have the next messages:
1 2 3 4 | ./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. |
It’s normal, you can’t have all the libraries on your computer. We can install it.
1 | sudo aptitude install libpcre3 libpcre3-dev libpcrecpp0 libssl-dev zlib1g-dev |
Start to compile it once again
1 2 3 4 | 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:
1 | --prefix=<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.
1 | --sbin-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.
1 | --conf-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
1 | --pid-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
1 | --error-log-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
1 | --http-log-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:
1 | sudo /usr/local/nginx/sbin/nginx |
Check the server.
1 2 3 4 | 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.
1 | kill -QUIT <PID> |
Upgrading the server in the work process is possible by sending USR2 signal to the master process.
1 | kill -USR2 <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.
1 | kill -HUP <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.
1 | kill -USR1 <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.
1 | sudo groupadd www |
New user
1 | sudo useradd www -g www |
Add www user to www group.
1 | sudo usermod -a -G www www |
Create password for www user.
1 | sudo passwd www |
Let’s create a structure for the web site. I think it will be like this.
1 2 | 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
1 | sudo nano /usr/local/nginx/conf/nginx.conf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | #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»
1 | sudo /usr/local/nginx/sbin/nginx -c /home/user/nginx.conf |
Let’s run the server
1 | sudo /usr/local/nginx/sbin/nginx |
If everything is ok, you’ll see the server is in the process.
1 2 3 4 | 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
1 | 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
1 | 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.
1 | wget http://www.lighttpd.net/download/lighttpd-1.4.19.tar.bz2 |
Uncompress it
1 2 3 | 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
1 | sudo aptitude install libpcre3-dev libbz2-dev |
and once again
1 | sudo ./configure |
1 | sudo make |
after this you have to move spawn-fcgi in /usr/bin/ directory.
1 | sudo cp src/spawn-fcgi /usr/bin/spawn-fcgi |
Create executive script
1 | sudo nano /usr/bin/php-fastcgi |
and put next lines inside
1 2 | #!/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
1 | sudo nano /etc/init.d/init-fastcgi |
and put next lines inside
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #!/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
1 | sudo chmod +x /usr/bin/php-fastcgi |
1 | 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.
1 | sudo /etc/init.d/init-fastcgi start |
Check the process.
1 2 3 4 5 6 7 | 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
1 | 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | #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
1 | sudo nano /home/www/linuxspace.org/public |
1 2 3 4 5 6 7 8 9 10 11 12 | <html> <head> <title>PHP Test</title> </head> <body> <p>This is an HTML line <p> <?php echo “This is a PHP line”; phpinfo(); ?> </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
1 2 3 4 | 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
1 | sudo /etc/init.d/mysql start |
Configure MySQL
1 | sudo /usr/bin/mysql_secure_installation |
Install the administrative tables…
1 | sudo /usr/bin/mysql_install_db |
go into MySQL
1 2 3 4 5 6 7 8 9 | 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
1 | mysql> CREATE DATABASE `wordpress` CHARACTER SET utf8 COLLATE utf8_general_ci; |
Where:
db_name – data base name
check it out
1 | mysql> SHOW CREATE DATABASE `wordpress`; |
1 2 3 4 5 6 7 | 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
1 | 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
Viewed 1530 times by 731 viewers
Комментарии (2)
Other Links to this Post
RSS-лента комментариев к этой записи. TrackBack URI


By Stuart Dallas, 6 Июль 2009 @ 11:33
Re spawn-fcgi – it has been removed from the lighttpd distribution because it has been broken off as a separate project: http://redmine.lighttpd.net/projects/spawn-fcgi – use that, NOT an older version or you won’t benefit from future improvements.
By sven86, 30 Август 2009 @ 11:06
For those who got the «403 Forbidden» error after installing nginx even though you followed the steps correctly, here’s what could be the solution.
Check the errors.log file at /home/www/linuxspace.org/logs/ or wherever you store it. If you got something like this:
2009/08/29 20:41:42 [error] 7637#0: *1 «/home/www/linuxspace.org/public/index.html» is forbidden (13: Permission denied), client: 192.168.0.104, server: localhost, request: «GET / HTTP/1.1″, host: «mydomain.com»
then it probably has to do with the permissions on the public folder and the files within it. Do the following:
sudo chmod o+rx /home/www/linuxspace.org/public
sudo chmod o+r /home/www/linuxspace.org/public/*
(change /home/www/linuxspace.org/public to the path that you have it set up on nginx.conf)
It should take care of the problem.