Giter Club home page Giter Club logo

beanstalk-nginx-php-fpm's Introduction

Replace Apache with Nginx and PHP-FPM on AWS Beanstalk

If you find yourself wanting to use Nginx and PHP-FPM instead of apache for your AWS Beanstalk PHP App, well good for you!

This is a sample deployment with all of the .ebextensions to do so without having to make a custom AMI

What's accomplished here?

  1. Install nginx
  2. Stop apache and keep it from starting up again
  3. Make sure nginx and phpfpm start on reboot
  4. Make sure environment variables are updated on both a configuration and app deployment

How do I use it?

You can pull down this repo, zip up the contents, making sure you include the .ebextensions directory and deploy to your beanlstalk application. Once the deployment has finished you should be able to hit your beanstalk ip and get the hello world response.

Environment variables

Environment variables are set in the /etc/nginx/fastcgi_params_env file and you can include them in your server block. You'll see an example of this in the default. This file will get updated with any environment variable changes on both application and config deployments.

You can get environment variables in your php script with:

getenv('ENV_VARIABLE_NAME)

Nginx and PHPFPM config changes

You'll find the base config files within the .ebextensions directory. You can modify as needed and they will take effect when you deploy.

Why not create a custom AMI?

You can go this route, but then in a few months when you want to use the latest version of amazon linux you'll need to completely recreate your custom AMI all over again. That gets to be a pain over the course of time. So, why not just stick with the default AMI and handle all of your setup via .ebextensions?

I don't understand what's happening when I deploy?

Take a look at the .ebextensions/01_setup.config file. I've commented each step. Things in the "commands" section happen early in the deployment process before your actual code is setup.

Why not just get rid of apache completely?

This ends up being a complete pain. Beanstalk has many commands baked in with apache, so it just gets troublesome to get them all. So instead we keep apache in place but just tell Elasticbeanstalk to disable it.

beanstalk-nginx-php-fpm's People

Contributors

gr8karma avatar ricktbaker avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

beanstalk-nginx-php-fpm's Issues

No such file or directory

I'm trying to send the code to the elastic beanstalk. But I'm getting the following error:

[Instance: i-070f6ba774e338d74] Command failed on instance. Return code: 1 Output: No such file or directory - /opt/elasticbeanstalk/hooks/appdeploy/post/98_setupvars.sh. For more detail, check /var/log/eb-activity.log using console or EB CLI.

This code worked perfectly until last week, and today started to give this problem and I can not deploy a new version to the server.

part of eb-activity.log

[2017-10-21T20:42:25.439Z] INFO  [4388]  - [Application update 2.0.5-8@52/AppDeployStage1/AppDeployEnactHook/99_reload_app_server.sh] : Starting activity...
[2017-10-21T20:42:25.496Z] INFO  [4388]  - [Application update 2.0.5-8@52/AppDeployStage1/AppDeployEnactHook/99_reload_app_server.sh] : Completed activity. Result:
  Equivalent Upstart operations: start httpd, stop httpd, restart httpd, status httpd
  Gracefully restarting httpd
  Stopping httpd
  stop: Unknown instance: 
  Starting httpd
  httpd start/running, process 4814
  [OK]
[2017-10-21T20:42:25.496Z] INFO  [4388]  - [Application update 2.0.5-8@52/AppDeployStage1/AppDeployEnactHook] : Completed activity. Result:
  Successfully execute hooks in directory /opt/elasticbeanstalk/hooks/appdeploy/enact.
[2017-10-21T20:42:25.496Z] INFO  [4388]  - [Application update 2.0.5-8@52/AppDeployStage1/AppDeployPostHook] : Starting activity...
[2017-10-21T20:42:25.497Z] INFO  [4388]  - [Application update 2.0.5-8@52/AppDeployStage1/AppDeployPostHook/01_monitor_httpd_pid.sh] : Starting activity...
[2017-10-21T20:42:25.635Z] INFO  [4388]  - [Application update 2.0.5-8@52/AppDeployStage1/AppDeployPostHook/01_monitor_httpd_pid.sh] : Completed activity. Result:
  + chmod 0755 /var/run/httpd
  + /opt/elasticbeanstalk/bin/healthd-track-pidfile --proxy nginx
[2017-10-21T20:42:25.635Z] INFO  [4388]  - [Application update 2.0.5-8@52/AppDeployStage1/AppDeployPostHook/98_setupvars.sh] : Starting activity...
[2017-10-21T20:42:25.638Z] INFO  [4388]  - [Application update 2.0.5-8@52/AppDeployStage1/AppDeployPostHook/98_setupvars.sh] : Activity has unexpected exception, because: No such file or directory - /opt/elasticbeanstalk/hooks/appdeploy/post/98_setupvars.sh (Errno::ENOENT)
	at /opt/elasticbeanstalk/lib/ruby/lib/ruby/2.2.0/open3.rb:193:in `spawn'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/2.2.0/open3.rb:193:in `popen_run'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/2.2.0/open3.rb:188:in `popen2e'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/executor-1.2/lib/executor/exec.rb:101:in `exec'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/executor-1.2/lib/executor/exec.rb:72:in `sh'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/executor-1.2/lib/executor.rb:15:in `sh'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/executable.rb:78:in `execute!'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/hook-directory-executor.rb:31:in `block (2 levels) in run!'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/activity.rb:195:in `call'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/activity.rb:195:in `exec'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/activity.rb:140:in `timeout_exec'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/activity.rb:123:in `block in create'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/activity.rb:122:in `instance_eval'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/activity.rb:122:in `create'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/hook-directory-executor.rb:29:in `block in run!'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/hook-directory-executor.rb:27:in `each'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/hook-directory-executor.rb:27:in `run!'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/command.rb:213:in `block (3 levels) in exec_stage'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/activity.rb:195:in `call'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/activity.rb:195:in `exec'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/activity.rb:140:in `timeout_exec'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/activity.rb:123:in `block in create'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/activity.rb:122:in `instance_eval'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/activity.rb:122:in `create'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/command.rb:208:in `block (2 levels) in exec_stage'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/command.rb:206:in `each'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/command.rb:206:in `each_with_index'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/command.rb:206:in `block in exec_stage'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/activity.rb:195:in `call'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/activity.rb:195:in `exec'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/activity.rb:140:in `timeout_exec'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/activity.rb:123:in `block in create'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/activity.rb:122:in `instance_eval'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/activity.rb:122:in `create'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/command.rb:203:in `exec_stage'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/command.rb:150:in `execute!'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/command-processor.rb:77:in `block (3 levels) in execute!'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/activity.rb:195:in `call'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/activity.rb:195:in `exec'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/activity.rb:140:in `timeout_exec'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/activity.rb:123:in `block in create'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/activity.rb:122:in `instance_eval'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/activity.rb:122:in `create'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/command-processor.rb:74:in `block (2 levels) in execute!'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/command-processor.rb:144:in `execute_command'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/command-processor.rb:70:in `block in execute!'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/healthd_reporter.rb:56:in `log'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/lib/elasticbeanstalk/command-processor.rb:69:in `execute!'
	from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.2.0/gems/beanstalk-core-2.11/bin/command-processor:45:in `<top (required)>'
	from /opt/elasticbeanstalk/lib/ruby/bin/command-processor:23:in `load'
	from /opt/elasticbeanstalk/lib/ruby/bin/command-processor:23:in `<main>'


[2017-10-21T20:42:25.638Z] INFO  [4388]  - [Application update 2.0.5-8@52/AppDeployStage1/AppDeployPostHook] : Activity internal failure.
[2017-10-21T20:42:25.638Z] INFO  [4388]  - [Application update 2.0.5-8@52/AppDeployStage1] : Activity internal failure.
[2017-10-21T20:42:25.638Z] INFO  [4388]  - [Application update 2.0.5-8@52] : Completed activity. Result:
  Application update - Command CMD-AppDeploy failed
[2017-10-21T20:42:54.455Z] INFO  [4886]  - [CMD-TailLogs] : Starting activity...
[2017-10-21T20:42:54.455Z] INFO  [4886]  - [CMD-TailLogs/AddonsBefore] : Starting activity...
[2017-10-21T20:42:54.455Z] INFO  [4886]  - [CMD-TailLogs/AddonsBefore] : Completed activity.
[2017-10-21T20:42:54.456Z] INFO  [4886]  - [CMD-TailLogs/TailLogs] : Starting activity...

Enhanced Health not working

I've noticed that my enhanced health metrics are not reporting. Albeit I have a modified version of this repo, the abseils remains largely the same and I wondered if this was a known issue?

Log rotate

I see the file to set the log rotate settings for nginx and php-fpm, but I don't see how these are applied. My instance does not appear to be rotating, could you shed any light on this please?

Error when setting Environment variable with white space in value - FIX

For anyone having trouble setting environment variables using AWS console when the value contains white space, make sure to wrap the value in single quotes like: 'My value with spaces'.

Will include the event entry to make this fix easier to find for others:

[Instance: i-XXXXXXXXXX] Command failed on instance. Return code: 6 Output: nginx: [emerg] invalid number of arguments in "fastcgi_param" directive in /etc/nginx/fastcgi_params_env:3 nginx: configuration file /etc/nginx/nginx.conf test failed. Hook /opt/elasticbeanstalk/hooks/configdeploy/enact/99_reload_app_server.sh failed. For more detail, check /var/log/eb-activity.log using console or EB CLI.

Failed Install

I was trying to take a shot at this but I am getting error messages. I downloaded the zip and uploaded the zip as is, so all the files should be there. (And I checked to make sure the files are there)

Screen Shot 2020-02-03 at 2 11 18 PM

My instance Info:
PHP 7.1 running on 64bit Amazon Linux/2.9.1

Would be great to know if this can be fixed.
Also, is there a way to modify this so I can use php-fpm with apache instead of nginx?

Seems to be failing on php-fpm

Not sure if recent updates or what, i see aws now supports 7.1 which i dont think it did when this was made. I tried to deploy this and got an error with php-fpm.

Basically 'service php-fpm start' fails although yum install php71-fpm works fine and confirms its there.

Before i go digging for a solution figured i'd ask as maybe i'm missing something obvious.

Elastic Beanstalk Health Severe - FIX

To anyone who is using this repo (it's awesome by the way!), you might be running into some problems with the health of Elastic Beanstalk always being severe.

The source of the issue stems from the checks that the load balancer makes to elastic beanstalk:

# This is a log entry from /var/log/nginx/access_log
XXX.XX.XX.XX - - [24/Apr/2018:02:55:29 +0000] "GET / HTTP/1.1" 301 31 "-" "ELB-HealthChecker/2.0" "-"

The ELB expects by default that the status code returned should be 200. In this case, it's a 301 because of specific setup for the application/website (EG: force redirect to https).

This is caused by the load balancer settings for elastic beanstalk. To edit them:

  1. Go to your elastic beanstalk environment
  2. Select your specific application
  3. Use the sidebar to select "Configuration", then "modify" the Load Balancer.
  4. From "Processes", select the checkbox next to name "default" and use the "Actions" dropdown > "Edit"
  5. The next settings are really specific to your setup, however as an example you can change the port to protocol to be specific to your needs. Basically, make sure the load balancer health check gets the correct status code.

Hope this helps someone!

start php-fpm-7.1 fails on new PHP Elasticbeanstalk platform

The new ElasticBeanstalk PHP platform is using php7.2 by default now. When I use my old, working .ebextensions configs based off this repo, I get errors related to starting php-fpm-7.1. I am testing installing php72-fpm package and changing all the 7.1 refrences to 7.2 now. Will report back on progress

healtd.nginx.conf typo

I have the error while deploying:

ERROR: [Instance: i-0ddd68fb52ba1e030] Command failed on instance. Return code: 1 Output: cp: cannot stat '.ebextensions/setup/config/cron/healthd.nginx.conf': No such file or directory. 
[18:45:21]container_command 080_healthd_nginx_cron_conf in .ebextensions/01_setup.config failed. For more detail, check /var/log/eb-activity.log using console or EB CLI.
[18:45:21]INFO: Command execution completed on all instances. Summary: [Successful: 0, Failed: 1].

I suppose it's a typo in healtd.nginx.conf file

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.