uwsgi upstart on amazon linux - nginx

I created a uwsgi file following this tutorial https://uwsgi.readthedocs.org/en/latest/Upstart.html on amazon linux. Though It doesn't seem to run as Nginx just says bad gateway. If I run the
/etc/init/uwsgi.conf
description "uwsgi tiny instance"
start on runlevel [2345]
stop on runlevel [06]
exec /home/ec2-user/venv/bin/uwsgi --ini /home/ec2-user/uwsgi-prod_demo.ini
if I do run the following in shell, then python application runs.
/home/ec2-user/venv/bin/uwsgi --ini /home/ec2-user/uwsgi-prod_demo.ini
uwsgi-prod_demo.ini
[uwsgi]
socket = :8080
chdir = /home/ec2-user/prod_demo
master = True
venv = /home/ec2-user/venv
callable = app
wsgi-file = /home/ec2-user/prod_demo/manage.py
enable-threads = True
https = =0,/home/ec2-user/xxx.com.au.pem,/home/ec2-user/newkey.pem,HIGH
nginx.conf
user ec2-user;
worker_processes 1;
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;
listen 443 ssl;
ssl_certificate /home/ec2-user/xxx.com.au.pem;
ssl_certificate_key /home/ec2-user/newkey.pem;
server_name import.xxx.com.au *.import.xxx.com.au;
access_log /var/log/prod_demo/access_log;
root /home/ec2-user/prod_demo;
location / {
uwsgi_pass 127.0.0.1:8080;
include uwsgi_params;
}
location /static {
alias /home/ec2-user/prod_demo/app/static;
}
location = /favicon.ico {
alias /home/ec2-user/prod_demo/app/static/images/favicon.ico;
}
}
}

To fix this I did a few things.
- Moved all scripts from the home directory to an /var/www/
- created an www group and www user and chown /var/www to www:www
Full Instructions
Create a user and group www and www
sudo groupadd www
sudo adduser www -g www
create a dir where your flask application will be ie /var/www/
sudo chown -R www:www /var/www
/etc/nginx/nginx.conf
# /etc/nginx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user www;
worker_processes 1;
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;
client_max_body_size 20M;
#keepalive_timeout 0;
keepalive_timeout 0;
uwsgi_read_timeout 86400;
uwsgi_send_timeout 86400;
#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;
listen 443 ssl;
ssl_certificate /var/www/test.com.au.pem;
ssl_certificate_key /var/www/newkey.pem;
server_name demo.test.com.au;
access_log /var/log/prod_demo/access_log;
root /var/www/prod_demo;
location / {
uwsgi_pass 127.0.0.1:28080;
include uwsgi_params;
}
location /static {
alias /var/www/prod_demo/app/static;
}
location = /favicon.ico {
alias /var/www/prod_demo/app/static/images/favicon.ico;
}
}
server {
listen 80;
listen 443 ssl;
ssl_certificate /var/www/test.com.au.pem;
ssl_certificate_key /var/www/newkey.pem;
server_name ajtravel.test.com.au;
access_log /var/log/prod_demo_two/access_log;
root /var/www/prod_demo_two;
location / {
uwsgi_pass 127.0.0.1:28082;
include uwsgi_params;
}
location /static {
alias /var/www/prod_demo_two/app/static;
}
location = /favicon.ico {
alias /var/www/prod_demo_two/app/static/images/favicon.ico;
}
}
#test config
server {
listen 80;
listen 443 ssl;
ssl_certificate /var/www/test.com.au.pem;
ssl_certificate_key /var/www/newkey.pem;
server_name demo.test.test.com.au;
access_log /var/log/prod_demo/access_log;
root /var/www/prod_demo;
location / {
uwsgi_pass 127.0.0.1:28080;
include uwsgi_params;
}
location /static {
alias /var/www/prod_demo/app/static;
}
location = /favicon.ico {
alias /var/www/prod_demo/app/static/images/favicon.ico;
}
}
server {
listen 80;
listen 443 ssl;
ssl_certificate /var/www/test.com.au.pem;
ssl_certificate_key /var/www/newkey.pem;
server_name ajtravel.test.test.com.au;
access_log /var/log/prod_demo_two/access_log;
root /var/www/prod_demo_two;
location / {
uwsgi_pass 127.0.0.1:28082;
include uwsgi_params;
}
location /static {
alias /var/www/prod_demo_two/app/static;
}
location = /favicon.ico {
alias /var/www/prod_demo_two/app/static/images/favicon.ico;
}
}
}
/etc/init/uwsgi-prod-demo.conf
# https://uwsgi.readthedocs.org/en/latest/Upstart.html
# /etc/init/uwsgi.conf
# simple uWSGI script
description "uwsgi tiny instance"
#start on runlevel [2345]
#stop on runlevel [06]
start on started elastic-network-interfaces
exec /var/www/venv/bin/uwsgi --ini /var/www/uwsgi-prod_demo.ini
/var/www/uwsgi-prod_demo.ini
[uwsgi]
uid = www
gid = www
socket = :28080
chdir = /var/www/prod_demo
master = True
venv = /var/www/venv
callable = app
wsgi-file = /var/www/prod_demo/manage.py
enable-threads = True

Related

uWSGI + NGINx + web2py application not accassable

I am using centos 7 with python 2.7.15 and uwsgi + nginx to host my app.
step by step i am getting closer to make it work.
I had to set the python 2.7.15 to work as python insted of 2.7.5
then I had some uwsgi probmels with emperor service.
but now... the app works when I run uwsgi trough
uwsgi --http :8000 --chdir /opt/web2py -w wsgihandler:application
but when I try to put it together with nginx I cannot access the page
My nginx config ATM is
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
map $request_uri $loggable {
~/engine/getTasks.* 0;
~/static/* 0;
default 1;
}
access_log /var/log/nginx/access.log main if=$loggable;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
client_max_body_size 10M;
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
location ^~ /.well-known/acme-challenge/ {
default_type "text/plain";
root /opt/web2py_cert/web2py.com;
}
location / {
uwsgi_pass unix:/run/uwsgi/web2py.sock;
include uwsgi_params;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
And my uwsgi.ini file
[uwsgi]
plugin = python2.7
logto = /opt/web2py/uwsgi.log
chdir = /opt/web2py
http = 0.0.0.0:80
module = wsgihandler:application
master = true
processes = 5
uid = woshi
socket = /run/uwsgi/web2py.sock
chown-socket = woshi:nginx
chmod-socket = 660
vacuum = true
any suggestions???
thank you

can't change nginx default port

I installed nginx on mac os sierra by
brew install nginx
here one concern brew started nginx service automatically but it shouldn't act like this (I should tell I need a background service or not!!!) but so far so good and localhost:8080 shows default nginx page.
then I tried to change the port in nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$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;
#}
}
# 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 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
include servers/*;
}
I restart service but now it's not serving 8080 neither 80
I tried to stop the service and start it my self by
sudo nginex
I get
nginx: [emerg] open() "/usr/local/var/run/nginx.pid" failed (2: No such file or directory)
I searched a lot and read a lot about this but I couldn't find similar issue
It seems your nginx is not running.
Check if nginx is running
$ ps -ef | grep nginx
When you see something like this
0 10653 1 0 3:32PM ?? 0:00.01 nginx: master process nginx
-2 11498 10653 0 3:37PM ?? 0:00.01 nginx: worker process
nginx is on. Otherwise it is not.
If nginx is not on run nginx first.
$ sudo nginx
If you editted nginx file config after nginx is on, you should reload nginx.
// assumes nginx is on
$ sudo nginx -s reload
By now you should see changes in effect.

NGinx: set localhost/project to project git folder

I am currently working on a small website in my free time. For development, I keep all git repositories in ~/git. Is it possible to configurate nginx so that if I go to localhost/A or localhost/B, it would use ~/git/A and ~/git/B respectively?
I am using fedora and tried with a single repo, but I keep getting 404:
/etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
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;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name localhost;
root /home/MartenBE/git/;
index index.html index.php;
location /A/ {
alias /home/MartenBE/git/A/;
}
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
I think only following should work for you.
server {
listen 80;
server_name localhost;
root /home/MartenBE/git/;
index index.html index.php;
}
You don't need to add location module because you have already defined root. Now if you try localhost/A/index.html, Nginx will try to find it under /home/MartenBE/git/A/index.html.
What are the requests you are trying ?
First of all it is good to seperate site configuration from main nginx conf file.
Do the following..
Remove all server blocks from /etc/nginx/nginx.conf.
create a file in /etc/nginx/sites-available/yourconf with the following content.
server {
listen 80;
server_name localhost;
root /home/MartenBE/git/;
index index.html index.php;
}
Create a symlink to the config file inside /etc/nginx/sites-enabled/yourconf.
Remove /etc/nginx/sites-enabled/default & reload the nginx config.
NOTE: if you are using ubuntu run sudo service nginx reload

Nginx 1.8 cache encoutered forbidden 403

I just want to cache my website's url /. But when I request the url, get 403 error, (other urls is Ok).the error log:
2016/01/06 17:25:04 [error] 26263#0: *1 directory index of "/home/app/path/" is forbidden, client: 223.85.143.126, server: domain.com, request: "GET / HTTP/1.1", host: "www.domain.com", referrer: "http://www.domain.com/accounts/"
I've try chmod 777 -R /home/app/path/ or chmod 777 -R /home/app/path/nginx_cache or change user nginx; to user root; in nginx.conf, all doesnot work.
Here is infomation.
sites-enabled conf:
proxy_cache_path /home/app/path/nginx_cache levels=1:2 keys_zone=my_cache:1m max_size=50m inactive=60m use_temp_path=off;
server {
listen 80;
server_name domain.com www.domain.com;
index index.html index.htm;
root /home/app/path;
charset utf-8;
client_max_body_size 5M;
if ($host !~* ^(domain.com|www.domain.com)$){
return 444;
}
location /media {
alias /home/app/path/media;
}
location /static {
alias /home/app/path/static;
}
location / {
uwsgi_pass 127.0.0.1:3032;
include /home/app/path/uwsgi_params;
}
location = / {
proxy_cache my_cache;
proxy_cache_valid 15s;
}
}
nginx.conf:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 768;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 30;
types_hash_max_size 2048;
reset_timedout_connection on;
client_body_timeout 10;
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 off;
#gzip on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Location do not inherits rules, so there is no UWSGI pass, nginx tries to display directory index and fails. Should be something like this:
location = / {
uwsgi_pass 127.0.0.1:3032;
include /home/app/path/uwsgi_params;
proxy_cache my_cache;
proxy_cache_valid 15s;
}

ArchLinux + nginx + Passenger Rails app always pointing to localhost

This is my first time using Arch based Linux environment (Manjaro Linux), and also Nginx.
I installed Nginx, opened http://localhost and it showed the standard Welcome to Nginx page.
Then I created 2 folders:
/etc/nginx/sites-available
etc/nginx/sites-enabled
In sites-available, I created a conf file named iima, with following data:
server {
listen 80;
server_name iima.merged.localhost;
passenger_enabled on;
location / {
root /home/path/redacted/public;
}
}
and then created a symbolic link:
sudo ln -s /etc/nginx/sites-available/iima /etc/nginx/sites-enabled/iima
Then in the /opt/nginx/conf/nginx.conf, I added:
include /etc/nginx/sites-enabled/*;
In the /etc/hosts, I added the following line at the end of the file:
127.0.0.1 iima.merged.localhost iima.merged.localhost
# I also tried below line while removing above line
127.0.0.1 iima.merged.localhost
Restarted the nginx server using sudo systemctl restart nginx (also tried with reload), but typing iima.merged.localhost always points to localhost page.
Am I missing anything? There are no error(s).log created in /var/log/nginx.
EDIT:
Content of /opt/nginx/conf/nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
passenger_root /home/harsh/.rbenv/versions/1.8.7-p375/lib/ruby/gems/1.8/gems/passenger-4.0.45;
passenger_ruby /home/harsh/.rbenv/versions/1.8.7-p375/bin/ruby;
include mime.types;
include /etc/nginx/sites-enabled/*;
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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
# server_name_in_redirect off;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$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;
#}
}
# 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 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}

Resources