###Init
- Spawn Ubuntu instance.
- Update aptitude with
sudo apt-get update
- 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
- Set root password
sudo passwd root
###Setup PHP
- 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;
}
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
- Permissions
sudo chmod 755 /usr/sbin/fastcgi-php
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
- Give script execute permissions
sudo chmod 755 /etc/init.d/init-fastcgi
- Start fastcgi
/etc/init.d/init-fastcgi start
- Start it on boot
sudo update-rc.d init-fastcgi defaults
- Restart nginx
/etc/init.d/nginx restart
###Setup Ruby
###Setting up domains
cd /etc/nginx/sites-available
- 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;
}
}
}
- Symbolic link it
ln -s /etc/nginx/sites-available/awesomecats.com /etc/nginx/sites-enabled/awesomecats.com
- Restart nginx
/etc/init.d/nginx restart
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.