Browse Articles in Meteor JS
 

Nginx 1.4.1 and Meteor.js equals loveIf you want to host Meteor JS applications, Apache’s lack of Websocket support, without having to patch, can get into the way. I will describe here how you can setup Nginx 1.4.1 with full Websocket support for all you Meteor JS application needs on Ubuntu 12.04.x.

Note: If you need to know all the other steps taken to set up your Meteor.js application on the server you can check out this article ( Deploying multiple vhost Meteor JS apps on Ubuntu 12.04 with MongoDB ) and just replace step number 6 with this article.

1. Installing the dependencies

On the command line become root.

sudo su

Now we will install the tools needed in Ubuntu 12.04 to compile software from source.

apt-get install build-essential

Now we have what we need to compile Nginx so lets move along.

2. Download Nginx 1.4.1

Now we will download the Nginx 1.4.1 package into our /usr/local/src directory and compile it.

cd /usr/local/src

mkdir nginx; cd nginx

Now download the Nginx 1.4.1 Stable package with wget.

wget http://nginx.org/download/nginx-1.4.1.tar.gz

Untar and enter the application source:

tar zxvf nginx-1.4.1.tar.gz

cd nginx-1.4.1

3. Compile Nginx 1.4.1

Now we need to run the configure script. We want to pass a few options to the configure script, like where we want to store the configuration and how we want the package distributed in our system. For a full list of options you have available you can run the following command:

./configure --help

Now enter the following command to run the configure script:

./configure --prefix=/usr/share/nginx \
    --sbin-path=/usr/sbin/nginx \
    --conf-path=/etc/nginx/nginx.conf \
    --pid-path=/var/run/nginx.pid \
    --lock-path=/var/lock/nginx.lock \
    --error-log-path=/var/log/nginx/error.log \
    --http-log-path=/var/log/nginx/access.log

The above script should be pretty self explanatory. We want to set our log location, lock location, and configuration location. If no errors appeared we can move on. If errors did occur than usually it is because of missing dependencies. Just note the errors and try to install what is missing with apt-get install.

Running the next two commands in one shot should compile Nginx 1.4.1 on Ubuntu 12.04 for our Meteor.js application’s disposal.

make && make install

At this point, we can move along and configure nginx to host our Meteor.js application.

4. Configure Nginx 1.4.1 to start at boot

Lets make sure we start Nginx when our server reboots first. We need to create a script in /etc/init.d/ so we can start and stop our Nginx server. Create a new file and enter the content.

nano /etc/init.d/nginx

Now paste the following:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/nginx
NAME=nginx
DESC=nginx
# Include nginx default if available
if [ -f /etc/default/nginx ]; then
  . /etc/default/nginx
fi
test -x $DAEMON || exit 0
set -e
. /lib/lsb/init-functions
test_nginx_config() {
    if $DAEMON -t $DAEMON_OPTS >/dev/null 2>&1; then
        return 0
    else
        $DAEMON -t $DAEMON_OPTS
        return $?
    fi
}
case "$1" in
    start)
        echo -n "Starting $DESC: "
        test_nginx_config
        # Check if the ULIMIT is set in /etc/default/nginx
        if [ -n "$ULIMIT" ]; then
            # Set the ulimits
            ulimit $ULIMIT
        fi
        start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \
            --exec $DAEMON -- $DAEMON_OPTS || true
        echo "$NAME."
        ;;
    stop)
        echo -n "Stopping $DESC: "
        start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \
            --exec $DAEMON || true
        echo "$NAME."
        ;;
    restart|force-reload)
        echo -n "Restarting $DESC: "
        start-stop-daemon --stop --quiet --pidfile \
            /var/run/$NAME.pid --exec $DAEMON || true
        sleep 1
        test_nginx_config
        # Check if the ULIMIT is set in /etc/default/nginx
        if [ -n "$ULIMIT" ]; then
            # Set the ulimits
            ulimit $ULIMIT
        fi
        start-stop-daemon --start --quiet --pidfile \
            /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
        echo "$NAME."
        ;;
    reload)
        echo -n "Reloading $DESC configuration: "
        test_nginx_config
        start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid \
            --exec $DAEMON || true
        echo "$NAME."
        ;;
    configtest|testconfig)
        echo -n "Testing $DESC configuration: "
        if test_nginx_config; then
            echo "$NAME."
        else
            exit $?
        fi
        ;;
    status)
        status_of_proc -p /var/run/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?
        ;;
    *)
        echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest}" >&2
        exit 1
        ;;
esac
exit 0

Save the script with pressing [CTRL] + [X] and [Y] for yes. Now give the script the right permissions.

chmod 755 /etc/init.d/nginx

To make the server start on reboot do the following:

nano /etc/default/nginx

Paste the below lines into the script.

# Note: You may want to look at the following page before setting the ULIMIT.
#  http://wiki.nginx.org/CoreModule#worker_rlimit_nofile
# Set the ulimit variable if you need defaults to change.
#  Example: ULIMIT="-n 4096"
#ULIMIT="-n 4096"

Save the script with pressing [CTRL] + [X] and [Y] for yes. Now the next command makes sure that Nginx start on a reboot:

update-rc.d nginx defaults

5. Setup a Nginx 1.4.1 virtual host (vhost) in Ubuntu 12.04

It is now time to setup a VirtualHost in /etc/nginx, but first we need to set up two directories and a small edit in the main Nginx config.

mkdir /etc/nginx/sites-available /etc/nginx/sites-enabled

Notice there is a space between the two directories above. mkdir allows us to create multiple directories when they are space separated. Now let us include the sites-enabled dir in our /etc/nginx/nginx.conf.

nano /etc/nginx/nginx.conf

In the http block right at the end before the block closes we need to add the following line:

include sites-enabled/*;

So now the file will look like:

... 

http {

    ...

    include sites-enabled/*;
}

The three dots is all the content we do not want to display here for clarity. Again, save the file with pressing [CTRL] + [X] and [Y] for yes.

Now all the files in sites-enabled will be included in the configuration of our Nginx Server on Ubuntu 12.04. The next step is to create a new file in sites-available which will later be symlinked to our sites-enabled directory.

nano /etc/nginx/sites-available/my-meteor-app.com

You can name the file whatever you want. Usually I will name mine the same as the domain they are being hosted on like chat.andrehonsberg.com or something similar.

Inside the file you just created we will add the following content. I will use my-meteor-app.com as my example domain name throughout the example. Again make sure to replace it with the actual data you need. Paste the following into the file and edit accordingly:

upstream my_meteor_app {
        server 127.0.0.1:58080;
}

server {
        listen 0.0.0.0:80;
        server_name my-meteor-app.com;
        access_log /var/log/nginx/my-meteor-app.access;

        location / {
                proxy_pass http://my_meteor_app/;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_set_header Host $http_host;

                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forward-Proto http;
                proxy_set_header X-Nginx-Proxy true;

                proxy_redirect off;
        }
}

Save the file again with [CTRL] + [X] and then [Y] for yes.

Now I will explain the different settings from above. The first part upstream tells the Nginx server where we want to proxy to. upstream my_meteor_app, my_meteor_app is the name we give to the upstream. This can again be what you want as long as you reference it on the proxy_pass option with the http:// prefixed and ending with /; to close it. Above you can see that our example Meteor app is running on port 58080. This high port is chosen because it can be opened by a normal system user. If the port would be 8080, we would then need root permissions. Running the app as root is not recommended for security reasons.

The next step we have to take is symlink the file to our sites-enabled directory. To achieve this change directories and execute the ln command.

cd /etc/nginx/sites-enabled

Now symlink with:

ln -s /etc/nginx/sites-available/my-meteor-app.com

Note: If you need to know how to get your Meteor application ready for this setup checkout this article ( http://www.andrehonsberg.com/article/deploy-meteorjs-vhosts-ubuntu1204-mongodb-apache-proxy ) .

6. Start, restart and stop Nginx on Ubuntu 12.04

Now you want to start or restart Nginx 1.4.1 with the following commands as root still:

/etc/init.d/nginx start

# or you can use the service command
service nginx start

# to restart use the following 2

/etc/init.d/nginx restart

# or
service nginx restart

You can also stop the Nginx server in Ubuntu 12.04 with the following commands:

/etc/init.d/nginx stop
# or
service nginx stop

7. Summary

Now you have a Nginx 1.4.1 Server on Ubuntu 12.04.x to host you Meteor JS apps. To host another app you just create another file in /etc/nginx/sites-available. In this file you will need to specify the upstream with a different port. This means run the other Meteor JS on another high port that is different from the one you already created. Also change the server_name key value to the different domain name. That is all there is to it.

Want to leave a Commnet?
kevohagan
Wednesday the 13th of November, 2013 at 16:31:03
Gravatr Image
Very nice tutorial Andre!

Thanks to your help i'm now running different meteor apps with nginx running on one server at digitalocean! :)

Just a small little detail, in this article you publish to port 58080, whereas on your other article you export your meteor app to 8080, some people might get confused with that. :)


Apart from that great job,


I would love to see more meteor.js articles from you!! :)


thanks a million ,


kev
Andre Honsberg
Wednesday the 13th of November, 2013 at 16:33:42
Gravatr Image
Thanks for the hint. I will update the article to be consistant.