首页 > 代码库 > 在ubuntu14.10上安装uwsgi+nginx
在ubuntu14.10上安装uwsgi+nginx
安装ubuntu 14.10服务器版,不用桌面版。
服务器安装时, 语言选 english,选中文的话,locale会是zh_CN,后面安装包会报没有语言包警告。
安装结束时,选择一个OpenSSH,可以直接使用 SecureCRT连接服务器进行操作。
不选择那个Landscape更新管理,听说它是60天免费试用。
1、修改apt更新源
国内源有搜狐,网易,阿里云。 我使用移动网,使用网易的源安不上pip,最后选的是sohu源。
deb http://mirrors.sohu.com/ubuntu/ utopic main restricted universe multiverse
deb http://mirrors.sohu.com/ubuntu/ utopic-security main restricted universe multiverse
deb http://mirrors.sohu.com/ubuntu/ utopic-updates main restricted universe multiverse
deb http://mirrors.sohu.com/ubuntu/ utopic-proposed main restricted universe multiverse
deb http://mirrors.sohu.com/ubuntu/ utopic-backports main restricted universe multiverse
deb-src http://mirrors.sohu.com/ubuntu/ utopic main restricted universe multiverse
deb-src http://mirrors.sohu.com/ubuntu/ utopic-security main restricted universe multiverse
deb-src http://mirrors.sohu.com/ubuntu/ utopic-updates main restricted universe multiverse
deb-src http://mirrors.sohu.com/ubuntu/ utopic-proposed main restricted universe multiverse
deb-src http://mirrors.sohu.com/ubuntu/ utopic-backports main restricted universe multiverse
使用sudo -s把自己变成root,不用每次输sudo,以下命令都省略sudo。
首先备份源列表:
vi cp /etc/apt/sources.list /etc/apt/sources.list_backup
而后用gedit或其他编辑器打开,在这里只能使用vi,或者nano:
vi /etc/apt/sources.list
按Esc 然后 dG, 全部删除内容。
按i键进入Insert模式,通过SecureCRT 粘贴进去。
2、配置网络
安装过程会有设置IP的对话框,不小心填错DNS。
vi /etc/network/interfaces
# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.11.36
netmask 255.255.255.0
network 192.168.11.0
broadcast 192.168.11.255
gateway 192.168.11.1
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 114.114.114.114
dns-search mz.com
重启网卡,让网络配置生效
ifdown eth0
ifup eth0
使用service networking restart 会报错 object failure。
3、安装uwsgi和nginx
ubuntu 14.10自带python 2.7.6, 做apt-get update后会升级到 2.7.8,不知道会不会不小心升级到python 3。
apt-get install python-pip
安装要占用130多M,不过还是划算。
安装uwsgi需要准备一下编译环境,否则会报找不到pytho.h错误。
apt-get install build-essential python
apt-get install python-dev
安装uwsgi
pip install uwsgi==2.0.8
pip会自己选择最新稳定版本,但我这里指定版本。
安装nginx
nginx=stable # use nginx=development for latest development version
add-apt-repository ppa:nginx/$nginx
apt-get update
apt-get install nginx
使用的ppa方式。
安装 django
pip install django==1.6
目前最新是1.7,但项目使用的包有的不支持,只能使用1.6。
安装 mysql
apt-get install mysql-server
这里安装的5.5.40版本,其中会提示输入root的密码,譬如 60240CC
4、配置uwsgi和nginx
新建了一个/etc/mz_conf/目录,把uwsgi和nginx的配置文件放在它下面。
配置nginx
cp /etc/nginx/uwsgi_params /etc/mz_conf/
在此目录创建nginx.conf
# nginx.conf # the upstream component nginx needs to connect to upstream django { server unix:///tmp/mz.sock; # for a file socket #server 127.0.0.1:8001; # for a web port socket (we'll use this first) } # configuration of the server server { # the port your site will be served on listen 80; # the domain name it will serve for server_name 192.168.11.36; # substitute your machine's IP address or FQDN charset utf-8; # max upload size client_max_body_size 128M; # adjust to taste # Django media location /media { alias /var/www/html/media; # your Django project's media files - amend as required } location /static { alias /var/www/html/static; # your Django project's static files - amend as required } # Finally, send all non-media requests to the Django server. location / { uwsgi_pass django; include /var/www/html/uwsgi_params; # the uwsgi_params file you installed } }
创建符号链接,将nginx.conf链接到 /etc/nginx/conf.d/目录
ln -s /etc/mz_conf/nginx.conf /etc/nginx/conf.d/
注:
(1)使用socket,注意///格式,而且使用的文件要等同于uwsgi配置的文件。
(2)server_name,如是服务器有域名,填域名不会错;若网内测试,可以填IP地址。 填错了,会报404错误。
(3)报403 forbidden错误,需要把上传到html文件夹的内容,修改到755。 我是修改到755的。
配置uwsgi
在/etc/mz_config下建立uwsgi.ini
# uwsgi.ini file [uwsgi] # Django-related settings # the base directory (full path) chdir = /var/www/html/ # Django's wsgi file module = mz.wsgi:application # process-related settings # master master = true # maximum number of worker processes processes = 10 # the socket (use the full path to be safe #socket = 127.0.0.1:8001 socket = /tmp/mz.sock # ... with appropriate permissions - may be needed chmod-socket = 666 # clear environment on exit vacuum = true process = 4 threads = 2
写成服务自启动,编辑 /etc/init/uwsgi.conf,并链接到vassals下: ln -s /etc/mz_conf/uwsgi.ini /etc/uwsgi/vassals/
description "uWSGI" start on runlevel [2345] stop on runlevel [06] respawn env UWSGI=/usr/local/bin/uwsgi env LOGTO=/var/log/uwsgi-emperor.log exec $UWSGI --master --emperor /etc/uwsgi/vassals --die-on-term --uid www --gid www --logto $LOGTO
要增加 www用户,命令:adduser www
不输入密码。
增加www组,命令: addgroup www
把www用户加到www组:adduser www www
做成service后,重启uwsgi和nginx,使用 service uwgis restart即可。
在ubuntu14.10上安装uwsgi+nginx