Linux下创建Django网站 步骤详解

博客新开,以后争取写更多原创文章。

先整理一篇曾经搭建Django网站的资料,欢迎大家批评指正。 (本文部分原创,参考了众多前辈资料)



1. Download django for linux:

   Official link: 

      http://www.djangoproject.com

   Command:   

     wget –no-check-certificate –c http://www.djangoproject.com/download/1.3.1/tarball/

 

 

2. Download Python code

   Official link:

       Theofficial website is often unavailable, so we use the following ftp link:

      http://www.python.org/ftp/python/

  Command:

      wget –c “http://www.python.org/ftp/python/2.7.2/Python-2.7.2.tar.bz2”

3. Download Mysql:

  Official link:

    http://dev.mysql.com/

  Command:

     #  wget –c “http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.17.tar.gz/from/http://sunsite.informatik.rwth-aachen.de/mysql/”

4. To install Mysql 5.5 with the source code,cmake is needed for Linux system.

4.1 Downloadcmake:

 Official link: 

    http://www.cmake.org/cmake/resources/software.html

 Command:

    wget –c “http://www.cmake.org/files/v2.8/cmake-2.8.6.tar.gz“

4.2 Install cmakefirstly:

  1. # tar zxvf cmake-2.8.4.tar.gz    
  2. # cd cmake-2.8.4    
  3. # ./configure    
  4. # make    
  5. # make install  

5. Install MySQL:

5.1 编译MySQL

  1. #tar zxvf mysql-5.5.13.tar.gz  
  2. #cd mysql-5.5.13  
  3. #rm CMakeCache.txt  
  4. #cmake .   -DCMAKE_INSTALL_PREFIX=/usr/local/mysql55/        -DMYSQL_DATADIR=/usr/local/mysql55/data        -DWITH_INNOBASE_STORAGE_ENGINE=1        -DMYSQL_TCP_PORT=3306        -DMYSQL_UNIX_ADDR=/usr/local/mysql55/data/mysql.sock      -DMYSQL_USER=mysql       -DWITH_DEBUG=0  
  5. #make   
  6. #make install  

5.2 安装和初始化mysql

          ./bin/mysql_install_db    --defaults-file=/usr/local/mysql/my.cnf      --user=Mysql

          说明:必须用参数--defaults-file指定my.cnf,否则系统用默认的/etc/my.cnf.

6.启动mysql:

  1. # bin/mysqld_safe --user=mysql &    
  2. # #启动mysql,看是否成功    
  3. # netstat -tnl|grep 3306  
  4. # 或者  
  5. # ps -aux|grep 'mysql'  

7.配置自启动

  1. # cp support-files/mysql.server /etc/init.d/mysqld  
  2. # chmod +x /etc/init.d/mysqld  
  3. # chkconfig –add mysqld  

      或者这样启动:  

  1. # #将mysql的启动服务添加到系统服务中    
  2. # cp support-files/mysql.server /etc/init.d/mysql.server    
  3. # #现在可以使用下面的命令启动mysql    
  4. # service mysql.server start    
  5. # #停止mysql服务    
  6. # service mysql.server stop    
  7. # #重启mysql服务    
  8. # service mysql.server restart   

7.1 MySQL使用问题一

今天在用一个装好的Mysql时,用safe_mysqldq启动的时候,出现

Fatal error: Can't open and lock privilege tables: Table'mysql.host' doesn't exist

最终解决方法如下:

在mysql的安装目录下,我的是/usr/local/mysql

./scripts/mysql_install_db   --usrer=mysql --datadir=/usr/local/mysql/data/

原因是重装的时候数据目录不一致导致

然后再次启动,OK

7.2 MySQL使用问题二:

[root@localhost software]# cd mysql-5.5.11
[root@localhost mysql-5.5.11]#cmake .
-- Could NOT findCurses (missing:  CURSES_LIBRARYCURSES_INCLUDE_PATH)
CMake Error at cmake/readline.cmake:82 (MESSAGE):
  Curses library not found.  Please installappropriate package,
      remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu,package name is libncurses5-dev, on Redhat and derivates it is ncurses-devel.
Call Stack (most recent call first):
  cmake/readline.cmake:126 (FIND_CURSES)
  cmake/readline.cmake:216 (MYSQL_USE_BUNDLED_LIBEDIT)
  CMakeLists.txt:250 (MYSQL_CHECK_READLINE)
-- Configuring incomplete, errors occurred!

解决办法:
[root@localhost mysql-5.5.11]#rm CMakeCache.txt
[root@localhost mysql-5.5.11]#yum install ncurses-devel

7.3 MySQL使用问题三: linux创建新用户出错Creating mailbox file:File exists

删除用户;  userdel –r mysql

安装mysql之后,如果没有mysql user/group, 先按如下顺序创建好:

Groupadd mysql

Useradd –g mysql mysql

8. 编译和安装 Python:

1.     bzip2 –d Python-2.7.2.tar.bz2;

2.     tar –xvf Python-2.7.2.tar;

3.     cd Python-2.7.2

4.     ./configure–prefix=/usr/local/python

5.     make

6.     make install

安装Python-2.7.5
执行./configure时报错:
configure: error: no acceptable C compiler found in $PATH
***********************************************************

查看得知未安装合适的编译器。

sudoyum install gcc-c++
(使用sudo yum install gcc-c++时会自动安装/升级gcc及其他依赖的包。)

9.  解压以及安装apache

#tar –zxvf httpd-2.2.4.tar.gz
#tar –zxvf mysql-5.0.19.tar.gz
#cd httpd-2.2.4
#./configure --prefix=/usr/local/apache --enable-so --enable-rewrite --enable-deflate--with-mpm=worker
#make && make install
#make clean
#cd

启动服务:
#/usr/local/apache/bin/apachectl –k start
然后在地址栏里面输入:本机IP/index.html如果可以看到“Itworks!”就说明apache已经安装成功了

9.1 Apache问题1:

#./configure--prefix……检查编辑环境时出现:

checkingfor APR... no
configure: error: APR not found .  Please read the documentation

解决办法:

1.下载所需软件包:

1.  wget http://archive.apache.org/dist/apr/apr-1.4.5.tar.gz 

2.  wget http://archive.apache.org/dist/apr/apr-util-1.3.12.tar.gz 

3.  wget http://jaist.dl.sourceforge.net/project/pcre/pcre/8.10/pcre-8.10.zip

yum install apr apr-util apr-devel apr-util-devel pcrepcre-devel

问题2: configure error: ..No recognized SSL/TLS toolkit detected

Solution: yum install openssl-devel

(Notice: Better to use httpd.2.2.* rather than httpd.2.4.*, to avoid configureerror when compiling mod_wsgi )

9.2 Apache 问题2: 如何配置httpd.conf

更详细内容参见:http://wenku.baidu.com/link?url=HH8Cf6uAuQ1ZAT3OKGc6w5rs39wdmkYutlc03nskJvQia9oa91lbNqg1GvTN-czSZmwBht3fj0-HhClS9Cw0ES7fIQLf0AQ6m0JEfTV1ANm

在apache的htdocs目录下创建django project:  jsky_site

编辑  jsky_site/jsky_site/settings.py, 添加数据库;

编辑apache/conf/httpd.conf , 添加如下内容:

在http.conf文件中加入以下几行命令:

WSGIScriptAlias / "your_dir/jsky_site/jsky_site/wsgi.py"
 
<Directory "your_dir/jsky_site/jsky_site">

Order Deny,Allow

Allow from all

</Directory>

9.3 问题3:打开浏览器访问基于apache的新网站,有如下error:

ImportError: Could not import settings ‘jsky_site.settings’ ( is iton sys.path? Is there an import error in the seetings file?): No module namedjsky_site.settings

Solution: 因为django的project里自动生成了wsgi.py文件,但是文件里需要加上

sys.path.append('your_dir/jsky_site')

10. Install mysql-python:

Download Link:  http://sourceforge.net/projects/mysql-python/

Install Directory:

# tar -zxvf MySQL-python-1.2.3.tar.gz

# cd MySQL-python-1.2.3

# whereis mysql_config ( 该程序在自己安装的mysqlyinstall目录下的bin目录)

mysql_config: /usr/bin/mysql_config /usr/share/man/man1/mysql_config.1.gz

# vi site.cfg ( site.cfg是mysql-python的安装(源)目录下)

threadsafe = False

mysql_config = /usr/bin/mysql_config

# whereis mysql

mysql: /usr/bin/mysql /usr/lib/mysql /usr/include/mysql/usr/share/mysql /usr/share/man/man1/mysql.1.gz

# export LD_LIBRARY_PATH=/usr/include/mysql

# python( your own python ) setup.py build

# python( your own python ) setup.py install

注意: 1.编译mysql-python需要mysql头文件。

         2.安装mysql-python需要setuptools工具。

Mysql_python安装参考文档:  http://www.cnzqs.com/?p=87

10.1 mysql-python 问题1:

安装mysql-python卡住 fatal error: Python.h  

Solution:   yum install python-devel

10.2 问题2: ImportError:libmysqlclient_r.so.18: cannot open shared object file: No such file ordirectory

Solution:找到mysql安装目录下的lib/libmysqlclient.so.18.0.0 ,建立软连接:
ln –s /home/mysql/mysql-5.5.18/lib/libmysqlclient.so.18/usr/lib/libmysqlclient.so.18

11. Download/Install mod_python:

 # wget http://archive.apache.org/dist/httpd/modpython/mod_python-3.3.1.tgz

 # gzip –d mod_python-3.3.1.tgz

 #  tar –xvf mod_python-3.3.1.tar

 # cd mod_python-3.3.1

 # ./configure  --with-apxs=/usr/local/support/apache2/bin/apxs

 # make

11.1      编译mod_python不通过,  

·     报错:connobject.c:142: error: request for member `next' in something not a structure or union   

·     解决:  

·     1、打开源码,找到:connobject.c的142行  

·     2、把!(b == APR_BRIGADE_SENTINEL(b) || 改为!(b == APR_BRIGADE_SENTINEL(bb) ||  

·     3、再次make clean; make ,解决。  

·     make  install # 安装完成  

·     为Apache的http.conf 添加一行:  

·     LoadModule python_module modules/mod_python.so 

11.2 可以用 mod_wsgi 来替换 mod_python:

# Tar –xzvf modwsgi-3.4.tar.gz

# Cd mod_wsgi-3.4

# ./configure –with-apxs  /usr/local/httpd/bin/apxs  --with-python /usr/local/python/bin/python

# Make

# Make install

12 Install Django:

  1.     # wget  http://www.djangoproject.com/download/1.2.5/tarball/  
  2.     # tar xfz Django-1.2.5.tar.gz  
  3.     # cd Django-1.2.5  
  4.     # python2.5   setup.py  install   # 安装   
  5.     # python2.5   
  6.     # >>> import django  
  7.     # >>>  

13 创建第一个Hello World网站

 · 下面全部在命令行中执行:  

· # mkdir /htdocs  

· # cd /htdocs  

· # /usr/local/support/python/bin/django-admin.py  startproject myproject  

· # cd myproject  

· # python manager.py runserver 0.0.0.0:8000  

· 好了,现在访问 http://服务器IP:8000,看到“It works”了吧 ^_^  

· 正常后,Ctrl + C退出来,刚才那个是Django自带的测试服务器,下面在Apache上开通虚拟主机啦。

13.1 问题1:使用Django新建项目仍然报错:nomodule named zlib

查找得知系统缺少zlib-devel包
安装zlib-devel包
shell>sudo yum install zlib-devel

重新编译安装Python
shell>./configure --with-zlib
shell>make
shell>sudo make install

13.2 问题2: 紧接问题1,recompilepython时提示没有找到_sqlite3, _tkinter, bsddb185, bz2, gdbmreadline:

Solution: yuminstall  bz2-devel  gdbm-develreadline-devel  sqlite2-devel, 其他没找到

Sqlite2-devel不安装无法使用 django默认的数据库

 


IT人的微信自媒体--- 杰天空, 走在寻找创意的路上

发掘创意,点缀生活,品味人生。
请搜索微信订阅号:
jksy_studio ,或者微信扫描头像二维码添加关注


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值