9 Aralık 2014 Salı

Nginx, Django, Gunicorn & Mysql Installation and Configuration on Debian 7

Nginx, Django, Gunicorn & Mysql Installation and Configuration

Step One;
Update packages
#apt-get update
#apt-get upgrade

Step Two;
Install and create virtualenv
#apt-get install python-virtualenv python-dev
#source virtualenv /opt/myenv
Notice that a new directory "myenv" was created in the "/opt" directory. This is where our virtualenv will live. Make sure to replace "/opt/myenv" with the path to where you want your virtualenv installed. I typically put my env's in /opt, but this is strictly preference. Some people create a directory named "webapps" at the root of the VPS. Choose whatever method makes the most sense to you.

Step Three ;
Install Django
#source /opt/myenv/bin/activate
You should now see that "(myenv)" has been appended to the beginning of your terminal prompt. This will help you to know when your virtualenv is active and which virtualenv is active should you have multiple virtualenv's on the VPS.
With your virtualenv active, we can now install Django. To do this, we will use pip, a Python package manager much like easy_install. Here is the command you will run:
(myenv)root@Django:/opt/myenv/bin#pip install django
You now have Django installed in your virtualenv! Now let's get our database server going.

Step Four ;
Install Mysql Server
Since we don't need our virtualenv active for this part, run the following command to deactivate:
(myenv)root@Django:/opt/myenv/bin#deactivate
This will always deactivate whatever virtualenv is active currently. Now we need to install dependencies for Mysql to work with Django with this command:
#apt-get install python-mysqldb libmysqlclient-dev mysql-server


Step Five;
Install Nginx
#apt-get install nginx

Step Six;
Install Gunicorn
Gunicorn is a very powerful Python WSGI HTTP Server. Since it is a Python package we need to first activate our virtualenv to install it. Here is how we do that:
#source /opt/myenv/bin/activate
Make sure you see the added "myenv" at the beginning of your terminal prompt. With your virtualenv now active, run this command:
(myenv)root@Django:/opt/myenv/bin# pip install gunicorn
Gunicorn is now installed within your virtualenv.
If all you wanted was to get everything installed, feel free to stop here. Otherwise, please continue for instructions on how to configure everything to work together and make your app accessible to others on the web.
(myenv)root@Django:/opt/myenv/bin#deactivate

Step Seven;
Configure Mysql
#mysql -u root -p 
>CREATE DATABASE djangodb;
>CREATE USER 'django'@'localhost' IDENTIFIED BY 'passwd';
>GRANT ALL PRIVILEGES ON djangodb . * TO 'django'@'localhost';
>FLUSH PRIVILEGES;

Step Eight;
Create a Django project
In order to go any further we need a Django project to test with. This will allow us to see if what we are doing is working or not. Change directories into the directory of your virtualenv (in my case /opt/myenv) like so:
#cd /opt/myenv
Now make sure your virtualenv is active. If you're unsure then just run the following command to ensure you're activated:
#source /opt/myenv/bin/activate
With your virtualenv now active, run the following command to start a new Django project:
(myenv)root@Django:/opt/myenv/bin#django-admin.py startproject myproject
You should see a new directory called "myproject" inside your virtualenv directory. This is where our new Django project files live.
In order for Django to be able to talk to our database we need to install a backend for Mysql. Make sure your virtualenv is active and run the following command in order to do this:
(myenv)root@Django:/opt/myenv/bin# pip install mysql-python
Change directories into the new "myproject" directory and then into it's subdirectory which is also called "myproject" like this:
(myenv)root@Django:/opt/myenv/bin#cd /opt/myenv/myproject/myproject
Edit the settings.py file with your editor of choice:
(myenv)root@Django:/opt/myenv/myproject/myproject#nano settings.py
Find the database settings and edit them to look like this:
DATABASES = {
        'default': {
'ENGINE': 'django.db.backends.mysql', 
'NAME': 'djangodb',
'USER': 'django',
'PASSWORD': 'passwd',
'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
'PORT': '3306',
  }
}
Save and exit the file. Now move up one directory so your in your main Django project directory 
#cd /opt/myenv/myproject
Activate your virtualenv if you haven't already with the following command:
#source /opt/myenv/bin/activate
With your virtualenv active, run the following command so that Django can add it's initial configuration and other tables to your database:
(myenv)root@Django:/opt/myenv/myproject/#python manage.py syncdb
You should see some output describing what tables were installed, followed by a prompt asking if you want to create a superuser. This is optional and depends on if you will be using Django's auth system or the Django admin.

Step Nine;
Configure Gunicorn
First lets just go over running Gunicorn with default settings. Here is the command to just run default 
#gunicorn_django --bind yourdomainorip.com:8001
Be sure to replace "yourdomainorip.com" with your domain, or the IP address of your VPS if you prefer. Now go to your web browser and visit yourdomainorip.com:8001 and see what you get. You should get the Django welcome screen.
If you look closely at the output from the above command however, you will notice only one Gunicorn worker booted. What if you are launching a large-scale application on a large VPS? Have no fear! All we need to do is modify the command a bit like so:
#gunicorn_django --workers=3 --bind yourdomainorip.com:8001
Now you will notice that 3 workers were booted instead of just 1 worker. You can change this number to whatever suits your needs.
Since we ran the command to start Gunicorn as root, Gunicorn is now running as root. What if you don't want that? Again, we can alter the command above slightly to accomodate:
#gunicorn_django --workers=3 --user=nobody --bind yourdomainorip.com:8001
If you want to set more options for Gunicorn, then it is best to set up a config file that you can call when running Gunicorn. This will result in a much shorter and easier to read/configure Gunicorn command.
You can place the configuration file for gunicorn anywhere you would like. For simplicity, we will place it in our virtualenv directory. Navigate to the directory of your virtualenv like so:
#cd /opt/myenv
Now open your config file with your preferred editor (nano is used in the example below):
#nano gunicorn_config.py
Add the following contents to the file:
command = '/opt/myenv/bin/gunicorn'
pythonpath = '/opt/myenv/myproject'
bind = '127.0.0.1:8001'
workers = 3
user = 'nobody'
Save and exit the file. What these options do is to set the path to the gunicorn binary, add your project directory to your Python path, set the domain and port to bind Gunicorn to, set the number of gunicorn workers and set the user Gunicorn will run as.
In order to run the server, this time we need a bit longer command. Enter the following command into your prompt:
#/opt/myenv/bin/gunicorn -c /opt/myenv/gunicorn_config.py myproject.wsg
You will notice that in the above command we pass the "-c" flag. This tells gunicorn that we have a config file we want to use, which we pass in just after the "-c" flag. Lastly, we pass in a Python dotted notation reference to our WSGI file so that Gunicorn knows where our WSGI file is.
Running Gunicorn this way requires that you either run Gunicorn in its own screen session (if you're familiar with using screen), or that you background the process by hitting "ctrl + z" and then typing "bg" and "enter" all right after running the Gunicorn command. This will background the process so it continues running even after your current session is closed. This also poses the problem of needing to manually start or restart Gunicorn should your VPS gets rebooted or were it to crash for some reason. To solve this problem, most people use supervisord to manage Gunicorn and start/restart it as needed. Installing and configuring supervisord has been covered in another article which can be found here.
Lastly, this is by no means an exhaustive list of configuration options for Gunicorn. Please read the Gunicorn documentation found at gunicorn.org for more on this topic.

Step Ten;
Configure Nginx
#service nginx restart
Since we are only setting NGINX to handle static files we need to first decide where our static files will be stored. Open your settings.py file for your Django project and edit the STATIC_ROOT line to look like this:
STATIC_ROOT = "/opt/myenv/static/" 
Add to /opt/myenv/myproject/myproject/settings.py
#nano /etc/nginx/sites-available/myproject
server {
        server_name yourdomainorip.com;

        access_log off;

        location /static/ {
            alias /opt/myenv/static/;
        }

        location / {
                proxy_pass http://127.0.0.1:8001;
                proxy_set_header X-Forwarded-Host $server_name;
                proxy_set_header X-Real-IP $remote_addr;
                add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
        }
    }
#cd /etc/nginx/sites-enabled
#ln -s ../sites-available/myproject
#rm default
#service nginx restart
And that's it! You now have Django installed and working with Mysql and your app is web accessible with NGINX serving static content and Gunicorn serving as your app server. If you have any questions or further advice, be sure to leave it in the comments section.








Hiç yorum yok:

Yorum Gönder