Run Ruby on Rails App for the first time only with nginx

So I’m just lazy, I know there is better way to serve Ruby on Rails with unicorn or uWSGI, but I will try later.

This time I just want a have a quick look how’s the RoR running on the browser only with nginx, just a quick guide, no bullshit. so currently I have rails installed on my ubuntu machine 14.04 with mysql database configured and nginx. So make sure you have those things installed properly to follow this guide.

I usually create my local domain first to navigate it easier:

$ sudo echo -e "127.0.0.1\tmyapp.local" >> /etc/hosts

Create the nginx config for your web app :

$ sudo vim /etc/nginx/conf.d/myapp.conf

very simple myapp.conf looks like:

server {
 listen 80;
 server_name www.myapp.local myapp.local;
 charset utf-8;
 access_log /var/log/nginx/myapp.access.log;
 error_log /var/log/nginx/myapp.error.log;
 location / {
 proxy_pass http://127.0.0.1:3000;
 }
}

Check nginx config and restart

$ sudo nginx -t
$ sudo service nginx restart

Run the rails (I told you, I’m just lazy) :

$ cd /myapp
$ rails server &

And now, Rails is running with port 3000 which nginx already addressed to myapp.local domain. Open up the browser and navigate to http://myapp.local


Setup docker + jenkins + nginx

Let’s setup jenkins with docker with local url : jenkins.local

First, add local url to /etc/hosts

$ sudo echo -e "127.0.0.1\tjenkins.local" >> /etc/hosts

Install docker and pull newest jenkins image

$ sudo apt-get install docker.io
$ sudo docker pull jenkins
$ sudo docker run -itd --name jenkins --publish 8080:8080 --publish 50000:50000 jenkins

Setup nginx

$ sudo vim /etc/nginx/conf.d/jenkins.conf
server {
  listen 80;
  server_name jenkins.local;
  charset utf-8;
  gzip_vary on;
  access_log /var/log/nginx/jenkins.access.log;
  error_log /var/log/nginx/jenkins.error.log;
  location / {
    proxy_pass http://127.0.0.1:8080;
  }
}

Save config and restart nginx

$ sudo service nginx restart

Open browser, and navigate to http://jenkins.local


Setup nginx, php, wordpress in ubuntu

So after all these years I’ve had using apache for run something that runs with php, but since I know about nginx, I’ve been using this webserver instead of apache.

This wordpress blog is running with nginx and php-fpm, because not like apache that you can run anything about php without install the additional package. With nginx, you’ll need php-fpm to run php behind nginx.

This is the steps that I always use for make my php run behind nginx, especially when I setup this wordpress blog. Since I’m using mysql for database, you might want to install php package for mysql as well.

apt-get install php5-common php5-fpm php5-mysql

create nginx configuration for php website

vim /etc/nginx/conf.d/myweb.conf

myweb.conf

server {
    listen 80;
    server_name www.pulpn.com pulpn.com;
    charset utf-8;
    gzip_vary on;
    access_log /var/log/nginx/myweb.access.log;
    error_log /var/log/nginx/myweb.error.log;
    add_header 'Access-Control-Allow-Origin' '*' always;
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
    location / {
        root /var/www/pulpn;
        index index.php;
        try_files $uri $uri/ /index.php?q=$uri&$args;
        location ~* \.php {
            try_files $uri =404;
            include fastcgi_params;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_pass  unix:/var/run/php5-fpm.sock;
        }
    }
}

Restart your nginx :

service nginx restart

Or

/etc/init.d/nginx restart