To Install Nginx
sudo apt-get install nginx
Setting up Virtual Hosts
Nginx, like Apache, supports name-based virtual hosting, where the sites are hosted according to their (sub)domain name.
Setting up a Standard Virtual Host
The default nginx config works well for a standard Virtual Host.
Lets take a look.
server { #replace <your_ip_here> with your ip address. listen <your_ip_here>:80; ## listen for ipv4; this line is default and implied #listen [::]:80 default ipv6only=on; ## listen for ipv6 #location of your files root /srv; index index.html index.htm; # The name of your domain (virtual hosts) 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 /index.php; # Uncomment to enable naxsi on this location #include /etc/nginx/naxsi.rules; #autoindex on; } # Only for nginx-naxsi : process denied requests #location /RequestDenied { # # For example, return an error code # return 418; #} #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; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # fastcgi_split_path_info ^(.+\.php)(/.+)$; # # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini # # # With php5-cgi alone: # fastcgi_pass 127.0.0.1:9000; # With php5-fpm: # fastcgi_pass unix:/var/run/php5-fpm.sock; # 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; } }
Following the commented instructions above; setting the ip address, the default directory, and your domain, you can create your own virtual host.
Afterwords, simply save the file in
/etc/nginx/sites-enabled
and restart nginx with
sudo /etc/init.d/nginx restart
Setting up a Virtual Host with SSL
Setting up a passthrough Virtual Host
Setting up Nginx Backends
Nginx with PHP
Nginx uses a fastcgi backend to communicate with other servers. Therefore, we install the php5 FPM server for nginx to pass php files onto.
sudo apt-get install php5-fpm
The default nginx configuration that comes with Ubuntu already contains the configuration for php-fpm. Simply remove the comments on the php block so that
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # fastcgi_split_path_info ^(.+\.php)(/.+)$; # # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini # # # With php5-cgi alone: # fastcgi_pass 127.0.0.1:9000; # With php5-fpm: # fastcgi_pass unix:/var/run/php5-fpm.sock; # fastcgi_index index.php; # include fastcgi_params; #}
looks like
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; # # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini # # # With php5-cgi alone: # fastcgi_pass 127.0.0.1:9000; # With php5-fpm: fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; #}
Afterwords, restart nginx
sudo service nginx restart
Nginx with Ruby (passenger)