虛擬主機(jī)配置
install libaries
yum install gcc
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel python-devel python-pip pcre-devel python-setuptools keychain
install mysql
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
yum -y install mysql-server
yum -y install mysql++-devel.x86_64
systemctl enable mysqld.service
systemctl start mysqld.service
systemctl stop mysqld.service
configure mysql
mysql -u root -p
mysql> select user,host,password from mysql.user;
*** 查詢用戶的密碼妻献,都為空聋溜,用下面的命令設(shè)置root的密碼為root
mysql> set password for root@localhost=password('root');
*** 開放遠(yuǎn)程登錄權(quán)限
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'test1234qwer~' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;
install nginx
yum -y install nginx
systemctl start nginx.service
configure nginx
vi /etc/nginx/nginx.conf
install django
pip install django
pip install redis
pip install requests
pip install MySQL-python
if pip install mysql-python error,then as follow:
download:
http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz
http://downloads.sourceforge.net/project/mysql-python/mysql-python-test/1.2.3c1/MySQL-python-1.2.3c1.tar.gz
安裝setuptools
# cd /usr/local/src/python
# tar zxvf setuptools-0.6c11.tar.gz
# cd setuptools-0.6c11
# python setup.py build
# python setup.py install
# cd /usr/local/src/python
# tar zxvf MySQL-python-1.2.3.tar.gz
# cd MySQL-python-1.2.3
# python setup.py build
# python setup.py install
>>> import MySQLdb
pip install uwsgi
測試uwsgi掸掸,創(chuàng)建test.py文件:
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"]
通過uwsgi運(yùn)行該文件。
ubuntu:~/$ uwsgi --http :8001 --wsgi-file test.py
接下來配置Django與uwsgi連接鲸鹦。此處,假定的我的django項(xiàng)目位置為:/usr/share/nginx/html/CanBing_Scan
ubuntu:~/$ uwsgi --http :8001 --chdir /usr/share/nginx/html/CanBing_Scan --wsgi-file /usr/share/nginx/html/CanBing_Scan/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191
常用選項(xiàng):
http : 協(xié)議類型和端口號
processes : 開啟的進(jìn)程數(shù)量
workers : 開啟的進(jìn)程數(shù)量,等同于processes(官網(wǎng)的說法是spawn the specified number ofworkers / processes)
chdir : 指定運(yùn)行目錄(chdir to specified directory before apps loading)
wsgi-file : 載入wsgi-file(load .wsgi file)
stats : 在指定的地址上塑悼,開啟狀態(tài)服務(wù)(enable the stats server on the specified address)
threads : 運(yùn)行線程。由于GIL的存在楷掉,我覺得這個(gè)真心沒啥用厢蒜。(run each worker in prethreaded mode with the specified number of threads)
master : 允許主進(jìn)程存在(enable master process)
daemonize : 使進(jìn)程在后臺(tái)運(yùn)行,并將日志打到指定的日志文件或者udp服務(wù)器(daemonize uWSGI)烹植。實(shí)際上最常用的斑鸦,還是把運(yùn)行記錄輸出到一個(gè)本地文件上。
pidfile : 指定pid文件的位置草雕,記錄主進(jìn)程的pid號巷屿。
vacuum : 當(dāng)服務(wù)器退出的時(shí)候自動(dòng)清理環(huán)境,刪除unix socket文件和pid文件(try to remove all of the generated file/sockets)
configure django + uwsgi
django-admin startproject CanBing_Scan
|-- CanBing_Scan
| |-- __init__.py
| |-- __init__.pyc
| |-- settings.py
| |-- settings.pyc
| |-- static
| | `-- 1.css
| |-- urls.py
| |-- urls.pyc
| |-- wsgi.py
| `-- wsgi.pyc
|-- CanBing_uwsgi.ini
`-- manage.py
創(chuàng)建文件如下:
# myweb_uwsgi.ini file
[uwsgi]
# Django-related settings
socket = :8000
# the base directory (full path)
chdir = /usr/share/nginx/html/CanBing_Scan
# Django s wsgi file
module = CanBing_Scan.wsgi
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 4
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
這個(gè)配置墩虹,其實(shí)就相當(dāng)于在上一小節(jié)中通過wsgi命令嘱巾,后面跟一堆參數(shù)的方式,給文件化了诫钓。
socket 指定項(xiàng)目執(zhí)行的端口號旬昭。
chdir 指定項(xiàng)目的目錄。
module myweb.wsgi 尖坤,可以這么來理解稳懒,對于myweb_uwsgi.ini文件來說,與它的平級的有一個(gè)myweb目錄慢味,這個(gè)目錄下有一個(gè)wsgi.py文件场梆。
其它幾個(gè)參數(shù),可以參考上一小節(jié)中參數(shù)的介紹纯路。
接下來或油,切換到myweb項(xiàng)目目錄下,通過uwsgi命令讀取myweb_uwsgi.ini文件啟動(dòng)項(xiàng)目驰唬。
另修改settings.py文件顶岸,配置如下:
ALLOWED_HOSTS = ["*"]
通過uwsgi命令讀取myweb_uwsgi.ini文件啟動(dòng)項(xiàng)目
ubuntu:~/$ uwsgi --ini myweb_uwsgi.ini
再接下來要做的就是修改nginx.conf配置文件腔彰。打開/etc/nginx/nginx.conf文件,添加如下內(nèi)容辖佣。
configure uwsgi+nginx
server {
listen 8099;
server_name 127.0.0.1
charset UTF-8;
access_log /var/log/nginx/myweb_access.log;
error_log /var/log/nginx/myweb_error.log;
client_max_body_size 75M;
autoindex off;
autoindex_exact_size off;
autoindex_localtime off;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
uwsgi_read_timeout 2;
}
location /static {
expires 30d;
add_header Cache-Control private;
alias /usr/share/nginx/html/CanBing_Scan/CanBing_Scan/static/;
}
}
直接訪問頁面霹抛。