Nginx on Linux VPS - Newbie Freindy, the Complete Guide

Status
Not open for further replies.

Freddy63

Active Member
138
2010
51
0
Hey guys,

This is my first tutorial. I was just browsing ‘Tutorials and Guides’ section and noticed few Nginx related tutorials. But none of them were complete tutorials. I mean installing Nginx is just a piece of big pie. You need to prepare your Linux for it. And after installation, what should you do? Can you install a WordPress or PHP base applications? Well, you can try your luck.

So I don’t have time to write all this. I have it covered on my blog ‘Step-by-Step Guide to WordPress on Nginx Server’. What I’m going to do here is, tell you the proper way of installing latest version of Nginx on an Ubuntu VPS. All additional details and background can be found at above mentioned post. And remember this is just a one part of many.

I need latest version of Nignx. So I won’t mess with integrated packages. I’ll install it from source. So we need to collect some dependency files. Below code will do it,

Code:
sudo aptitude -y install libpcre3 libpcre3-dev libpcrecpp0 libssl-dev zlib1g-dev
We need a folder inside our Ubuntu box to download installation files. Let’s create it,

Code:
mkdir ~/sources
cd ~/sources/
We’ll use ‘wget’ to download latest release or Nginx from official site. As for today, latest version is 1.2.0. To make sure you get the latest version, go to this page and find out if it’s still the up to date version. Change the version number accordingly on below code if it’s deferent from official site. Following code will download it.

Code:
wget http://nginx.org/download/nginx-1.2.0.tar.gz
Extract it,
Code:
tar -zxvf nginx-1.0.12.tar.gz
Navigate to extracted folder,
Code:
cd nginx-1.0.12
Now configure it. Note that code at the end. ‘--with-http_ssl_module’ it’s for SSL support. You probably don’t need it unless you have a SSL license. Just remove it.
Code:
./configure --sbin-path=/usr/local/sbin --with-http_ssl_module
Below command will install Nginx on your Ubuntu box.
Code:
make
sudo make install
Now fire it up,
Code:
sudo /usr/local/sbin/nginx
That’s it. Open your ip in browser and it should say ‘Welcome to nginx!’
That’s it right? No not exactly.
Nginx won’t auto start on reboots. So let’s make it happen.
First kill Nginx,
Code:
sudo kill `cat /usr/local/nginx/logs/nginx.pid`
We’ll create a script to do it. So create a new file,
Code:
sudo nano /etc/init.d/nginx
Paste this lot to new file,
Code:
#! /bin/sh

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/sbin/nginx
NAME=nginx
DESC=nginx

test -x $DAEMON || exit 0

# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
        . /etc/default/nginx
fi

set -e

case "$1" in
  start)
        echo -n "Starting $DESC: "
        start-stop-daemon --start --quiet --pidfile /usr/local/nginx/logs/$NAME.pid
                --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
        ;;
  stop)
        echo -n "Stopping $DESC: "
        start-stop-daemon --stop --quiet --pidfile /usr/local/nginx/logs/$NAME.pid
                --exec $DAEMON
        echo "$NAME."
        ;;

  restart|force-reload)
        echo -n "Restarting $DESC: "
        start-stop-daemon --stop --quiet --pidfile
                /usr/local/nginx/logs/$NAME.pid --exec $DAEMON
        sleep 1
        start-stop-daemon --start --quiet --pidfile
                /usr/local/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
        ;;
  reload)
      echo -n "Reloading $DESC configuration: "
      start-stop-daemon --stop --signal HUP --quiet --pidfile /usr/local/nginx/logs/$NAME.pid
          --exec $DAEMON
      echo "$NAME."
      ;;
  *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
        exit 1
        ;;
esac

exit 0
Give permissions to it,
Code:
sudo chmod +x /etc/init.d/nginx
sudo /usr/sbin/update-rc.d -f nginx defaults
Let’s tell Nginx how it should be working on our box. Open Nginx configuration file,
Code:
sudo nano /usr/local/nginx/conf/nginx.conf
Delete everything on that file and paste following,

Code:
user www-data www-data;
worker_processes  4;

events {
    worker_connections  1024;
}

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

    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     off;
    keepalive_timeout  5;

    gzip  on;
    gzip_comp_level 2;
    gzip_proxied any;
    gzip_types      text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    include /usr/local/nginx/sites-enabled/*;
}
Save and close.
Let’s create Virtual Host File Structure & Symlinks,
We need 2 new folders
Code:
sudo mkdir /usr/local/nginx/sites-available
sudo mkdir /usr/local/nginx/sites-enabled
Create default default vhost file,
Code:
sudo nano /usr/local/nginx/sites-available/default
And paste default rules into it,
Code:
server  {
            listen       80;
            server_name  localhost;

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

            # redirect server error pages to the static page /50x.html
            error_page   500 502 503 504  /50x.html;
            location = /50x.html
                       {
                        root   html;
                       }
        }
Enable vhost
Code:
sudo ln -s /usr/local/nginx/sites-available/default /usr/local/nginx/sites-enabled/default
Restart Nginx
Code:
sudo /etc/init.d/nginx start
Check your ip again. It should still say ‘Welcome to Nginx!’

That’s it! Nginx is now working properly. Remember this is 5th post of my Nginx series. All other tutorials are available at my blog.
Source - Install and Configure Nginx Web Server on Ubuntu VPS
 
8 comments
hmmm

I can't see any difference from other nginx tutorials except the version you downloaded is the newest one.

and of course all tutorials don't mention how to setup nginx to deal with php files through 3rd party fastcgi application.
 
Status
Not open for further replies.
Back
Top