虚拟主机配置
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;
*** 查询用户的密码,都为空,用下面的命令设置root的密码为root
mysql> set password for root@localhost=password('root');
*** 开放远程登录权限
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,创建test.py文件:
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"]
通过uwsgi运行该文件。
ubuntu:~/$ uwsgi --http :8001 --wsgi-file test.py
接下来配置Django与uwsgi连接。此处,假定的我的django项目位置为:/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
常用选项:
http : 协议类型和端口号
processes : 开启的进程数量
workers : 开启的进程数量,等同于processes(官网的说法是spawn the specified number ofworkers / processes)
chdir : 指定运行目录(chdir to specified directory before apps loading)
wsgi-file : 载入wsgi-file(load .wsgi file)
stats : 在指定的地址上,开启状态服务(enable the stats server on the specified address)
threads : 运行线程。由于GIL的存在,我觉得这个真心没啥用。(run each worker in prethreaded mode with the specified number of threads)
master : 允许主进程存在(enable master process)
daemonize : 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)。实际上最常用的,还是把运行记录输出到一个本地文件上。
pidfile : 指定pid文件的位置,记录主进程的pid号。
vacuum : 当服务器退出的时候自动清理环境,删除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
创建文件如下:
# 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
这个配置,其实就相当于在上一小节中通过wsgi命令,后面跟一堆参数的方式,给文件化了。
socket 指定项目执行的端口号。
chdir 指定项目的目录。
module myweb.wsgi ,可以这么来理解,对于myweb_uwsgi.ini文件来说,与它的平级的有一个myweb目录,这个目录下有一个wsgi.py文件。
其它几个参数,可以参考上一小节中参数的介绍。
接下来,切换到myweb项目目录下,通过uwsgi命令读取myweb_uwsgi.ini文件启动项目。
另修改settings.py文件,配置如下:
ALLOWED_HOSTS = ["*"]
通过uwsgi命令读取myweb_uwsgi.ini文件启动项目
ubuntu:~/$ uwsgi --ini myweb_uwsgi.ini
再接下来要做的就是修改nginx.conf配置文件。打开/etc/nginx/nginx.conf文件,添加如下内容。
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/;
}
}
直接访问页面。