Rectangle 27 0

Python's standard way of publishing applications via web servers is WSGI--you can think of it as a Python's native CGI. uWSGI is a WSGI-compliant server that uses uwsgi protocol to talk to other uWSGI instances or upstream servers. Usually the upstream server is nginx with HttpUwsgiModule that allows it to communicate using uwsgi protocol--with nginx you have additional layer of protection for your app server, load balancing and serving the static files. In most scenarios, You Should Be Using Nginx + UWSGI. To answer your question, uWSGI is installed and run separately from nginx, and they both need to be configured to communicate to each other.

Pure WSGI is pretty low-level, so you may want to use a WSGI-compliant framework. I guess the top two are Django and Flask.

For a hello world Flask setup, Serving Flask With Nginx seems to be a good article.

python - difference between uwsgi module in nginx and uwsgi server - S...

python django nginx uwsgi
Rectangle 27 0

Install and Configure Nginx as a Reverse Proxy

With uWSGI configured and ready to go, we can now install and configure Nginx as our reverse proxy. This can be downloaded with yum by typing:

sudo yum install nginx

Once Nginx is installed, we can go ahead and modify the server block configuration. We will edit the main Nginx configuration file:

sudo nano /etc/nginx/nginx.conf

The web2py application detects whether you are connecting with plain HTTP or with SSL encryption. Because of this, our file will actually contain one server block for each. We will start with the server block that is configured to operate on port 80.

Change the server_name to reference the domain name or IP address where your site should be accessible. Afterwards, we will create a location {} block that will match requests for static content. Basically, we want to use regular expressions to match requests ending with /static/ with a preceding path component. We want to map these requests to the applications directory within our web2py project. Make sure you reference your user's home directory and app name:

Afterwards, we need to adjust the location / {} block to pass requests to our uWSGI socket. We just need to include the uWSGI parameter file packaged with Nginx and pass the requests to the socket we configured (in our uWSGI .ini file):

server {
    listen                  80 default_server;
    server_name             server_domain_or_IP;
    root                    /usr/share/nginx/html;

    include /etc/nginx/default.d/*.conf;

    location ~* /(\w+)/static/ {
        root /home/user/myapp/applications/;
    }

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/home/user/myapp/myapp.sock;
    }
}

    . . .

This will be all that we need for our first server block.

At the bottom of the file, but inside the closing http {} block bracket, make another server {} block. We will use this to configure SSL connections.

Start with the basics. This server block will listen for connections on port 443, the default SSL port. We will set the server's domain name or IP address, just as we did for the port 80 server block. To start the actual SSL configuration, we'll specify that SSL should be on for this block and we'll indicate the path to the SSL certificate and key that should be used to encrypt the connection. We'll be moving the files there momentarily:

We'll continue on with a bit of SSL boilerplate meant to establish the accepted protocols and ciphers. Afterwards, we can set up the same location / {} block we configured in the port 80 server block:

http {
    . . .
    server {
        listen 80;
        . . .
    }
    server {
        listen 443;
        server_name server_domain_or_IP;

        ssl on;
        ssl_certificate /etc/nginx/ssl/myapp.crt;
        ssl_certificate_key /etc/nginx/ssl/myapp.key;

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
        ssl_prefer_server_ciphers on;

        location / {
            include uwsgi_params;
            uwsgi_pass unix:/run/uwsgi/myapp.sock;
        }
    }
}

You should now have two server blocks configured in this file. Save and close it when you are finished.

Python Frameworks Deployment Nginx Python CentOS
Rectangle 27 0

Install and Configure uWSGI

Now that we have the web2py application operational, we can configure uWSGI. uWSGI is an application server that can communicate with applications over a standard interface called WSGI. To learn more about this, read this section of our guide on setting up uWSGI and Nginx on Ubuntu 14.04.

Deployment Python Frameworks Python Nginx Ubuntu
Rectangle 27 0

Install and Configure Nginx as a Reverse Proxy

With uWSGI configured and ready to go, we can now install and configure Nginx as our reverse proxy. This can be downloaded and installed using yum:

sudo yum install nginx

Once Nginx is installed, we can go ahead and edit the main configuration file:

sudo nano /etc/nginx/nginx.conf

Within this file, next to the existing server block, we will create an additional server block for each of our sites:

http {

    . . .

    include /etc/nginx/conf.d/*.conf;

    server {
    }

    server {
    }

    server {
        listen 80 default_server;
        server_name localhost;

        . . .

The blocks we created will hold the configuration for our uWSGI sites. We'll cover the directives that we need in the first server block now.

First, we need to tell the server block which port number and domain name that it should respond to. We'll assume that you have a domain name for each of your sites:

server {
    listen 80;
    server_name firstsite.com www.firstsite.com;
}

Next, we'll tell Nginx that we don't need to worry about a missing favicon. We will then specify the directory where our first site's static assets were collected for when these files are requested. Nginx can hand them straight to the client from that directory:

Next, we create a catch-all location block that will pass all additional queries straight to uWSGI. We will include the uwsgi parameters found in the /etc/nginx/uwsgi_params file and pass the traffic to the socket that the uWSGI server sets up:

With that, our first server block is complete.

The second server block for our other site will be almost the same. You can copy and paste the server block we just created to get started. You will need to modify the domain name that the site should respond to, the location of the site's static files, and the site's socket file:

server {
    listen 80;
    server_name secondsite.com www.secondsite.com;

    location = favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/user/secondsite;
    }

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/run/uwsgi/secondsite.sock;
    }
}

When you are finished with this step, save and close the file.

Check the syntax of the Nginx file to make sure you don't have any mistakes:

sudo nginx -t

If no errors are reported, our file is in good condition.

We have one additional task we have to complete to make our sites work correctly. Since Nginx is handling the static files directly, it needs access to the appropriate directories. We need to give it executable permissions for our home directory, which is the only permission bit it is lacking.

The safest way to do this is to add the Nginx user to our own user group. We can then add the executable permission to the group owners of our home directory, giving just enough access for Nginx to serve the files:

sudo usermod -a -G user nginx
chmod 710 /home/user

Now, we can start the Nginx server and the uWSGI process:

sudo systemctl start nginx
sudo systemctl start uwsgi

You should now be able to reach your two projects by going to their respective domain names. Both the public and administrative interfaces should work as expected.

If this goes well, you can enable both of the services to start automatically at boot by typing:

sudo systemctl enable nginx
sudo systemctl enable uwsgi
Deployment Django Nginx Python Frameworks Python CentOS
Rectangle 27 0

Introduction

In this guide, we will be setting up a simple WSGI application served by uWSGI. We will use the Nginx web server as a reverse proxy to the application server to provide more robust connection handling. We will be installing and configuring these components on a CentOS 7 server.

Python Frameworks Nginx Python CentOS
Rectangle 27 0

i recommend to use uwsgi for managing fcgi processes. it is capable of spawning a amount of worker-processes ready for input and restart them when they die. highly configurable and easy to install and use.

i have the options worker-exec, spooler, processes, protocol, enable-threads and master set.

go - Automatic respawn fastcgi process by nginx? - Stack Overflow

nginx go fcgi
Rectangle 27 0

Install and Configure uWSGI

Now that we have the web2py application operational, we can configure uWSGI. uWSGI is an application server that can communicate with applications over a standard interface called WSGI. To learn more about this, read this section of our guide on setting up uWSGI and Nginx on Ubuntu 14.04.

Python Frameworks Deployment Nginx Python CentOS
Rectangle 27 0

Install and Configure Nginx as a Reverse Proxy

With uWSGI configured and ready to go, we can now install and configure Nginx as our reverse proxy. This can be downloaded from Ubuntu's default repositories:

sudo apt-get install nginx

Once Nginx is installed, we can go ahead and create a server block configuration file for each of our projects. Start with the first project by creating a server block configuration file:

Inside, we can start our server block by indicating the port number and domain name where our first project should be accessible. We'll assume that you have a domain name for each:

server {
    listen 80;
    server_name firstsite.com www.firstsite.com;
}

Next, we can tell Nginx not to worry if it can't find a favicon. We will also point it to the location of our static files directory where we collected our site's static elements:

After that, we can use the uwsgi_pass directive to pass the traffic to our socket file. The socket file that we configured was called firstproject.sock and it was located in our project directory. We will use the include directive to include the necessary uwsgi parameters to handle the connection:

That is actually all the configuration we need. Save and close the file when you are finished.

We will use this as a basis for our second project's Nginx configuration file. Copy it over now:

sudo cp /etc/nginx/sites-available/firstsite /etc/nginx/sites-available/secondsite

Open the new file in your text editor:

sudo nano /etc/nginx/sites-available/secondsite

Here, you'll have to change any reference to firstsite with a reference to secondsite. You'll also have to modify the server_name so that your second project responds to a different domain name. When you are finished, it will look something like this:

server {
    listen 80;
    server_name secondsite.com www.secondsite.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/user/secondsite;
    }

    location / {
        include         uwsgi_params;
        uwsgi_pass      unix:/home/user/secondsite/secondsite.sock;
    }
}

Save and close the file when you are finished.

Next, link both of your new configuration files to Nginx's sites-enabled directory to enable them:

sudo ln -s /etc/nginx/sites-available/firstsite /etc/nginx/sites-enabled
sudo ln -s /etc/nginx/sites-available/secondsite /etc/nginx/sites-enabled

Check the configuration syntax by typing:

sudo service nginx configtest

If no syntax errors are detected, you can restart your Nginx service to load the new configuration:

sudo service nginx restart

If you remember from earlier, we never actually started the uWSGI server. Do that now by typing:

sudo service uwsgi start

You should now be able to reach your two projects by going to their respective domain names. Both the public and administrative interfaces should work as expected.

Nginx Deployment Django Python Python Frameworks Ubuntu
Rectangle 27 0

Introduction

In this guide, we will be setting up a simple WSGI application served by uWSGI. We will use the Nginx web server as a reverse proxy to the application server to provide more robust connection handling. We will be installing and configuring these components on an Ubuntu 14.04 server.

Python Frameworks Python Nginx Ubuntu
Rectangle 27 0

The following example might be useless for you, because it seems you installed uWSGI manually, instead of using system repository. But I think, you can easly find how uWSGI is configured on Ubuntu and make the same configuration on your system.

Here how I have done it on Ubuntu. I installed both uWSGI and nginx from Ubuntu repo, so I got the following dirs:

/etc/nginx/sites-available
/etc/nginx/sites-enabled
/etc/uwsgi/apps-available
/etc/uwsgi/apps-enabled

On /etc/uwsgi/apps-available I placed two files: app_a.ini and app_b.ini. There is no option socket (as well as pid and daemonize) in these files. uWSGI will detect socket, log, and pid file names using ini-file name. Then I created symlink to these files in /etc/uwsgi/apps-enabled to enable apps.

/etc/nginx/sites-available/default
enabled
upstream app_a {
    server unix:///run/uwsgi/app/app_a/socket;
}
upstream app_b {
    server unix:///run/uwsgi/app/app_b/socket;
}

server {
    listen 8000;

    location / {
        uwsgi_pass app_a;
        include uwsgi_params;
    }
}
server {
    listen 9000;

    location / {
        uwsgi_pass app_b;
        include uwsgi_params;
    }
}

Hi, @dmitry-vakhrushev thank you for your nice post. I still am not able to make it correctly. I have done what you said. I also restart the nginx and uwsgi??? When i now go to different sites, it gave me 502 Bad Gateway... Meanwhile, where can i find the log files for uwsgi?

Hi @dmitry-vakhrushev... you win. the website gives up by showing me the correct way that it is supposed to be. This is great. Thanks.

out of curiosity, when i did what you showed above, it seems like i have the permission issue. For instance, i used to have some codes (in the website) to write some contents to a file. But now it fails. Any thought?

It might happen because uWSGI runs behalf a system user (www-data is default one on Ubuntu).

python - nginx + uwsgi for multiple sites using multiple ports - Stack...

python nginx uwsgi
Rectangle 27 0

Install and Configure Nginx as a Reverse Proxy

With uWSGI configured and ready to go, we can now install and configure Nginx as our reverse proxy. This can be downloaded from Ubuntu's default repositories:

sudo apt-get install nginx

Once Nginx is installed, we can go ahead and modify the server block configuration. We will use the default server block as a base, since it has most of what we need:

sudo nano /etc/nginx/sites-available/default

The web2py application detects whether you are connecting with plain HTTP or with SSL encryption. Because of this, our file will actually contain one server block for each. We will start with the server block that is configured to operate on port 80.

Change the server_name to reference the domain name or IP address where your site should be accessible. Afterwards, we will create a location {} block that will match requests for static content. Basically, we want to use regular expressions to match requests ending with /static/ with a preceding path component. We want to map these requests to the applications directory within our web2py project. Make sure you reference your user's home directory and app name:

Afterwards, we need to adjust the location / {} block to pass requests to our uWSGI socket. We just need to include the uWSGI parameter file packaged with Nginx and pass the requests to the socket we configured (in our uWSGI .ini file):

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    server_name server_domain_or_IP;

    location ~* /(\w+)/static/ {
        root /home/user/myapp/applications/;
    }

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/home/user/myapp/myapp.sock;
    }
}

This will be all that we need for our first server block.

At the bottom of the file, there is a commented out section that has most of the directives needed to serve content with SSL. You can identify this block because it has listen 443; as the first directive. Uncomment this block to begin configuring it.

To start, change the server_name again to match your server's domain name or IP address. We can then jump to the ssl_certificate and ssl_certificate_key directives. We will be placing the SSL certificates we generated into a directory at /etc/nginx/ssl momentarily, so specify the path to the files at that location:

In the ssl_protocols list, remove SSLv3, as has been found to have vulnerabilities inherent in the protocol itself.

We can then jump to the location / {} block and put the same uWSGI proxying information we did in the last server block:

server {
    listen 443;
    server_name server_domain_or_IP;

    root html;
    index index.html index.htm;

    ssl on;
    ssl_certificate /etc/nginx/ssl/myapp.crt;
    ssl_certificate_key /etc/nginx/ssl/myapp.key;

    ssl_session_timeout 5m;

    #ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
    ssl_prefer_server_ciphers on;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/home/user/myapp/myapp.sock;
    }
}

You should now have two server blocks configured in this file. Save and close it when you are finished.

Deployment Python Frameworks Python Nginx Ubuntu