- 浏览: 7645 次
- 性别:
- 来自: -
最近访客 更多访客>>
文章分类
最新评论
-
larva:
我的还是可以
http://www.payvv.com
ht ...
彻底对Google App Engine失去信心 -
duka:
不错,收藏了,很有应用价值。
django 的 contribs 之 contenttype -
xypcn:
同 感
彻底对Google App Engine失去信心
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
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
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
sudo mkdir /etc/nginx ./configure --sbin-path=/usr/local/sbin --with-http_ssl_module --conf-path=/etc/nginx/nginx.conf
sudo nano /etc/nginx/nginx.conf
include /usr/local/nginx/conf/mime.types;
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/*;
sudo mkdir /etc/nginx/sites-available sudo mkdir /etc/nginx/sites-enabled
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
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
### 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
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
Changing the postgres user OS password:
sudo passwd -d postgres sudo su postgres -c passwd ...
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
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
Create the Database
Now we need to create a database that our django app can use. Login into the psql interface
psql template1
CREATE DATABASE django_db OWNER django_login ENCODING 'UTF8';
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
Installing and Configuring your Django App
Create a directory for your Django site
mkdir -p /home/demo/public_html/app/{public,logs}
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/'
MEDIA_URL = 'http://media.yourdomain.com'
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
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
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>
sudo a2ensite app sudo /etc/init.d/apache2 restart
Setting up Nginx Proxy
Create a virtual host config file for the site for Nginx
sudo nano /etc/nginx/sites-available/app
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; } }
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/
相关推荐
Ubuntu 12.04 下安装 Nginx、Python、uWSGI 和 Django 的步骤 在本文中,我们将介绍如何在 Ubuntu 12.04 环境下安装 Nginx、Python、uWSGI 和 Django。这些技术栈组合是非常流行的 Web 应用程序开发环境。 一、...
在本教程中,我们将深入探讨如何在Ubuntu系统上使用Daphne和Nginx部署Python Django项目,同时利用Supervisor进行进程管理。首先,Django 3.0开始支持ASGI(Asynchronous Server Gateway Interface)应用程序,使得...
### Ubuntu部署Django项目方法详解 #### 一、前言 本文将详细介绍如何在Ubuntu 18.04.1 LTS环境下部署一个基于Django 2.0的应用。本教程适用于那些希望在生产环境中部署Django项目的开发人员或运维工程师。我们将...
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开发教程】是一份详尽的指南,旨在帮助开发者在Ubuntu操作系统上构建和部署Django项目。这份教程涵盖了从安装基础环境到完成服务器部署的全过程,非常适合那些希望快速上手Django开发并熟悉Linux环境...
Beginning Django Web App Dev with Python Beginning Django Web App Dev with Python
ubuntu下django+apache+mod_wsgi部署。我的测试成功了!
《Django Web Development with Python》是一本专注于使用Python语言进行Django框架开发的书籍,它提供了详尽的指导,帮助开发者构建高效、安全且可扩展的Web应用。Django是Python社区中最受欢迎的Web框架之一,以其...
《Python Web Development with Django》是专门讲解如何使用Python语言和Django框架进行Web开发的一本书。这本书属于“Developer’s Library”系列,该系列为程序员提供高质量的编程参考书籍和教程。该系列书籍由...
在ubuntu的命令行窗口中进行如下操作: 1、安装虚拟环境 sudo pip install virtualenv 2、创建虚拟环境 mkvirtualenv 文件名 -p python3(这是python版本) 有些朋友对“mkvirtualenv 文件名” 有疑问,这个是默认...
本书《Django for beginners learn web development with Django 2.0》面向对Web开发感兴趣的初学者,旨在通过项目驱动的方式教授使用Django 2.0框架开发Web应用程序的技能,强调使用Python 3.x版本。虽然本书是英文...
### Python Web Development With Django #### 一、书籍概述与定位 本书《Python Web Development With Django》是一本关于使用Python和Django框架进行Web开发的专业书籍。与其他市场上已有的Django书籍相比,本书...
在本项目中,我们将深入探讨如何使用Python的Django框架构建一个在线考试系统。这个系统是为大学课程设计的,旨在提供一个平台,让学生能够在线进行考试,教师可以发布、管理试题,同时系统还能自动评分。为了实现这...
Web Development with Django Cookbook 英文版 Web Development with Django Cookbook 英文版 Web Development with Django Cookbook 英文版
主要介绍了Ubuntu系统搭建django+nginx+uwsgi的思路详解,本文分步骤给大家介绍的非常详细,具有一定的参考借鉴价值 ,需要的朋友可以参考下
标题中的“在Windows及Ubuntu下安装Django”指的是在两种不同的操作系统环境下,即Microsoft Windows和Ubuntu Linux上安装Python的Web框架Django的过程。Django是一个功能强大的、免费的开源框架,用于快速开发安全...
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 ...
其实就是把不同功能的文件放到不同目录下,然后通过代码代用将各个模块组合起来。这样的好处就是松耦合。具体各模块的作用通过创建工程来介绍
《Web Development with Django Cookbook, Second Edition》是一本深入探讨使用Django框架进行Web开发的实战指南。这本书的第二版更新了最新的Django版本知识,旨在帮助开发者高效地解决在构建Web应用过程中遇到的...
《深入剖析"Tango with Django"源代码》 "Tango with Django"是一份针对Django框架的教程项目,其源代码存放在名为“tango_with_django-master”的压缩包中。这个项目旨在帮助开发者通过实践来学习Django,理解其...