Tuesday, July 30, 2013

Yii and AWS: Configure Yii on EC2 AMI

First you need to install essential modules on AMI. If you like to use Nginx, please take a look at tutorial here. If you like Apache more, please see here.
I myself likes to use Nginx. However, by default AWS Nginx config does not contain sites-avaliable and sites-enabled folder. I recommend using sites-enable and available structure to keep things organized.

Configure Nginx

First we make two directories within nginx config.

cd /etc/nginx/
sudo mkdir sites-available
sudo mkdir sites-enabled

then we need to config nginx.conf, we construct a new config file.

sudo vim nginx.temp

Paste the following code into the file without modification.Refer to Yii official website.

worker_processes 4;
pid /var/run/nginx.pid;

events {
 worker_connections 768;
 # multi_accept on;
}

http {

 ##
 # Basic Settings
 ##

 sendfile on;
 tcp_nopush on;
 tcp_nodelay on;
 keepalive_timeout 65;
 types_hash_max_size 2048;
 # server_tokens off;

 # server_names_hash_bucket_size 64;
 # server_name_in_redirect off;

 include /etc/nginx/mime.types;
 default_type application/octet-stream;

 ##
 # Logging Settings
 ##

 access_log /var/log/nginx/access.log;
 error_log /var/log/nginx/error.log;

 ##
 # Gzip Settings
 ##

 gzip on;
 gzip_disable "msie6";

 # gzip_vary on;
 # gzip_proxied any;
 # gzip_comp_level 6;
 # gzip_buffers 16 8k;
 # gzip_http_version 1.1;
 # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

 ##
 # nginx-naxsi config
 ##
 # Uncomment it if you installed nginx-naxsi
 ##

 #include /etc/nginx/naxsi_core.rules;

 ##
 # nginx-passenger config
 ##
 # Uncomment it if you installed nginx-passenger
 ##
 
 #passenger_root /usr;
 #passenger_ruby /usr/bin/ruby;

 ##
 # Virtual Host Configs
 ##

 include /etc/nginx/conf.d/*.conf;
 include /etc/nginx/sites-enabled/*;
}


#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
# server {
#  listen     localhost:110;
#  protocol   pop3;
#  proxy      on;
# }
# 
# server {
#  listen     localhost:143;
#  protocol   imap;
#  proxy      on;
# }
#}

then we replace the config file

sudo mv nginx.temp nginx.conf

after that we edit a nginx server file, replace the with your own domain

sudo vim sites-avaliable/

copy and edit the server info to the file, replace the domain with your own domain info and the host_path and document to be the root of your hosted files.

server {
    set $host_path "/var/www/html/";
    set $document "/var/www/html/";
    server_name  "domain";

    #access_log  /var/log/nginx/access.log  main;

    root   $host_path;
    set $yii_bootstrap "index.php";
    charset utf-8;

    location / {
        index  index.html $yii_bootstrap;
        try_files $uri $uri/ /$yii_bootstrap?$args;
    }

    location ~ ^/(protected|framework|themes/\w+/views) {
        deny  all;
    }

    #avoid processing of calls to unexisting static files by yii
    location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
        try_files $uri =404;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php {
        fastcgi_split_path_info  ^(.+\.php)(.*)$;

        #let yii catch the calls to unexising PHP files
        set $fsn /$yii_bootstrap;
        if (-f $document$fastcgi_script_name){
            set $fsn $fastcgi_script_name;
        }

        fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
        include fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME  $document$fsn;

        #PATH_INFO and PATH_TRANSLATED can be omitted, but RFC 3875 specifies them for CGI
        fastcgi_param  PATH_INFO        $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED  $document$fsn;
    }

    location ~ /\.ht {
        deny  all;
    }
}


make a symbolic link from sites-enabled to sites-available, so even the file is deleted in sites-enabled, the file in sites-available is still there.

sudo ln -s sites-available/domain sites-enabled/domain

Make sure that you session was started automatically in /etc/php.ini config "session.auto_start = 1" or it was started manually by session_start() or Yii similar function

$session=new CHttpSession;
$session->open();

And check your session status by session_status() function. restart nginx and the server, everything will be fine.

11 comments:

  1. I would recommend you to use Cloudways Yii hosting platform for deploying your yii app on Aws cloud server. It is easy and quick.

    ReplyDelete
  2. Very helpful blog it was a pleasure reading your blog would love to read it more AWS Training in India | AWS Training in Bangalore

    ReplyDelete
  3. Thanks for sharing this worthy article with us. Its very useful to learn and understand easily with the help of your guidelines. The data you have provided in this blog is very effective for AWS Learner to improve their productivity.
    AWS Training In Marathahalli

    ReplyDelete
  4. A big thank you for sharing such a valuable blog, I found this blog really useful. As a contribution to your blog postThis was a wondereful post being shared. Your story always bring hope and new energy.I gained many unknown information, the way you have clearly explained is really fantastic.
    Java training in Chennai

    Java Online training in Chennai

    Java Course in Chennai

    Best JAVA Training Institutes in Chennai

    Java training in Bangalore

    Java training in Hyderabad

    Java Training in Coimbatore

    Java Training

    Java Online Training

    ReplyDelete
  5. Very nice post here thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's

    IELTS Coaching in chennai

    German Classes in Chennai

    GRE Coaching Classes in Chennai

    TOEFL Coaching in Chennai

    spoken english classes in chennai | Communication training


    ReplyDelete