Installing nginx on Debian
DRAFT …
Find latest version of nginx
http://sysoev.ru/en/ http://wiki.codemongers.com/NginxInstallOptions
Install a compiler otherwise: ./configure: error: C compiler gcc is not found
The command:
1 sudo apt-get install build-essential
Install pre-requisites otherwise you’ll get:
Configuration summary + threads are not used + PCRE library is not found + OpenSSL library is not found + md5 library is not used + sha1 library is not used + zlib library is not found
The command:
1 sudo apt-get install libpcre3 libpcre3-dev libpcrecpp0 libssl-dev zlib1g-dev
Much better:
Configuration summary + threads are not used + using system PCRE library + using system OpenSSL library + md5 library is not used + sha1 library is not used + using system zlib library
Compile and install nginx
1 $ ./configure \ 2 --sbin-path=/usr/local/sbin \ 3 --conf-path=/etc/nginx/nginx.conf \ 4 --pid-path=/var/run/nginx.pid \ 5 --error-log-path=/var/log/nginx/error.log \ 6 --http-log-path=/var/log/nginx/access.log \ 7 --with-http_ssl_module \ 8 --http-client-body-temp-path=/tmp/nginx_client \ 9 --http-proxy-temp-path=/tmp/nginx_proxy \ 10 --http-fastcgi-temp-path=/tmp/nginx_fastcgi 11 $ make 12 $ sudo make install
Run the install script
1 cd /usr/local/src 2 3 wget http://sysoev.ru/nginx/nginx-0.5.35.tar.gz 4 5 tar zxvf nginx-0.5.35 6 7 cd nginx-0.5.35
Create an nginx user and group
1 $ useradd -g www-data -d /var/www nginx
Create the web server directory
1 mkdir /var/www 2 chown root.www-data /var/www 3 chmod ug=rwx,o= /var/www
Test configuration
1 nginx -t 2 2008/03/09 20:51:05 [info] 5034#0: the configuration file /etc/nginx/nginx.conf syntax is ok 3 2008/03/09 20:51:05 [info] 5034#0: the configuration file /etc/nginx/nginx.conf was tested successfully
Start nginx
1 nginx
Generate a 56-bit DES encrypted (htpasswd) password with Ruby
Run the following in an irb console to generate a 56-bit DES encrypted password:
1 "password".crypt("salt")
The password can be used in an Apache or Nginx htpasswd file to enable basic authentication.
The generated password can also be used in other Unix password files.
Password protecting a folder/resource with Nginx
First add the following to your Nginx configuration file:
1 location / { 2 auth_basic "Restricted"; 3 auth_basic_user_file /etc/nginx/htpasswd; 4 }
Then create the htpasswd file:
1 # this be passwords 2 thisbetheusername:thisbeencryptedpass:yercomment
To generate a htpasswd password without installing Apache you can use the following Perl or Ruby code:
Perl
1 perl -le 'print crypt("password", "salt")'
Ruby (run in irb)
1 "password".crypt("salt")
The crypt() method uses 56-bit DES encryption, which is used in /etc/passwd and htpasswd.
Check if a file or directory exists with bash
This script tests if nginx exists and is executable. The script prints a warning and exits, if nginx doesn’t exists or isn’t executable:
1 DAEMON=/usr/local/sbin/nbinx 2 if [ ! -x $DAEMON ] 3 then 4 echo "Couldn't find $DAEMON. Please set path to DAEMON." 5 exit 0 6 fi
See man test for more information on how to use the test command.
Change nginx configuration on the fly
First make your changes to nginx.conf.
Then run the following command to test the new configuration:
1 # nginx -t -c /etc/nginx/nginx.conf 2 2007/10/18 20:55:07 [info] 3125#0: the configuration file /etc/nginx/nginx.conf syntax is ok 3 2007/10/18 20:55:07 [info] 3125#0: the configuration file /etc/nginx/nginx.conf was tested successfully
Next, look for the process id of the master nginx process:
1 # ps -ef|grep nginx 2 root 1911 1 0 18:00 ? 00:00:00 nginx: master process /usr/sbin/nginx 3 www-data 1912 1911 0 18:00 ? 00:00:00 nginx: worker process
Lastly, tell nginx to reload the configuration and restart the worker processes:
1 # kill -HUP 1911