Skip to content

Instantly share code, notes, and snippets.

@johnathancroom
Created July 14, 2012 08:13
Show Gist options
  • Save johnathancroom/3110020 to your computer and use it in GitHub Desktop.
Save johnathancroom/3110020 to your computer and use it in GitHub Desktop.
EC2

###Init

  1. Spawn Ubuntu instance.
  2. Update aptitude with sudo apt-get update
  3. Install junk we want sudo apt-get install nginx spawn-fcgi php5 php5-cli php5-common php5-suhosin php5-cgi php-pear php5-mysql htop git
  4. Set root password sudo passwd root

###Setup PHP

  1. Change /etc/nginx/sites-available/default to:
server {
  listen   80;
  server_name  localhost;
  access_log  /var/log/nginx/localhost.access.log;
 
  ## Default location
  location / {
    root   /var/www;
    index  index.php;
  }
 
  ## Images and static content is treated different
  location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
    access_log        off;
    expires           30d;
    root /var/www;
  }
 
  ## Parse all .php file in the /var/www directory
  location ~ .php$ {
    fastcgi_split_path_info ^(.+\.php)(.*)$;
    fastcgi_pass   backend;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /var/www$fastcgi_script_name;
    include fastcgi_params;
    fastcgi_param  QUERY_STRING     $query_string;
    fastcgi_param  REQUEST_METHOD   $request_method;
    fastcgi_param  CONTENT_TYPE     $content_type;
    fastcgi_param  CONTENT_LENGTH   $content_length;
    fastcgi_intercept_errors        on;
    fastcgi_ignore_client_abort     off;
    fastcgi_connect_timeout 60;
    fastcgi_send_timeout 180;
    fastcgi_read_timeout 180;
    fastcgi_buffer_size 128k;
    fastcgi_buffers 4 256k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;
  }
 
  ## Disable viewing .htaccess & .htpassword
  location ~ /\.ht {
    deny  all;
  }
}
upstream backend {
  server 127.0.0.1:9000;
}
  1. sudo vi /usr/sbin/fastcgi-php with the contents:
#!/bin/sh
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -f /usr/bin/php5-cgi
  1. Permissions sudo chmod 755 /usr/sbin/fastcgi-php
  2. sudo vi /etc/init.d/init-fastcgi with the contents:
#!/bin/bash
PHP_SCRIPT=/usr/sbin/fastcgi-php
RETVAL=0
case "$1" in
    start)
      $PHP_SCRIPT
      RETVAL=$?
  ;;
    stop)
      killall -9 php
      RETVAL=$?
  ;;
    restart)
      killall -9 php
      $PHP_SCRIPT
      RETVAL=$?
  ;;
    *)
      echo "Usage: php-fastcgi {start|stop|restart}"
      exit 1
  ;;
esac
exit $RETVAL
  1. Give script execute permissions sudo chmod 755 /etc/init.d/init-fastcgi
  2. Start fastcgi /etc/init.d/init-fastcgi start
  3. Start it on boot sudo update-rc.d init-fastcgi defaults
  4. Restart nginx /etc/init.d/nginx restart

###Setup Ruby

  1. Go here

###Setting up domains

  1. cd /etc/nginx/sites-available
  2. Add file awesomecats.com with contents:
upstream awesomecats {
  server 127.0.0.1:3000;
}
server {
  listen 80;
  server_name awesomecats.com;

  access_log /var/www/awesomecats.com/logs/access.log;
  error_log /var/www/awesomecats.com/logs/error.log;

  root /var/www/awesomecats.com/public/;
  index index.html;

  location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    if (-f $request_filename/index.html) {
      rewrite (.*) $1/index.html break;
    }
    if (-f $request_filename.html) {
      rewrite (.*) $1.html break;
    }
    if (!-f $request_filename) {
      proxy_pass http://awesomecats;
      break;
    }
  }
}
  1. Symbolic link it ln -s /etc/nginx/sites-available/awesomecats.com /etc/nginx/sites-enabled/awesomecats.com
  2. Restart nginx /etc/init.d/nginx restart
@azazqadir
Copy link

Setting up a PHP stack server on AWS EC2 is quite difficult because of the complexities involved in the process. Managing EC2 is a challenge in itself. I always suggest my clients to avoid spending too much time or money on management of servers means less time and money for your core business operations. If there are better ways to PHP web hosting on AWS, then go for it.

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