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

I am trying to find recent documentation on replacing Apache / PHP with nginx and php-fpm for an AWS beanstalk application. However, the only thing I found is old when it comes to modifying hostmanager to do this, so this no longer applies.

I can work on this a bit, but I'm curious if anyone has done this recently, and what was their procedure?

+3


source to share


1 answer


I have tried too hard to find any tutorials or docs regarding installing NGINX on a resilient beanstalk. After walking around the world all day today, I think I figured out how to do it successfully.

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

1. Set up your EB environment and find one of the ec2 instances it uses. Right-click the instance and select Run Like This More. Proceed to the steps to start a new instance.

2. SSH into the newly started 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 running when the server boots:

chkconfig php-fpm on
chkconfig nginx on

      

4: Set the configuration for nginx. I 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 s 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 s 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 document root
        # concurs with nginx 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 and copy all of the above to /etc/nginx/nginx.conf

5: Go to the instance you got into, right click on it and select "Build Image" and it will create an ami for you. Then you can go into any EB application and go to the Config> Instances tab and change the AMI to the id you just created.

I had an environment running for a couple of hours in this configuration and it works well.

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

Edit: here are my sources of information:

+5


source







All Articles