I'm having trouble deploying my app. I have static files at public/javascripts and public/stylesheets. What is the correct way to write that in the static-files.config file for AWS Elastic Beanstalk...
This doesn't work...
option_settings:
aws:elasticbeanstalk:environment:proxy:staticfiles:
/public/javascripts: javascripts
/public/stylesheets: stylesheets
It is a Mongo, Express, Node app using EJS.
The problem in more detail is this...when the app deploys, my home page is there with no styling and none of the links work. I have rearranged the ordering of things in the config files several ways but nothing seems to help get the app to see the stylings and javascript. One version had the file like this...
option_settings:
aws:elasticbeanstalk:environment:proxy:staticfiles:
/javascripts: public/javascripts
/stylesheets: public/stylesheets
and I ended up with this error in the log...
/var/log/nginx/error.log
2022/02/14 18:05:08 [error] 3601#3601: *13 open() "/var/app/current/stylesheets/.env" failed (2: No such file or directory), client: 20.97.246.144, server: , request: "GET /public/.env HTTP/1.1", host: "3.98.252.66"
In the app structure, the public folder is at the root/top level with the javascripts and stylesheets folders the next level down from public(in otherwords /public/javascript/ and /public/styleshhets/). The javascript folder contains three javascript files, and the stylesheets folder contains three css files. If I knew for sure the config was correct, I could move on to see if the problem is something else. The app of course works in development and when deployed to Heroku.
This is the documentation from AWS...
AWS elastic beanstalk static files documentation
K
I had this working before on AWS Linux AMI but no luck with AWS Linux 2.
I need to access my environment properties from the Nginx configuration file during the EB application deployment. It's a Single instance Node Server.
I did it like this with the AWS Linux AMI and it worked without a problem:
.ebextensions/00_options.config
option_settings:
aws:elasticbeanstalk:application:environment:
DOMAIN: socket.example.com
MASTER_DOMAIN: https://example.com
etc..
.ebextensions/10_proxy.config
... some configs ...
files:
/etc/nginx/conf.d/proxy.conf:
mode: "000644"
owner: root
group: root
content: |
upstream nodejs {
server 127.0.0.1:8081;
keepalive 256;
}
map $http_origin $cors_header {
hostnames;
default "";
`{"Fn::GetOptionSetting": {"Namespace": "aws:elasticbeanstalk:application:environment", "OptionName": "MASTER_DOMAIN"}}` "$http_origin";
}
server {
listen 80;
listen 8080;
server_name `{"Fn::GetOptionSetting": {"Namespace": "aws:elasticbeanstalk:application:environment", "OptionName": "DOMAIN"}}`;
location ~ /.well-known {
allow all;
root /usr/share/nginx/html;
}
location / {
return 301 https://$host$request_uri;
}
}
etc..
.... some more configs ....
I'm not including most of the configs above because they're not relevant.
So when I did this before, everything worked as expected. The config file inserted my properties and created the file in the /etc/nginx/conf.d/proxy.conf folder.
Now with AWS Linux 2 the specs have changed and we have to add our Nginx configuration files in the .platform/nginx/conf.d folder located in our application bundle root folder.
Here the reference ( see Reverse proxy configuration)
So I created a proxy.conf file in the location mentioned above with the content that was previously inserted in /etc/nginx/conf.d/proxy.conf.
.platform/nginx/conf.d/proxy.conf
upstream nodejs {
server 127.0.0.1:8081;
keepalive 256;
}
map $http_origin $cors_header {
hostnames;
default "";
`{"Fn::GetOptionSetting": {"Namespace": "aws:elasticbeanstalk:application:environment", "OptionName": "MASTER_DOMAIN"}}` "$http_origin";
}
etc...
And then the problems began..
This first trial throwed unexpected "{" in /var/proxy/staging/nginx/conf.d/proxy.conf:11 at me.
And after that I tried a lot of things. Tried it with ${MASTER_DOMAIN} and messed around with the new EB AWS Linux 2 hooks (see link above Platform hooks). All for no avail it seems like you can't access the properties from the Nginx configs. I've read an article or a documentation from Nginx mentioning something similar today but I can't find it anymore (did a lot of googling).
I also tried to create a config file like I did with the working version which purpose was to save a temp file somewhere with the included properties and then include this file in the needed .platform/nginx/conf.d/proxy.conf file because I started to think that there is no way to include them directly with the new specs.
.ebextensions/10_proxy.config
... some configs ....
files:
/var/proxy/staging/custom_folder/proxy.conf:
mode: "000644"
owner: root
group: root
content: |
etc...
.platform/nginx/conf.d/proxy.conf
include custom_folder/proxy.conf;
With this idea in mind I did a lot of nonsense, I created hooks for creating (mkdir) directories in which I tried to temporarily save the file which leaded to new permission errors. I wasn't able to give the proper permissions to prebuild, postdeploy files but this is another issue.
And a lot more of trying and failing...
But then I've read (also from the link above):
"If you configure your proxy to send traffic to multiple application processes, you can configure several environment properties, and use their values in both proxy configuration and your application code."
And hope came back.. Does this mean I actually CAN directly add environmental variables into the Nginx configs located in the .platform directory? ... I don't know.. Do you?
I could continue to describe all the things I tried all night long so I will stop here. I hope you get the issue. If not ask me and I will do my best to make all this understandable.
Also my mind isn't very clear anymore after 14 hours of battling this issue. I need a break.
If you did it to the end thank you for your time and help would be greatly appreciated.
Summary
One way to do it is to create a shell script in .platform/hooks/postdeploy.
Here is a simplified example, assuming you have an Elastic Beanstalk environment property called MASTER_DOMAIN:
#!/bin/bash
# write nginx config file
cat > /etc/nginx/conf.d/elasticbeanstalk/test.conf << LIMIT_STRING
location /test/ {
default_type text/html;
return 200 "nginx variable: \$host, and EB env property: $MASTER_DOMAIN";
}
LIMIT_STRING
# restart nginx service so the config takes effect
systemctl restart nginx.service
The location block from this example can be replaced by the nginx content from .ebextensions/10_proxy.config in the original post. No need for the Fn::GetOptionSetting stuff though.
I think you also need a duplicate script in .platform/confighooks/postdeploy.
Details below.
(sorry for the wall of text)
Environment variables in nginx
Actually, as discussed in here and here, it is not possible (out-of-the-box) to use os environment variables inside the http, server, or location blocks in nginx config files. There are some workarounds, such as using lua, perl, or templates, but let's not get into those. This part has nothing to do with AWS.
In the OP's original configuration for Amazon Linux AMI (AL1), using the files section in .ebextensions/10_proxy.config, they were actually using a shell script to write the nginx config file during deployment. The shell script expanded the environment variables, but the resulting proxy.conf for nginx did not actually access any environment variables.
That's why it worked on AL1.
Platform hooks
Now, for Amazon Linux 2 (AL2), we can do something similar using shell scripts in the .platform/hooks and .platform/confighooks folders.
These .platform hook scripts are executed as the root user, and they have access to the Elastic Beanstalk (EB) environment properties. The EB environment properties can be accessed just like normal OS environment variables, so there is no need to use the Fn::GetOptionSetting stuff.
Basically, we need to create a shell script that writes a file with the content from your original .ebextensions/10_proxy.config. However, there are two questions we need to consider:
Should we use a prebuild, predeploy, or postdeploy hook?
What is the proper destination directory for our nginx proxy.conf file?
File locations
To answer these questions, we have to refer to the AWS documentation for Extending Elastic Beanstalk Linux platforms, and specifically the Instance deployment workflow section.
... The current working directory (cwd) for platform hooks is the application's root directory. For prebuild and predeploy files it's the application staging directory, and for postdeploy files it's the current application directory. If one of the files fails (exits with a non-zero exit code), the deployment aborts and fails.
This is interesting, but leaves some questions, e.g. where is the "application staging directory" located? We can fill in the blanks by inspecting one of our deployment log files. Based on our eb-engine.log, here's what happens with the platform hooks and nginx config files during app deployment (skipping a lot of details):
the source bundle is downloaded from S3 and extracted to /var/app/staging/
platform hooks in .platform/hooks/prebuild/ are executed
proxy server configuration is copied from /var/app/staging/.platform/nginx/ to /var/proxy/staging/nginx
platform hooks in .platform/hooks/predeploy/ are executed
proxy server is started, configuration is copied from /var/proxy/staging/nginx/ to /etc/nginx
platform hooks in .platform/hooks/postdeploy/ are executed
Note, after deployment the app is located in /var/app/current.
Based on the above, there are several options:
Create a shell script in .platform/hooks/postdeploy that writes to /etc/nginx/conf.d/proxy.conf.
The nginx service is already running, at this stage, so we need to restart for the configuration to take effect.
Below is a minimal test example. In this example we write to the elasticbeanstalk subdirectory, because we just want to add a location inside the default server block. We can then visit the /test/ page in a browser, to check that the configuration works.
We use some bash io redirection (<<, >) to write the nginx config file.
Note that we need to escape any nginx variables, e.g. $host becomes \$host, otherwise the shell will interpret them as environment variables.
Also note that the shell scripts need to have execution permission, as explained under More about platform hooks in the docs.
#!/bin/bash
cat > /etc/nginx/conf.d/elasticbeanstalk/test.conf << LIMIT_STRING
location /test/ {
default_type text/html
return 200 "nginx variable: \$host, and EB env property: $MASTER_DOMAIN";
}
LIMIT_STRING
systemctl restart nginx.service
Alternatively, we could create a shell script in .platform/hooks/predeploy that writes to /var/proxy/staging/nginx/conf.d/proxy.conf.
There is no need to restart the nginx service in this case, because this hook is executed before the server configuration is applied.
BEWARE:
Not sure if this is a bug or a design feature, but our newly created proxy.conf disappears after a configuration deployment (as opposed to an application deployment), unless we put a duplicate script in the .platform/confighooks/postdeploy directory. Not very DRY...
EDIT: AWS support confirmed that we need duplicate scripts in hooks and confighooks in this case. The application example in the docs also shows some duplicates (at least duplicate filenames) in hooks and confighooks.
EDIT:
Instead of duplicating scripts, we can also write a confighook that calls a hook, e.g. .platform/confighooks/predeploy/01_my_confighook.sh could look like this:
#!/bin/bash
source "/var/app/current/.platform/hooks/predeploy/01_my_hook.sh"
Disclaimer: This was tested on a freshly created single instance EB environment with "Python 3.7 running on 64bit Amazon Linux 2/3.1.5" using all default configuration and the default AWS Python sample application (only extended with our custom hooks).
I'm deploying an PHP application a AWS Elastic Beanstalk.
The base app is working, but I doesn't get a configured writable directory for the app
When I try to create a temporary file with the following code:
$CACHEFilePath = \Yii::$app->runtimePath . $model->reportpath;
$tempfile = tempnam($CACHEFilePath, 'charts') . '.png';
I'm getting the following error :
tempnam(): file created in the system's temporary directory
Even I force the directory to be /tmp, the error stay the same. I also tried to force the upload_tmp_dir from php.ini with Environment properties
Please advice
Well I change my code like follow :
$CACHEFilePath = sys_get_temp_dir() . $this->reportpath;
with the upload_tmp_dir=/tmp in the php.ini
I have a webapp which I deploy on a Jetty server through jetty runner. I inject properties read through a properties file using Spring. As of now, I have kept the properties file within the webapp itself (in WEB-INF/classes) directory. I want to keep this properties file external to the webapp and then inject them using Spring. Is there any configuration in the jetty.xml file I can do so I can achieve this?
I managed to solve this by adding the properties file as a command line parameter while starting jetty. Something like:
java -Dexternal.properties.file=myfile.properties -jar jetty.jar
In java code, I read it by getting system property and then using FileUtils to get file.
String externalPropertiesFile = System.getProperty("extern.properties.file");
File file = FileUtils.getFile(externalPropertiesFile);
I'm trying to set up openshift to publish my django project.
I created a scalable python3.3 app with django preinstalled and I added postgres9.2 cartridge.
I found the dirs structure quite complicated but in the end I noticed that the default example project was located under apps-root/runtime/repo/wsgi/openshift/ so I moved all files from this directory to a folder named 'backup' and I pasted here my project.
Now when I visit my site I get:
503 Service Unavailable
No server is available to handle this request.
I read that this can be due to HAproxy. I tried to restart my app through Openshift Online Web Interface but I still get the same error.
So:
1) How can I solve this issue?
2) How can I change the root folder of my project from apps-root/runtime/repo/wsgi/openshift/ to the root of my git repo so that I don't have unwanted folder (i.e. /wsgi/openshift/) in my local and bitbucket repo?
UPDATE:
looking at my logs I get:
==> python/logs/appserver.log <==
server = server_class((host, port), handler_class)
File "/opt/rh/python33/root/usr/lib64/python3.3/socketserver.py", line 430, in __init__
self.server_bind()
File "/opt/rh/python33/root/usr/lib64/python3.3/wsgiref/simple_server.py", line 50, in server_bind
HTTPServer.server_bind(self)
File "/opt/rh/python33/root/usr/lib64/python3.3/http/server.py", line 135, in server_bind
socketserver.TCPServer.server_bind(self)
File "/opt/rh/python33/root/usr/lib64/python3.3/socketserver.py", line 441, in server_bind
self.socket.bind(self.server_address)
OSError: [Errno 98] Address already in use
If I visit HAProxy status page in Express table "Server Status" is DOWN both in "local-gear" and "backend" rows.
I have the same issue and this was resolved after changing the haproxy.cfg.
option httpchk GET /
Comment out that line in haproxy.cfg or else set it to
option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www
where www is your app link. See http://haproxy.1wt.eu/download/1.4/doc/configuration.txt for detail if you want to know more about haproxy configuration. Hope it works
If you want to build django yourself you may might want to check out this thread as I think it will help How to configure Django on OpenShift?
If you want to use something prebuilt then check out the django quickstart here https://www.openshift.com/quickstarts/django