Linux+Python+Django+Nginx+FastCGI+AMF 安裝實記
使環境變數生效(或重啟作業系統)
# export PGDATA=/usr/local/pgsql-8.4/data
# touch /var/log/pgsql.log
# chown postgresql.postgresql /var/log/pgsql.log
修改配置檔案,開通網路功能(postgresql)
# su postgresql
# cd /usr/local/pgsql-8.4/data 修改配置使監聽生效,取消以下兩行的註釋
# vim postgresql.conf
listen_addresses = '*' # what IP address(es) to listen on;
port = 5432 # (change requires restart)
password_encryption = on
# vim pg_hba.conf 增加以下兩行:
host all all 10.0.6.0/24 password
host all all 10.0.0.0/24 password啟動資料庫(postgresql)
# cd /usr/local/pgsql-8.4/bin
# ./postmaster -i -D /usr/local/pgsql-8.4/data > /var/log/pgsql.log 2>&1 &
# cat /var/log/psql.log
LOG: checkpoint record is at 0/42C424
LOG: redo record is at 0/42C424; undo record is at 0/0; shutdown TRUE
LOG: next transaction ID: 0/593; next OID: 10820
LOG: next MultiXactId: 1; next MultiXactOffset: 0
LOG: database system is ready建立使用者
# ./createuser -a -d -P sqluser(注意大小寫,sqluser是使用者名稱)
Enter password for new role: 123456
Enter it again: 123456
Shall the new role be allowed to create more new roles? (y/n) y
CREATE ROLE建立資料庫
# ./-O sqluser mydatabase
sudo sqluser mydatabase
CREATE DATABASE配置資料庫自啟動指令碼(root)
# cd /etc/rc.d/init.d
# cp (第一步解壓的安裝檔案目錄)/postgresql-8.4.3/contrib/start-script/linux postgresql
# chmod +x postgresql
# vim postgresql
prefix=/usr/local/pgsql-8.4
PGDATA="/usr/local/pgsql-8.4/data"
PGUSER=postgresql
PGLOG="/var/log/pgsql.log"
# chkconfig --add postgresql
重啟資料庫
# /etc/rc.d/init.d/postgresql restart
六、安裝資料庫驅動 psycopg2-2.0.14.tar.gz
# tar zxvf psycopg2-2.0.14.tar.gz
# cd psycopg2-2.0.14
# python setup.py install
修改如下檔案:
# vim /etc/profile
在 unset i前面新增環境變數
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/pgsql-8.4/lib
應用配置:
# source /etc/profile
七、安裝 Django-1.2.1.tar.gz
# tar xzvf Django-1.2.1.tar.gz
# cd Django-1.2.1
# sudo python setup.py install
配置Django環境變數
# ln -s /usr/local/python26/bin/django-admin.py /usr/bin/django-admin.py
建立專案
# cd /var/www
# django-admin.py startproject <專案名稱>
# cd t01
# django-admin.py startapp <App名稱>
建立靜態資料夾
# mkdir templates
建立錯誤日誌檔案
# mkdir /var/log/django
# vim /var/log/django/access.out.log
# vim /var/log/django/error.log
建立Django重啟指令碼:
#vim /etc/init.d/django
新增如下內容:
#!/bin/bash
# Replace these three settings.
PROJDIR="/var/www/t01"
PIDFILE="/var/run/django.pid"
SOCKET="/tmp/django.sock"
OUTLOGFILE="/var/log/django/access.out.log"
ERRLOGFILE="/var/log/django/error.log"
cd $PROJDIR
if [ -f $PIDFILE ]; then
kill `cat -- $PIDFILE`
rm -f -- $PIDFILE
fi
exec python manage.py runfcgi maxchildren=10 maxspare=5 minspare=5 pidfile=$PIDFILE host=127.0.0.1 port=8000 method=prefork outlog=$OUTLOGFILE errlog=$ERRLOGFILE
#exec python manage.py runfcgi maxchildren=20 maxspare=20 minspare=15 pidfile=$PIDFILE socket=$SOCKET method=prefork umask=777
#chmod 777 $SOCKET
修改django檔案的許可權
# chmod 777 /etc/init.d/django
建立開機啟動Django指令碼
# vim /etc/rc.local
在末尾新增:
/etc/init.d/django
八、安裝 nginx-0.8.34.tar.gz
# tar xzvf nginx-0.8.34.tar.gz
# cd nginx-0.8.34
# ./configure --prefix=/usr/local/nginx-0.8.34
# make
# make install
配置Nginx環境變數
# ln -s /usr/local/nginx-0.8.34/sbin/nginx /usr/bin/nginx
建立錯誤日誌檔案
# mkdir /var/log/nginx
# vim /var/log/nginx/error_log
修改Nginx配置檔案:
# vim /usr/local/nginx-0.8.34/conf/nginx.conf
修改內容如下:
#user www-data;
worker_processes 2;
error_log /var/log/nginx/error_log info;
events {
worker_connections 1024;
use epoll;
}
http {
include /usr/local/nginx-0.8.34/conf/mime.types;
default_type application/octet-stream;
log_format main
'$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$gzip_ratio"';
client_header_timeout 10m;
client_body_timeout 10m;
send_timeout 10m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 4 2k;
request_pool_size 4k;
gzip on;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_types text/plain;
output_buffers 1 32k;
postpone_output 1460;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 75 20;
ignore_invalid_headers on;
index index.html;
server {
listen 80;
server_name localhost;
access_log /usr/local/nginx-0.8.34/logs/access.log;
location ^~ /media/ {
alias /var/www/t01/media;
}
location ~* ^.+\.(gif|png|jpg|jpeg|css|swf|htm|html|asp|php|jsp|js|doc|txt)$ {
root /var/www/t01/templates;
#access_log off;
}
location / {
# host and port to fastcgi server
fastcgi_pass 127.0.0.1:8000;
#fastcgi_pass unix:/tmp/django.sock;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_pass_header Authorization;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_intercept_errors off;
}
}
}
啟動Nginx:
# nginx
新增Nginx開機啟動指令碼:
# vim /etc/rc.local
在末尾新增 nginx