Skip to content

Instantly share code, notes, and snippets.

@ultim8k
Last active June 28, 2018 10:53
Show Gist options
  • Save ultim8k/ee3305fc7df6b880978c2e91f3c19311 to your computer and use it in GitHub Desktop.
Save ultim8k/ee3305fc7df6b880978c2e91f3c19311 to your computer and use it in GitHub Desktop.
Setting up local development environment for our app

Setting up local dev env for our app

Setting up the domain

Our domain is foobar.com so the one we want for local development is dev.foobar.com. We need to make sure that our browser will redirect all the requests to the local machine instead of asking a DNS.

sudo vim /etc/hosts

Then we need to add our dns entry at the end. πŸ”§

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost

0.0.0.0         dev.foobar.com

Setting up the web server

After pointing the domain to local machine we need to setup a local web server that will proxy all the requests to our domain to our app. We are going to nginx for this.

Assuming we already have homebrew installed, simply run:

brew install nginx

Now go to our projects' directory and create a directory to host all our configuration files.

cd /Users/USER_NAME/PROJECTS/PATH
mkdir nginx_local_confs

And then we create the configuration file in that directory.

cd nginx_local_confs
vim foobar.com.conf

Our local app is running on port 3000 so the configuration will be the following. πŸ”§

# nginx local configuration

upstream awesomeAppServer {
    server 127.0.0.1:3000;
}

server {
    listen 80;
    server_name dev.foobar.com;

    location / {
        proxy_pass  http://awesomeAppServer;
    }
}

Next we need to edit our server main configuration. We assume that homebrew installed nginx in /usr/local. By convention UNIX configuration files are located in /etc/, /usr/etc/, /usr/local/etc/ and so on.

sudo vim /usr/local/etc/nginx/nginx.conf

And include the individual configuration files for our apps. πŸ”§

worker_processes  1;

events {
    worker_connections  1024;
}

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

    sendfile on;

    keepalive_timeout 65;

    include servers/*;
    include /Users/USER_NAME/PROJECTS/PATH/nginx_local_confs/*.conf;
}

Finally we need to start our server:

sudo nginx

Done! πŸš€

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment