1,python web部署的實現:
python+uwsgi+nginx實現web。
本文測試環境:
服務器:樹莓派B+
操作系統:
root@node100:~# cat /etc/issue Raspbian GNU/Linux 7 \n \l
IP:
root@node100:~# ip ad s 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether b8:27:eb:ed:b8:e5 brd ff:ff:ff:ff:ff:ff inet 192.168.137.100/24 brd 192.168.137.255 scope global eth0 valid_lft forever preferred_lft forever
軟件需求:
python2.7.3
django-1.7.2
rpyc-3.3.0
uwsgi-2.0.9
nginx-1.6.2
mysql-5.5.40
都是目前穩定版本最新版本。
2,創建相應目錄:
root@node100:~# mkdir /data root@node100:~# mkdir /data/logs #所有日誌存放位置 root@node100:~# mkdir /data/www #頁面代碼 root@node100:~# mkdir /data/server #軟件安裝位置,後面nginx貌似裝到/data下面了,這個目錄就沒用了。 root@node100:~# mkdir /soft #下載軟包位置
3,安裝必須的軟件包
# apt-get update#更新下系統 安裝相應的庫文件,有些系統以及安裝了就不用重新安裝了: # apt-get install libpcre3-dev libssl-dev libpcre3 libssl build-essential zlib1g zlib1g-dev build-essential #可以解決包間的依賴關係等,很重要,raspbian默認安裝。
----------------------- 安裝nginx --------------------------
下載軟件包:
# cd /soft/ # wget http://nginx.org/download/nginx-1.6.2.tar.gz
創建nginx運行用戶和組:
# groupadd -g 5000 www # useradd -u 5000 -g 5000 www # tar -zxf nginx-1.6.2.tar.gz # cd nginx-1.6.2/ # ./configure --prefix=/data/nginx --user=www --group=www --with-http_stub_status_module ## --with-http_stub_status_module #啓用Nginx狀態監控模塊,默認不開啓。 # make # make install
----------------------- 安裝mysql --------------------------
# aptitude search mysql|grep ^i#查看系統中是否有mysql相應包,有則刪除 # apt-get remove mysqlxxx#刪除系統自帶mysql相關包 # apt-get install mysql-server mysql-client python-mysqldb(python鏈接mysql支持包)#默認會安裝許多依賴包,並且會讓你設置mysql root賬號密碼。
默認安裝完成自動啓動,測試:
# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 43 Server version: 5.5.40-0+wheezy1 (Debian) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 3 rows in set (0.00 sec)
測試python-mysqldb是否安裝成功:
root@node100:~# python Python 2.7.3 (default, Mar 18 2014, 05:13:23) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import MySQLdb >>>
不報錯則成功。
----------------------- 安裝rpyc --------------------------
rpyc模塊用於平臺與主控端做數據通訊交互,獲取最新版:https://pypi.python.org/packages/source/r/rpyc
# cd /soft/ # wget https://pypi.python.org/packages/source/r/rpyc/rpyc-3.3.0.tar.gz # tar -zxf rpyc-3.3.0.tar.gz # cd rpyc-3.3.0/ # python setup.py install
無報錯則成功。
----------------------- 安裝uwsgi --------------------------
#### tar包安裝:
獲取最新版:http://uwsgi-docs.readthedocs.org/en/latest/Download.html
可以在百度百科裏面查到uwsgi的有關信息。
# apt-get install python-dev #編譯時需要python相關庫文件 # cd /soft # wget http://projects.unbit.it/downloads/uwsgi-2.0.9.tar.gz # tar -zxf uwsgi-2.0.9.tar.gz # cd uwsgi-2.0.9/ # python uwsgiconfig.py --build #看服務器配置,等待時間可能比較長。 ### apt-get源安裝 # apt-get install uwsgi#這種安裝會解決包的依賴關係,不用擔心安裝遇到依賴包錯誤。
看到下面代碼,表示安裝成功:
[ ok ] Starting app server(s): uwsgi (omitted; missing conffile(s) in /etc/uwsgi/apps-enabled).
----------------------- 安裝Django --------------------------
參考:http://chongzi100.blog.51cto.com/340243/1600754
版本:Django-1.7.2
4,創建django工程:
# cd /data/www/ # django-admin startproject web04 # tree web04 web04 ├── manage.py └── web04 ├── __init__.py ├── settings.py ├── urls.py ├── wsgi.py 1 directory, 5 files
django測試,可以看參考文章。
5,配置nginx:
# pwd /data/nginx/conf # cat nginx.conf user www; worker_processes 1; pid /data/logs/nginx/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; gzip on; server { listen 80; server_name web01.test.com; location / { uwsgi_pass 192.168.137.100:8000; uwsgi_param UWSGI_CHDIR /data/www/web04/web04; uwsgi_param UWSGI_SCRIPT wsgi; include uwsgi_params; access_log /data/logs/nginx_access.log; #打開日誌功能方便排錯。 } } }
6,配置uwsgi,創建uwsgi配置文件/data/nginx/conf/uwsgi.ini
# cat uwsgi.ini [uwsgi] socket = 192.168.137.100:8000 master = true pidfile = /data/logs/uwsgi.pid processes = 4 #指定進程數 workers = 1 #分配CPU核數 chdir = /data/www/web04/web04 #項目主目錄 pythonpath = /data/www/web04 #項目上層目錄 profiler = true memory-report = true enable-threads = true logdate = true limit-as = 512 #內存限制512k daemonize = /data/logs/django.log #開啓日誌,方便排錯 gid = www uid = www vhost = false #如果是多站點,可改爲true plugins = python #指定uwsgi將使用python
更多的參數說明,可參考官方說明。
http://uwsgi-docs.readthedocs.org/en/latest/Configuration.html
7,測試:
啓動uwsgi: uwsgi --ini /data/nginx/conf/uwsgi.ini 啓動nginx: /data/nginx/sbin/nginx
8,遇到的錯誤:
"unavailable modifier requested"
該錯誤在網上搜了下,多數是uwsgi配置文件無法找到python導致的。
解決方法:
確認已經安裝了uwsgi-plugin-python:
# aptitude search uwsgi-plugin-python i uwsgi-plugin-python - Python WSGI plugin for uWSGI p uwsgi-plugin-python3 - Python 3 WSGI plugin for uWSGI
兩個參數都表示軟件包的當前狀態。
i #表示已安裝
p #表示清除軟件包
修改配置文件:
# vim uwsgi.ini plugins = python #添加該行,指定uwsgi用的是python
9,結束語:
文中沒有做python再去鏈接mysql的測試,在以後的文章中寫入吧。如果有哪裏錯了,請留言給我。