Rick Baker
Rick Baker

Reputation: 903

Replacing Apache/PHP with nginx and php-fpm on AWS elastic beanstalk

I've been trying to find recent documentation on replacing Apache/PHP with nginx and php-fpm for an AWS beanstalk application. However, the only thing I've found is old where it refers to modifying the hostmanager to accomplish this, so this no longer applies.

I can hack my way through it with some effort, but I'm curious if anyone has done this recently and what their procedure was?

Upvotes: 3

Views: 4372

Answers (1)

Sneaksta
Sneaksta

Reputation: 1061

I too struggled to find any tutorials or documentation concerning the setup of NGINX on Elastic Beanstalk. After fiddling around all day today, I think I've figured out how to set it up successfully.

Here are the steps I took to get setup, listed in a (hopefully) concise manner:

1. Setup an EB environment, and find one of the ec2 instances used by it. Right click the instance and select "Launch more likes this". Continue through the steps to launch a new instance.

2. SSH into the newly launched instance and run the following commands (put it in a bash script if you like):

# Remove apache
yum remove httpd

# install nginx and other needed components
yum install \
php54.x86_64 \
php54-bcmath.x86_64 \
php54-cli.x86_64 \
php54-common.x86_64 \
php54-dba.x86_64 \
php54-devel.x86_64 \
php54-fpm.x86_64 \
php54-gd.x86_64 \
php54-intl.x86_64 \
php54-mbstring.x86_64 \
php54-mcrypt.x86_64 \
php54-pdo.x86_64 \
php54-pecl-apc.x86_64 \
php54-process.x86_64 \
php54-xml.x86_64

Then make sure php-fpm and nginx are started on server boot:

chkconfig php-fpm on
chkconfig nginx on

4: Setup the config for nginx. I've copied mine below, but yours might be slightly different depending on your setup:

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user  nginx;
worker_processes  4;

error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


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

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

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

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        server_name  localhost;
        root   /var/app/current/public_html;

        #charset koi8-r;

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

        location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
                # Some basic cache-control for static files to be sent to the browser
                expires max;
                add_header Pragma public;
                add_header Cache-Control "public, must-revalidate, proxy-revalidate";
        }


        # main php files
        location / {
            index  index.html index.htm;
            try_files $uri $uri/ /index.php?$query_string;
        }

        # redirect server error pages to the static page /40x.html
        #
        error_page  404              /404.html;
        location = /40x.html {
            root   /usr/share/nginx/html;
        }

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    try_files $uri $uri/ /index.php$is_args$args;
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(.*)$;
            root           /var/app/current/public_html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
            deny  all;
        }

        # make sure the hostmanager works (this is important for EB)
        location /_hostmanager/ {
            proxy_pass         http://127.0.0.1:8999/;
        }

    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}

Just go ahead and copy all of the above into /etc/nginx/nginx.conf

5: Go to the instance that you're ssh-ed into, right click on it and select 'Create Image' and it'll create an ami for you. You can then go into any EB application and go to the Configuration > instances tab and change the AMI to the ID of the ami you just created.

I've had an environment running for a couple of hours on this configuration, and it's working well.

I hope everything works for you as it did (finally) for me. :)

Edit: Here are my sources of info:

Upvotes: 5

Related Questions