Friday, August 5, 2011

Installing Nginx on Ubuntu (11.04)

I installed nginx on my Ubuntu (11.04), using Synaptic.

1. Resouces.
Nginx: http://wiki.nginx.org/
Sleekd: http://sleekd.com/general/configuring-nginx-and-unicorn/
Slicehost: http://articles.slicehost.com/nginx

2. Installation.
Search for nginx in Synaptic and install it.
It installs 3 packages, nginx, nginx-common, nginx-full.

3. Start the Nginx HTTP Server.
$ sudo /etc/init.d/nginx start      # start server
Starting nginx: nginx.
$ sudo /etc/init.d/nginx status     # check status
 * nginx is running

4. Test
http://localhost    # browser url
And the result is:













5. Check Installation

(1) Check the running processes.
$ ps aux | grep nginx
root      1253  0.0  0.0  10128   908 ?        Ss   19:43   0:00 nginx: master process /usr/sbin/nginx
www-data  1254  0.0  0.0  10280  1272 ?        S    19:43   0:00 nginx: worker process 
www-data  1255  0.0  0.0  10280  1272 ?        S    19:43   0:00 nginx: worker process
www-data  1257  0.0  0.0  10280  1272 ?        S    19:43   0:00 nginx: worker process
www-data  1258  0.0  0.0  10280  1272 ?        S    19:43   0:00 nginx: worker process
1001      2879  0.0  0.0   5128   856 pts/0    S+   21:52   0:00 grep --color=auto nginx
It looks like the installer created a new user www-data.

(2) Let's check this user.
$ finger www-data
Login: www-data          Name: www-data
Directory: /var/www      Shell: /bin/sh
Never logged in.
No mail.
No Plan.
It turned out that "the default web server user on Debian based systems is www-data" according to this site: slicehost. The installer also created a new group www-data, to which the user www-data belongs.
$ members www-data      # list all users who belong to group www-data.
www-data


(3) Get some more information about this installation with -V option.
$ nginx -V
nginx version: nginx/0.8.54
TLS SNI support enabled
configure arguments:
 --conf-path=/etc/nginx/nginx.conf                  # <==== Here is the path to nginx.conf.
 --error-log-path=/var/log/nginx/error.log
 --http-client-body-temp-path=/var/lib/nginx/body
 --http-fastcgi-temp-path=/var/lib/nginx/fastcgi
 --http-log-path=/var/log/nginx/access.log
 --http-proxy-temp-path=/var/lib/nginx/proxy
 --http-scgi-temp-path=/var/lib/nginx/scgi
 --http-uwsgi-temp-path=/var/lib/nginx/uwsgi
 --lock-path=/var/lock/nginx.lock
 --pid-path=/var/run/nginx.pid
 --with-debug
 --with-http_addition_module
 --with-http_dav_module
 --with-http_geoip_module
 --with-http_gzip_static_module
 --with-http_image_filter_module
 --with-http_realip_module
 --with-http_stub_status_module>
 --with-http_ssl_module
 --with-http_sub_module
 --with-http_xslt_module
 --with-ipv6
 --with-sha1=/usr/include/openssl
 --with-md5=/usr/include/openssl
 --with-mail
 --with-mail_ssl_module
 --add-module=/build/buildd/nginx-0.8.54/debian/modules/nginx-upstream-fair

6. Configuration Files.
Let's look at some configuration files.

(1) /etc/nginx/nginx.conf
user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events {
 worker_connections 768;
 # multi_accept on;
}

http {

 ##
 # Basic Settings
 ##

 sendfile on;
 tcp_nopush on;
 tcp_nodelay on;
 keepalive_timeout 65;
 types_hash_max_size 2048;
 # server_tokens off;

 # server_names_hash_bucket_size 64;
 # server_name_in_redirect off;

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

 ##
 # Logging Settings
 ##

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

 ##
 # Gzip Settings
 ##

 gzip on;
 gzip_disable "msie6";

 # gzip_vary on;
 # gzip_proxied any;
 # gzip_comp_level 6;
 # gzip_buffers 16 8k;
 # gzip_http_version 1.1;
 # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

 ##
 # Virtual Host Configs
 ##

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


#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
# server {
#  listen     localhost:110;
#  protocol   pop3;
#  proxy      on;
# }
# 
# server {
#  listen     localhost:143;
#  protocol   imap;
#  proxy      on;
# }
#}

(2) /etc/nginx/sites-available/default

# You may add here your
# server {
# ...
# }
# statements for each of your virtual hosts to this file

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

server {
 #listen   80; ## listen for ipv4; this line is default and implied
 #listen   [::]:80 default ipv6only=on; ## listen for ipv6

 root /usr/share/nginx/www;
 index index.html index.htm;

 # Make site accessible from http://localhost/
 server_name localhost;

 location / {
  # First attempt to serve request as file, then
  # as directory, then fall back to index.html
  try_files $uri $uri/ /index.html;
 }

 location /doc {
  root /usr/share;
  autoindex on;
  allow 127.0.0.1;
  deny all;
 }

 location /images {
  root /usr/share;
  autoindex off;
 }

 #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 /usr/share/nginx/www;
 #}

 # 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$ {
 # fastcgi_pass 127.0.0.1:9000;
 # fastcgi_index index.php;
 # 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;
# root html;
# index index.html index.htm;
#
# location / {
#  try_files $uri $uri/ /index.html;
# }
#}


# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
#
# root html;
# index index.html index.htm;
#
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
#
# ssl_session_timeout 5m;
#
# ssl_protocols SSLv3 TLSv1;
# ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
# ssl_prefer_server_ciphers on;
#
# location / {
#  try_files $uri $uri/ /index.html;
# }
#}

OK, according to this conf file, the root directory for the default site is /usr/share/nginx/www.
And its home page file is index.html. Let's make some change.
$ cd /usr/share/nginx/www
$ ls
50x.html  index.html
$ sudo gedit index.html
I made a change to the welcome text from "Welcome to nginx!" to "Welcome to My Nginx!" with blue color.
And here is the result.

2 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete