Skip to content

Instantly share code, notes, and snippets.

@wosephjeber
Last active September 22, 2015 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wosephjeber/0933703590edd6bfa080 to your computer and use it in GitHub Desktop.
Save wosephjeber/0933703590edd6bfa080 to your computer and use it in GitHub Desktop.
Opportunity dev environment setup

##Apache setup

Apache is included with OSX. The configuration files are found at /etc/apache2/. We'll be editing two files: /etc/apache2/httpd.conf and /etc/apache2/extras/httpd-vhosts.conf.

####httpd.conf

First, we need to enable some modules. Uncomment (remove the #) the lines that contain #LoadModule php5_module libexec/apache2/libphp5.so and LoadModule rewrite_module libexec/apache2/mod_rewrite.so.

Just a bit below that line, you'll see this:

User _www
Group _www

Change the user to your username. For example, on my machine it reads User jweber

The default document root for Apache is /Library/WebServer/Documents/. If you'd like to serve pages from a different directory on your machine, look for the following block:

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/Library/WebServer/Documents"

Change the DocumentRoot to whatever directory you're using. You'll also need to find the following block a little farther down the file...

#
# This should be changed to whatever you set DocumentRoot to.
#
<Directory "/Library/WebServer/Documents">

...and change the path there as well. In that code block that <Directory "/Libary/WebServer/Documents"> sets off, you'll find this block:

  #
  # AllowOverride controls what directives may be placed in .htaccess files.
  # It can be "All", "None", or any combination of the keywords:
  #   Options FileInfo AuthConfig Limit
  #
  AllowOverride None

Change the last line to AllowOverride All.

Finally, toward the bottom of the file you'll find this:

# Virtual hosts
#Include /private/etc/apache2/extra/httpd-vhosts.conf

Uncomment that last line to include vhosts.

####httpd-vhosts.conf

Virtual hosts allow us to host multiple domain names on one server. There are two ways to go about using vhosts for local development in Apache. The first, and simplest, is to use IP-based vhosts and serve each site through the same IP address (in this case we'll use localhost, which is the same as 127.0.0.1) with a different port number for each site. For example, on my machine I have set up the url http://localhost:8881 to serve up the local copy of Opportunity's 2013 Annual Report. Going to http://localhost:8882 serves up the local Brand Guidelines microsite. The second way to go about this is to use name-based vhosts to serve each site through the same port (port 80 is the default). For example, you could set up http://annualreport.dev to serve up the Annual Report, and http://brandguidelines.dev to serve up the Brand Guidelines. Using name-based vhosts adds an additional step to configuring a new site, but makes your dev urls easier to remember (and cooler).

#####IP-based vhosts

Edit httpd-vhosts.conf. You should see some example configurations already in place. Use the following as an example to add your first site:

<VirtualHost *:8881>
    ServerAdmin jweber@opportunity.org
    DocumentRoot "/Library/WebServer/Documents/Annual-Report-2013"
    ServerName localhost
</VirtualHost>

You'll also need to tell Apache to listen on that port. Add the following to the bottom of the file (it can technically go anywhere in the file, or in httpd.conf, but the bottom of the vhosts configuration file is convenient):

Listen 8881

Repeat this for each additional dev site you want to add. You'll need to add Listen [port number] for each vhost on a separate line.

Anytime you make a change to the Apache configuration, you'll need to restart Apache for the changes to take effect. Do this with sudo apachectl restart.

####Name-based vhosts

Similar to IP-based vhosts, but with a few changes and an additional step.

Use port 80 and set the ServerName to the domain name you want to use. You'll want to set a ServerAlias for any other urls, e.g. www.[domain name]. You can add as many ServerAlias's as you'd like. Use the following example:

<VirtualHost *:80>
    ServerAdmin jweber@opportunity.org
    DocumentRoot "/Library/WebServer/Documents/Annual-Report-2013"
    ServerName annualreport.dev
    ServerAlias www.annualreport.dev
</VirtualHost>

Next, you'll need to edit the following file: /etc/hosts. By default, it should contain something like the following:

##
# 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 
fe80::1%lo0	    localhost

This file basically acts as the DNS on your machine. Each entry matches a host name with an IP address. Add a new record for each named vhost. For example, to add the Annual Report vhost from above, the hosts file should look like this:

##
# 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 
fe80::1%lo0	    localhost
127.0.0.1       annualreport.dev

Restart Apache and you should be good to go.

#Local dev environment setup

Here are the steps to follow for setting up a local dev environment for Opportunity:

##General stuff

You'll want the following applications:

  • An IDE such as Sublime, Coda, or Atom. I recommend Atom, as it's free and open source and has a strong community around it: https://atom.io
  • Sequel Pro for working with local and remote MySQL databases: http://www.sequelpro.com
  • An FTP client. I like Transmit by Panic: http://panic.com/transmit. Cyberduck is a free alternative.
  • GitHub for Mac. Not required, as you can do everything via command line, but I find the GUI really useful, particularly for previewing changed files and staging files to commit.

##Getting ready for Rails Follow these steps to get OptINnow up and running locally.

###Overview

  1. Install XCode Command Line Tools
  2. Install homebrew
  3. Install git
  4. Install rbenv
  5. Install ruby
  6. Install bundler
  7. Install imagemagick
  8. Clone git repos
  9. Run bundle install in repo directory
  10. Install MySQL
  11. Import dev copy of database
  12. Update config files
  13. Start Rails server

###More specifics

####1. Install XCode Command Line Tools Go to the Mac App Store and install XCode (it's free).

####2. Install homebrew

If it asks us to install Command Line Tools, say yes.

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Run brew doctor to make sure there aren't any issues. Run brew update before using brew to make sure it's up to date (it's updated frequently).

####3. Install git

brew install git

####4. Install rbenv

rbenv is our Ruby version manager. It's an alternative to rvm, which is also good.

Run brew install rbenv ruby-build. This will install rbenv.

Then run $ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile and echo 'eval "$(rbenv init -)"' >> ~/.bash_profile. This adds the rbenv commands to our path. You'll need to restart the terminal window, or open a new terminal window for the change to take place.

Any time you install a new ruby version or a new ruby gem, you'll need to run rbenv rehash to have access to those tools in the command line.

####5. Install Ruby

We're currently using Ruby v1.9.3-p448 (version 1.9.3, patch 448).

Install Ruby with rbenv install 1.9.3-p448. Set that as the global ruby version with rbenv global 1.9.3-p448. You can always overwrite this on a per-project basis with rbenv local [ruby version] in the project directory.

####6. Install bundler

Bundler installs and managers our projects' gem dependencies. http://bundler.io/

gem install bundler

####7. Install imagemagick

Use Homebrew to install imagemagick. Remember, it's good practice to run brew update and brew doctor before installing packages.

brew install imagemagick

####8. Clone git repos

Use the command line or GitHub app and clone the repos to your machine. You can put the Rails apps wherever you want.

####9. Run bundle install

cd into the app directory and run bundle install. This may take a while, especially for installing nokogiri. I had a number of issues installing nokogiri, so be prepared to get your Google on.

####10. Install MySQL

brew install mysql

To start MySQL server: sudo mysql.server start. If you need to restart or stop the server for some reason, use sudo mysql.server restart or sudo mysql.server stop, respectively. MySQL doesn't start up automatically, so you'll need to start it manually after installing or booting up you computer.

####11. Import dev copy of database

Get a dump of the OptINnow database. Use Sequel Pro to import the database to localhost.

####12. Update config files

Update the necessary config files to run the app locally, but be sure to never commit them. Ask the senior dev to walk you through it. Most important is config/database.yml. I keep copies of these changed files on my computer, as they have a tendency to get overwritten when you're branching the repo and pulling down changes.

####13. Start Rails server

From the app directory, run rails s. You should be up and running at http://localhost:3000.

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