web2py is an awesome python framework which has great documentation including several deployment recipes. Yet what I miss there is the recipe for deploying using nginx (preferably with uwsgi). There are some incomplete notes around the web (like here), but I couldn't find any complete, stand-alone guide.
OK, looking closer into the web2py email list that I linked above, I figured out that the copmlete solution is already there. I could follow the instructions and, thanks pbreit's brilliant post, now my deployment works like a charm (using only 38MB RAM in idle state) with nginx+uwsgi.
Here are the parts that I used (I just stripped down the fabfile.py to use it on command line)
Note: where there is 'put('....' I used nano text editor to create and edit files
apt-get -y install build-essential psmisc python-dev libxml2 libxml2-dev python-setuptools
cd /opt;
wget http://projects.unbit.it/downloads/uwsgi-latest.tar.gz
tar -zxvf uwsgi*
mv /opt/uwsgi*/ /opt/uwsgi/
cd /opt/uwsgi/; python setup.py install
chown -R www-data:www-data /opt/uwsgi
touch /var/log/uwsgi.log
chown www-data /var/log/uwsgi.log
apt-get -y install libpcre3-dev build-essential libssl-dev
cd /opt; wget http://nginx.org/download/nginx-0.8.54.tar.gz
cd /opt; tar -zxvf nginx*
cd /opt/nginx*/; ./configure --prefix=/opt/nginx --user=nginx --group=nginx --with-http_ssl_module
cd /opt/nginx*/; make
cd /opt/nginx*/; make install
adduser --system --no-create-home --disabled-login --disabled-password --group nginx
cp /opt/uwsgi*/nginx/uwsgi_params /opt/nginx/conf/uwsgi_params
wget https://library.linode.com/web-servers/nginx/installation/reference/init-deb.sh
mv init-deb.sh /etc/init.d/nginx
chmod +x /etc/init.d/nginx
/usr/sbin/update-rc.d -f nginx defaults
/etc/init.d/nginx start
cd /opt/
wget https://library.linode.com/web-servers/nginx/python-uwsgi/reference/init-deb.sh
mv /opt/init-deb.sh /etc/init.d/uwsgi
chmod +x /etc/init.d/uwsgi
echo 'PYTHONPATH=/var/web2py/ MODULE=wsgihandler' >> /etc/default/uwsgi
/usr/sbin/update-rc.d -f uwsgi defaults
/etc/init.d/uwsgi start
rm /opt/nginx/conf/nginx.conf
# modify nginx.conf below and save it as /opt/nginx/conf/nginx.conf
cd /opt/nginx/conf; openssl genrsa -out server.key 1024
cd /opt/nginx/conf; openssl req -batch -new -key server.key -out server.csr
cd /opt/nginx/conf;
openssl x509 -req -days 1780 -in server.csr -signkey server.key -out server.crt
/etc/init.d/nginx restart
nginx.conf
user www-data;
worker_processes 4;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 2;
sendfile on;
#tcp_nopush on;
tcp_nodelay on;
gzip on;
server {
listen 80;
server_name example.com www.example.com;
location / {
uwsgi_pass 127.0.0.1:9001;
include uwsgi_params;
}
location /static {
root /var/web2py/applications/init/;
}
}
# HTTPS server
server {
listen 443;
server_name www.example.com example.com;
ssl on;
ssl_certificate /opt/nginx/conf/server.crt;
ssl_certificate_key /opt/nginx/conf/server.key;
location / {
uwsgi_pass 127.0.0.1:9001;
include uwsgi_params;
uwsgi_param UWSGI_SCHEME $scheme;
}
location /static {
root /var/web2py/applications/init/;
}
}
}
Derived from web2py email list
With the help from this Linode post
There is a solution here: http://www.web2pyslices.com/slice/show/1495/updated-uwsgi-nginx-script-for-ubuntu-1110
Related
i try to setup an nginx server with reverse proxy via a bash script. But somehow i end up in an endless redirect loop but i dont know why. Does someone has an idea?
sudo apt install nginx -y
sudo apt-get remove apache2* -y
sudo apt install ufw -y
sudo ufw allow ssh
sudo ufw allow 'Nginx Full'
sudo systemctl start nginx
sudo systemctl restart nginx
#sudo systemctl status nginx
sudo mkdir -p /var/www/servername.com/html
sudo chmod -R 755 /var/www/servername.com
sudo chown -R $USER:$USER /var/www/servername.com/html
sudo bash -c 'echo "<html> <head> <title>Welcome to servername.com!</title> </head> <body> <h1>Success! The servername.com server block is working!</h1> <b>Meow Meow!</b> </body> </html>" > /var/www/servername.com/html/index.html'
sudo bash -c 'echo "server {
listen 80;
listen [::]:80;
root /var/www/servername.com/html;
index index.html index.htm index.nginx-debian.html;
#You can put your domain name here for ex: www.servername.com
server_name MYIP;
location / {
try_files $uri $uri/ =404;
}
}" > /etc/nginx/sites-available/servername.com'
sudo ln -s /etc/nginx/sites-available/servername.com /etc/nginx/sites-enabled/
sudo systemctl restart nginx
I am trying to confirm my logs are in json format but I cannot even see one log. I am using docker-compose
version: '3'
services:
nginx:
image: test_site
volumes:
- /Users/mikeJ/Desktop/test-logs/access:/tmp/logs/access
- /Users/mikeJ/Desktop/test-logs/error:/tmp/logs/error
build:
context: .
restart: unless-stopped
ports:
- "8040:8040"
ngnix.conf
worker_processes 1;
events { worker_connections 1024; }
http {
include mime.types;
sendfile on;
access_log on;
log_format json_combined escape=json
'{'
'"time_local":"$time_local",'
'"remote_addr":"$remote_addr",'
'"remote_user":"$remote_user",'
'"request":"$request",'
'"status": "$status",'
'"body_bytes_sent":"$body_bytes_sent",'
'"request_time":"$request_time",'
'"http_referrer":"$http_referer",'
'"http_user_agent":"$http_user_agent"'
'}';
server {
listen 8040;
error_log /tmp/logs/error/error.log warn;
access_log /tmp/logs/access/access.log;
server_name localhost;
location /{
root /usr/share/nginx/html/;
index index.html;
}
location ~ ^/test/footer {
root /usr/share/nginx/html/;
expires 5m;
access_log on;
}
}
dockerfile
FROM nginx:1.15.0-alpine
RUN rm -v /etc/nginx/nginx.conf
# Copying nginx configuration file
ADD nginx.conf /etc/nginx/
# setup nginx caching
RUN mkdir -p /tmp/nginx/cache
#create directory for logs
RUN mkdir -p /tmp/logs/error
RUN mkdir -p /tmp/logs/access
#adding footer file
ADD footer /usr/share/nginx/html/footer
# Expose ports
EXPOSE 8040
I even ssh into the container and nothing is there.
from inside the container
# ps aux | grep nginx
1 root 0:00 nginx: master process nginx -g daemon off;
7 nginx 0:00 nginx: worker process
Could you confirm if the nginx.conf is correct?
It seems that the nginx process does not have permissions to write to the directory created.
ps -eo "%U %G %a" | grep nginx
Run the command above to learn the user. It is nginx in your case.
Change the owner and group for the log directory and reload the nginx service.
#create directory for logs
RUN mkdir -p /tmp/logs/error
RUN mkdir -p /tmp/logs/access && \
chown -R nginx:nginx /tmp/logs/
#adding footer file
ADD footer /usr/share/nginx/html/footer
Check the logs folder post accessing one of your URLs.
I currently try to use nginx as a proxy for elasticsearch engine, all with docker.
My run command for elasticsearch is the following :
docker run --name elasticsearch_5.2.1 \
-d \
elasticsearch:5.2.1
The one for nginx :
docker run --name nginx_1.11.10 \
-p 8200:80 \
-l elasticsearch_5.2.1:elasticsearch \
-v /my.conf:/etc/nginx/nginx.conf:ro \
-d \
nginx:1.11.10
And my nginx config is :
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
upstream elasticsearch_proxy {
server elasticsearch:9200;
}
server {
listen 80;
location / {
proxy_pass http://elasticsearch_proxy;
proxy_http_version 1.1;
proxy_set_header Connection "Keep-Alive";
proxy_set_header Proxy-Connection "Keep-Alive";
}
}
}
But, when nginx start, I have this error :
2017/03/01 23:45:47 [emerg] 1#1: host not found in upstream "elasticsearch:9200" in /etc/nginx/nginx.conf:15
nginx: [emerg] host not found in upstream "elasticsearch:9200" in /etc/nginx/nginx.conf:15
I understand that nginx can't found elasticsearch with his alias. But I can't find the problem.
Is there someone who already has this problem ?
Thank you.
You need to create a user network.
docker create network my_app
And then run both containers on that network.
docker run --name elasticsearch_5.2.1 \
-d --network my_app \
elasticsearch:5.2.1
docker run --name nginx_1.11.10 \
-p 8200:80 \
-l elasticsearch_5.2.1:elasticsearch \
--network my_app \
-v /my.conf:/etc/nginx/nginx.conf:ro \
-d \
nginx:1.11.10
Then you should be able to resolve names properly as if they were DNS names.
I am having a strange issue at the moment where when I browse to a port-forwarded URI (in this case http://localhost:9001) of a Vagrant box in my browser I get redirected back to localhost (default server). I'm not sure why this is happening and it's really frustrating.
On my guest machine I have the root folder hosted in /var/www/wordpress and this is my /nginx/sites-available/nginx_vhost file:
server {
listen 80;
listen [::]:80 ipv6only=on;
root /var/www/wordpress;
index index.php index.html index.htm;
server_name localhost;
location / {
# try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~* \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
#fastcgi_read_timeout 300;
#proxy_read_timeout 300;
}
}
Here is my Vagrant file:
Vagrant.configure("2") do |config|
# Set Vagrant box to use
config.vm.box = "ubuntu/trusty64"
# Configure port forwarding
config.vm.network :forwarded_port, guest: 80, host: 9001, auto_correct: true
# Set synched folder
config.vm.synced_folder "./", "/var/www/wordpress", create: true, group: "www-data", owner: "www-data"
# Configure the VM
config.vm.provider "virtualbox" do |v|
v.name = "Pet Vets Vagrant Box"
v.customize ["modifyvm", :id, "--memory", "1024"]
end
# Set up shell provisioning
config.vm.provision :shell, :path => "bootstrap.sh"
end
And my bootstrap.sh file:
#!/bin/bash
echo "Provisioning virtual machine..."
apt-get update
echo "Installing Tree..."
apt-get install tree
echo "Installing Git"
apt-get install git -y > /dev/null
echo "Installing Nginx"
apt-get install nginx -y >/dev/null
echo "Configuring Nginx"
cp /var/www/wordpress/nginx_vhost /etc/nginx/sites-available/nginx_vhost > /dev/null
ln -s /etc/nginx/sites-available/nginx_vhost /etc/nginx/sites-enabled/nginx_vhost
rm /etc/nginx/sites-available/default
service nginx restart > /dev/null
echo "Updating PHP repository"
apt-get install python-software-properties build-essential -y > /dev/null
add-apt-repository ppa:ondrej/php5 -y > /dev/null
apt-get update > /dev/null
echo "Installing PHP"
apt-get install php5-common php5-dev php5-cli php5-fpm libssh2-php -y > /dev/null
echo "Installing PHP extensions"
apt-get install curl php5-curl php5-gd php5-mcrypt php5-mysql -y > /dev/null
apt-get install libapache2-mod-php5
The Wordpress installation works fine when I host it on nGinx locally (not using Vagrant) but as soon as I place it in a Vagrant box it doesn't want to play. Does anyone have any ideas?
Thanks!
So the problem wasn't with nGinx it was with the Wordpress config.
I had siteurl and home in the wp_options table both set to http::localhost where it needed to be set to http://localhost:9001.
Hope this helps anyone in the future!
Thanks
I have taken over the development of a few websites and am currently trying to get one hosted within a Vagrant box. I am very familiar with Vagrant but am having a strange issue that I have been unable to fix since last Friday.
I have created the Vagrant file and MYSQL database for the Wordpress installation has been moved to my local (host) machine and I point to this from the Wordpress installation on the guest machine. All the Wordpress files exist and the folder is being shared with the guest machine.
My Vagrant file looks as follows:
Vagrant.configure("2") do |config|
# Set Vagrant box to use
config.vm.box = "ubuntu/trusty64"
# Configure port forwarding
config.vm.network :forwarded_port, guest: 80, host: 8930, auto_correct: true
# Set synched folder
config.vm.synced_folder "./", "/var/www", create: true, group: "www-data", owner: "www-data"
# Configure the VM
config.vm.provider "virtualbox" do |v|
v.name = "St. David's Lab"
v.customize ["modifyvm", :id, "--memory", "1024"]
end
# Set up shell provisioning
config.vm.provision :shell, :path => "bootstrap.sh"
end
The boostrap.sh file is used to setup the required software and similar on the guest machine and looks as follows:
#!/bin/bash
echo "Provisioning virtual machine..."
apt-get update
echo "Installing Git"
apt-get install git -y > /dev/null
echo "Installing Nginx"
apt-get install nginx -y >/dev/null
echo "Configuring Nginx"
cp /var/www/nginx_vhost /etc/nginx/sites-available/nginx_vhost > /dev/null
ln -s /etc/nginx/sites-available/nginx_vhost /etc/nginx/sites-enabled/nginx_vhost
rm -rf /etc/nginx/sites-available/default
service nginx restart > /dev/null
echo "Updating PHP repository"
apt-get install python-software-properties build-essential -y > /dev/null
add-apt-repository ppa:ondrej/php5 -y > /dev/null
apt-get update > /dev/null
echo "Installing PHP"
apt-get install php5-common php5-dev php5-cli php5-fpm -y > /dev/null
echo "Installing PHP extensions"
apt-get install curl php5-curl php5-gd php5-mcrypt php5-mysql -y > /dev/null
apt-get install libapache2-mod-php5
And here is the server config that gets created on the guest machine:
server {
listen 80;
server_name localhost;
root /var/www/;
index index.php index.html;
# Important for VirtualBox
sendfile off;
location / {
try_files $uri $uri/ =404;
}
location ~* \.php {
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_cache off;
fastcgi_index index.php;
}
}
I have changed the siteurl in the Wordpress database to localhost:8930 as well.
The issue I am having is that when I try and access the address localhost:8930 (as defined in the port forwarding in my Vagrant file) I get redirected back to localhost default index page (http://localhost). It is not a cache issue as I have cleared this, used an incognito window and replaced the index file with a simple "hello world" and it shows.
Can anyone see why this may be happening?
Thanks
The $uri/ clause of the try_files directive is causing an external redirect to the default port. You probably want to avoid external redirects from nginx itself, because it over complicates your 8930 port mapping rule.
One solution is to remove the index directive and the $uri/ clause.
You really need to add a default controller for WordPress anyway, something like:
location / {
try_files $uri /index.php?$args;
}
EDIT:
Detailed analysis of the problem:
You present the URL http://localhost:8930 which is presented to nginx as http://localhost:80. The location / processes the request. The try_files directive tests for the existence of a file and a directory. The presence of a directory causes an external redirect to http://localhost:80/. The external redirect is an undocumented side-effect of the $uri/ clause.
The try_files directive is documented here.