`
ning1988
  • 浏览: 7689 次
  • 性别: Icon_minigender_1
  • 来自: -
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

Setting up Django on SliceHost with Ubuntu Hardy,

阅读更多

This week I setup Django on a slicehost slice using Ubuntu Hardy for the OS , Postgresql for the db, Apache for serving the django app, and Nginx for serving the media and proxying to apache. For my future reference and hopefully to help out others here is the process I went through.

Slicehost has some easy to follow and straight forward articles to start I just followed along with the articles.

Setting up Ubuntu

Start with the Ubuntu Hardy setup - page 1 . You can follow this guide straight through. The only complicated part is when you do the “visudo” command to add yourself to the sudoers it puts you in the “vi” editor which isn’t very intuitive if you’re not use to it. To add your user to the list go to the end of the

root    ALL=(ALL) ALL
line and type “a” to start appending. Then hit return and enter the line as described in the article. Once the line is entered hit “esc” to enter back into command mode then type “ZZ ” to exit and save the file. If you want more info here is a reference to vi commands .

Then move onto page 2 Ubuntu Hardy setup -page 2 . Again just follow along with this article the only item I had to change was the locale setting as I’m in the US not the UK . As Cody mentioned in the comments for the article instead of entering:

sudo locale-gen en_GB.UTF-8
...
sudo /usr/sbin/update-locale LANG

=
en_GB.UTF
-8
I used the following:
sudo locale-gen en_US.UTF-8 
...
sudo /usr/sbin/update-locale LANG=en_US.UTF-8

Setting up DNS

At this point if you haven’t setup your dns you’ll to point to your slice you’ll want to do that within the slice admin interface. You’ll want at least two domains or subdomains one for the media files and one for the actual django application. In this case we are doing a “media.yourdomain.com” and “app.yourdomain.com”. If the django app is the website instead of the “app.yourdomain.com” you can setup a “www.yourdomain.com” and just use that anywhere we mention the “app.yourdomain.com”.

Installing Nginx

Currently Slicehost doesn’t have an article for installing Nginx on Ubuntu Hardy, but they do have one for Ubuntu Gutsy (the previous version of Ubuntu) which will work for our purposes. Follow along with the Ubuntu Gutsy - Installing Nginx from source . There is a newer version of Nginx now from when the article was written, so just go to the Nginx site and get the current stable version. I also wanted me config files in a different location than the default so in the article where it does the following command:

./configure --sbin-path=/usr/local/sbin --with-http_ssl_module
I did the following:
sudo mkdir /etc/nginx
./configure --sbin-path=/usr/local/sbin --with-http_ssl_module --conf-path=/etc/nginx/nginx.conf
This puts the config scripts in the /etc/nginx directory instead of the /usr/local/nginx/conf/ directory. This also more closely matches the setup you’d get if you installed nginx via aptitude and helprs for following along in later articles Once you’ve gone through the configure, make, install process edit the nginx.conf file
sudo nano /etc/nginx/nginx.conf
and change the following line in the http section:
include       /usr/local/nginx/conf/mime.types;
to be
include       /etc/nginx/mime.types;

At this point Nginx should be installed and working. Next we want to create the init script for it, and lucky for us Slicehost has an article covering this (again it’s for Ubuntu Gutsy, but it’ll work for Hardy) Ubuntu Gutsy - adding an nginx init script . No changes are needed for that article so just follow along.

Configuring Nginx

Follow along with the Slicehost article Ubuntu Gutsy - Nginx configuration #1 . There is only one slight difference we need to change for this article. As the article assumes Nginx was installed by aptitude and not via source. At the bottom of the article it talks about the “include”, but our conf file doesn’t have an include. To fix this add the following line just below the gzip settings within the /etc/nginx/nginx.conf file.

include /etc/nginx/sites-enabled/*;
Some of you may noticed that we don’t a “/etc/nginx/sites-enabled/” directory, so let’s create that and a “sites-available directory.
sudo mkdir /etc/nginx/sites-available
sudo mkdir /etc/nginx/sites-enabled
This sets it up so that any virtual host config files within the /etc/nginx/sites-enabled folder will automatically be included.

Setting up the Media Virtual Host

Now we need to setup some virtual hosts for our site. To start with right now we’ll just setup the “media” virtual host, we’ll come back to the virtual host for our django site later. So follow along with the Ubuntu Gutsy - Nginx Virtual Hosts . The only change here is where the article uses the “domain1.com” place holder we want to use “media” for the folder and config file names and then the domain of “media.yourdomain.com” where “yourdomain.com” is the web domain for your application. You don’t need to setup the domain2.com examples unless you just want to test/try it out.

Apache Install

Once again follow along with the Slicehost article Ubuntu Hardy - installing Apache and PHP5 . No changes are needed to this article, the only items is unless you need php for some of your sites you’ll want to stop when you reach the “PHP5 Install” part of the article.

Next follow along with the Ubuntu Hardy - Apache configuration #1 article. Change the default port from 80 to 8080

Listen 8080
I also commented out the listening on the ssl port as currently I don’t have SSL  configured.

Reducing the timeout is recommended, but you’ll want to keep in mind if you have any long running scripts to ensure they have enough time to complete. As we are only using Apache for serving the Django app and no static content we want to turn off Keep Alive off to improve performance.

KeepAlive Off

Go through the additional configuration in Ubuntu Hardy - Apache configuration #2

Django Install

Slicehost also has an article on installing Django Ubuntu Gutsy - Django installation . I highly recommend you install Django from trunk unless you have a specific need for using the older aptitude version. One addition to this article is after installing subversion edit the subversion conf

sudo nano /etc/subversion/config
to add “pyc” files to the global ignore list. Uncomment the following line and add the “*.pyc” onto the end. I also use “local_settings.py” to hold the settings I don’t want in subversion so I add that to the list as well.
### Set global-ignores to a set of whitespace-delimited globs
### which Subversion will ignore in its 'status' output, and
### while importing or adding files and directories.
global-ignores = *.o *.lo *.la #*# .*.rej *.rej .*~ *~ .#* .DS_Store *.pyc local_settings.py

After installing Django we’ll also want to add the admin media to our “media” site the easiest way to do this is with a symbolic link

ln -s /usr/lib/python2.5/site-packages/django/contrib/admin/media /home/demo/public_html/media/public/admin
(where “demo” is your user name)

Configuring Postgresql

In the above article Ubuntu Gutsy - Django installation . It took you through installing Postgres, but left it at that, we still need to setup some users, and create a database.

Setting up Postgres Users

The postgres install will create a “postgres” user by default that is the superuser for the database server. First thing we want to do is change the password for the “postgres” user this requires changing it within postgres and within the OS .

Changing the postgres users database password:

su postgres -c psql template1
template1=# ALTER USER postgres WITH PASSWORD 'password';
template1=\q
Where “password” is the new password.

Changing the postgres user OS password:

sudo passwd -d postgres
sudo su postgres -c passwd
...
It’s convenient to have the two passwords match, but not required.

Next create a postgres user for your login (this isn’t required but it can be convenient) at the command prompt enter:

sudo -u postgres createuser -P demo
where “demo” is your username. Answer the questions as it prompts you.

Now create a user for your django app to use for logging into the database. For this user don’t give it any of the super user, create db, create role, priveleges.

sudo -u postgres createuser -P django_login
where “django_login” is the username you’ll use to connect with from your django app. Remember the user name and password as we’ll use those in your django app’s settings.py file For more information on creating users you can refer to the Postgres documentation .

Create the Database

Now we need to create a database that our django app can use. Login into the psql interface

psql template1
Then at the psql prompt enter
CREATE DATABASE django_db OWNER django_login ENCODING 'UTF8';
This creates a database called “django_db” owned by “django_login” (change those values be what you want the database to be called and to the user you created in the previous step).

Configuring Access to the Postgres Server

Who can access the db server and from where is set in the /etc/postgresql/8.3/main/pg_hba.conf file, go ahead and open it up to edit. Postgres again has plenty of documentation on the file and how to set it up. For my case I only allow three users to access the database and all only from locally

local   all         postgres                          ident sameuser
local   all         demo                          ident sameuser
local   django_db    django_login                      md5
The first line you can leave as is as it lets the “postgres” user connect through the unix sockets. Then add the second line (again substituting your username for “demo”) so your user can access the system locally. Finally the last line is to allow your django application to access the system, where “django_db” is the name of the database you created in the step above and “django_user” is the user name you create earlier for your app to use.

Installing and Configuring your Django App

Create a directory for your Django site

mkdir -p /home/demo/public_html/app/{public,logs}
Upload or create your django project into the “/home/demo/public_html/app” folder. Also if you app depends on any other django applications or python libraries install those now as well.

Settings.py

Within your settings.py file you’ll need to set the database variables to the database info you created earlier. Here is an example using our example settings:

DATABASE_ENGINE = 'postgresql_psycopg2'           # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'ado_mssql'.
DATABASE_NAME = 'django_db'             # Or path to database file if using sqlite3.
DATABASE_USER = 'django_login'             # Not used with sqlite3.
DATABASE_PASSWORD = 'password'         # Not used with sqlite3.
DATABASE_HOST = ''             # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = ''             # Set to empty string for default. Not used with sqlite3.

Set the MEDIA_ROOT:

MEDIA_ROOT = '/home/demo/public_html/media/public/'
Set the MEDIA_URL:
MEDIA_URL = 'http://media.yourdomain.com'
Set the ADMIN_MEDIA_PREFIX:
ADMIN_MEDIA_PREFIX = 'http://media.yourdomain.com/admin/'

SyncDB and Test

At this point your Django app should be able to create the database and work (not from the web yet, but from the commandline. Go into your django project directory and enter the syncdb command.

./manage.py syncdb

At this point you should be able to go into the shell

./manage.py shell
and interact with your app.

Configuring the Web Servers for Django

Now that your app is installed and working it’s time to configure Apache and Nginx to server the django app.

Apache/Django Config

Create a virtual host file for the django apache configuration

sudo nano /etc/apache2/sites-available/app
In that file put the following config:
NameVirtualHost *
<virtualhost>
        ServerAdmin webmaster@yourdomain.com

        DocumentRoot /home/demo/public_html/app/public
        <directory>
                SetHandler python-program
                PythonPath "['/home/demo/public_html/app', '/home/demo/public_html/app/yourproject'] + sys.path"
                PythonHandler django.core.handlers.modpython
                SetEnv DJANGO_SETTINGS_MODULE yourproject.settings
                PythonDebug On
                Options FollowSymLinks
                AllowOverride None
        </directory>

        ErrorLog /home/demo/public_html/app/logs/apache_error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog /home/demo/public_html/app/logs/apache_access.log combined
        ServerSignature Off
</virtualhost>
Configuring it for your project. Enable this virtual site and restart apache.
sudo a2ensite app
sudo /etc/init.d/apache2 restart
Now the site should be running on apache port 8080 (which can’t be accessed outside the server unless you opened the port in the firewall).

Setting up Nginx Proxy

Create a virtual host config file for the site for Nginx

sudo nano /etc/nginx/sites-available/app
Put this into the file:
server {
  listen   80;
  server_name app.yourdomain.com;

  location / {
    proxy_pass      http://127.0.0.1:8080/;
    proxy_redirect  off;

    proxy_set_header   Host             $host;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_set_header        X-Magic-Header "secret";

    client_max_body_size       10m;
  }
}
Basically this is saying anything that comes to app.yourdomain.com:80 forward to the apache instance running locally on port 8080. Save the file and then add it to the enabled sites and restart nginx.
sudo ln -s /etc/nginx/sites-available/app /etc/nginx/sites-enabled/app
sudo /etc/init.d/nginx restart

All Done

That should be it, you should now be able to got to http://app.yourdomain.com/ and access your django project.

 

 

http://www.punteney.com/writes/setting-django-slicehost-ubuntu-hardy-postgres-apa/

分享到:
评论

相关推荐

    Ubuntu12.04 nginx python uwsgi Django安装步骤

    Ubuntu 12.04 下安装 Nginx、Python、uWSGI 和 Django 的步骤 在本文中,我们将介绍如何在 Ubuntu 12.04 环境下安装 Nginx、Python、uWSGI 和 Django。这些技术栈组合是非常流行的 Web 应用程序开发环境。 一、...

    Ubuntu系统Daphne + Nginx部署Python Django项目精讲【含代码说明】

    在本教程中,我们将深入探讨如何在Ubuntu系统上使用Daphne和Nginx部署Python Django项目,同时利用Supervisor进行进程管理。首先,Django 3.0开始支持ASGI(Asynchronous Server Gateway Interface)应用程序,使得...

    Ubuntu部署Django项目方法详解

    ### Ubuntu部署Django项目方法详解 #### 一、前言 本文将详细介绍如何在Ubuntu 18.04.1 LTS环境下部署一个基于Django 2.0的应用。本教程适用于那些希望在生产环境中部署Django项目的开发人员或运维工程师。我们将...

    Web Development with Django Cookbook 2nd 2016第2版 epub格式

    We'll start by setting up the virtual environment for a Django project and configuring it. Then you'll learn to write reusable pieces of code for your models and find out how to manage database ...

    Django-Ubuntu开发教程

    【Django-Ubuntu开发教程】是一份详尽的指南,旨在帮助开发者在Ubuntu操作系统上构建和部署Django项目。这份教程涵盖了从安装基础环境到完成服务器部署的全过程,非常适合那些希望快速上手Django开发并熟悉Linux环境...

    Beginning Django Web App Dev with Python

    Beginning Django Web App Dev with Python Beginning Django Web App Dev with Python

    ubuntu下django+apache+mod_wsgi部署

    ubuntu下django+apache+mod_wsgi部署。我的测试成功了!

    Django Web Development with Python.rar

    《Django Web Development with Python》是一本专注于使用Python语言进行Django框架开发的书籍,它提供了详尽的指导,帮助开发者构建高效、安全且可扩展的Web应用。Django是Python社区中最受欢迎的Web框架之一,以其...

    Python Web Development with Django.pdf

    《Python Web Development with Django》是专门讲解如何使用Python语言和Django框架进行Web开发的一本书。这本书属于“Developer’s Library”系列,该系列为程序员提供高质量的编程参考书籍和教程。该系列书籍由...

    ubuntu下的虚拟环境中安装Django的操作方法

    在ubuntu的命令行窗口中进行如下操作: 1、安装虚拟环境 sudo pip install virtualenv 2、创建虚拟环境 mkvirtualenv 文件名 -p python3(这是python版本) 有些朋友对“mkvirtualenv 文件名” 有疑问,这个是默认...

    Django for beginners learn web development with Django 2.0

    本书《Django for beginners learn web development with Django 2.0》面向对Web开发感兴趣的初学者,旨在通过项目驱动的方式教授使用Django 2.0框架开发Web应用程序的技能,强调使用Python 3.x版本。虽然本书是英文...

    Web Development with Django Cookbook, Second Edition

    《Web Development with Django Cookbook, Second Edition》是一本深入探讨使用Django框架进行Web开发的实战指南。这本书的第二版更新了最新的Django版本知识,旨在帮助开发者高效地解决在构建Web应用过程中遇到的...

    Python Web Development With Django

    ### Python Web Development With Django #### 一、书籍概述与定位 本书《Python Web Development With Django》是一本关于使用Python和Django框架进行Web开发的专业书籍。与其他市场上已有的Django书籍相比,本书...

    Django框架实现在线考试系统

    在本项目中,我们将深入探讨如何使用Python的Django框架构建一个在线考试系统。这个系统是为大学课程设计的,旨在提供一个平台,让学生能够在线进行考试,教师可以发布、管理试题,同时系统还能自动评分。为了实现这...

    Web Development with Django Cookbook

    Web Development with Django Cookbook 英文版 Web Development with Django Cookbook 英文版 Web Development with Django Cookbook 英文版

    Ubuntu系统搭建django+nginx+uwsgi的教程详解

    主要介绍了Ubuntu系统搭建django+nginx+uwsgi的思路详解,本文分步骤给大家介绍的非常详细,具有一定的参考借鉴价值 ,需要的朋友可以参考下

    在windows及ubuntu下安装django

    标题中的“在Windows及Ubuntu下安装Django”指的是在两种不同的操作系统环境下,即Microsoft Windows和Ubuntu Linux上安装Python的Web框架Django的过程。Django是一个功能强大的、免费的开源框架,用于快速开发安全...

    Web Development with Django Cookbook (2nd)

    Web Development with Django Cookbook - Second Edition will guide you through all the web development process with Django 1.8 framework. You will get started with the virtual environment and ...

    Django 项目setting默认配置

    其实就是把不同功能的文件放到不同目录下,然后通过代码代用将各个模块组合起来。这样的好处就是松耦合。具体各模块的作用通过创建工程来介绍

    tango_with_django-master源代码

    《深入剖析"Tango with Django"源代码》 "Tango with Django"是一份针对Django框架的教程项目,其源代码存放在名为“tango_with_django-master”的压缩包中。这个项目旨在帮助开发者通过实践来学习Django,理解其...

Global site tag (gtag.js) - Google Analytics