天天看点

django+nginx+uwsgi部署安装nginx安装mysql安装虚拟环境安装uwsgi配置nginxcentos问题云服务器部署的问题

  • 安装nginx
  • 安装mysql
  • 安装虚拟环境
  • 安装uwsgi
  • 配置nginx
    • 配置uwsgi
  • centos问题
    • 权限问题
    • 重启nginx
  • 云服务器部署的问题

安装nginx

在ubuntu系统下的安装命令

sudo apt-get install nginx
           

安装完成后,用下面命令查看进程

ps aux|grep nginx
           
django+nginx+uwsgi部署安装nginx安装mysql安装虚拟环境安装uwsgi配置nginxcentos问题云服务器部署的问题

安装mysql

安装命令

sudo apt-get install mysql-server
           

设置完root密码后,继续安装

安装完成后,查看进程,会发现mysqld进程

ps aux|grep mysql 
           

进入mysql

mysql -u root -p 
           

绑定ip地址

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
           

找到下面这行,这里要注意,开发阶段用navicat调试时用0.0.0.0,而真正部署上线后应该用127.0.0.1

bind-address = 0.0.0.0
           

重启mysql

sudo service mysql restart
           

允许外部IP连接MYSQL,需要在mysql下执行

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION ;

FLUSH PRIVILEGES;
           

注意,如果使用navicat连接云服务器上的MYSQL

连接设置里写localhost,并且填MYSQL用户名和密码

然后在SSH页签上填写云服务器的ip地址和用户名密码

安装虚拟环境

安装virtualenv

pip install virtualenv


pip install virtualenvwrapper
           

配置workon命令

vim ~/.bashrc
           

最后加两行内容

export WORKON_HOME =$HOME/.virtualenvs

source /home/xxx/.local/bin/virtualenvwrapper.sh
           

然后用下面命令生效改动

source ~/.bashrc
           

创建虚拟环境

mkvirtualenv mxonline
           

进入虚拟环境

workon mxonline
           

导出安装库的信息

pip freeze > requirements.txt
           

复制内容到linux环境,创建一个新的txt文件,粘贴进去

安装需要的开发包

pip install -r requirements.txt
           

某些开发包下载比较慢怎么办,中止安装,然后单独安装这个包

pip install -i https://pypi.douban.com/simple pillow==3.4.1
           

mysql_config not found

问题,需要安装sqlclient

sudo apt-get install libmysqlclient-dev
           

安装uwsgi

pip install uwsgi
           

projectsedu.com第一篇文章有介绍

进入项目根目录,启动wsgi,MxOnline.wsgi为wsgi文件的路径

uwsgi --http :8000 --module MxOnline.wsgi
           

在此之前用常规方式启动项目

python manage.py runserver 0.0.0.0:8000
           

确认项目启动无误

配置nginx

/etc/nginx/conf.d/

目录下

vim uc_nginx.conf

文件内容为

# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8000; # 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 你的ip地址或者域名 ; # substitute your machine's IP address or FQDN
charset utf-8;

# max upload size
client_max_body_size 75M;   # adjust to taste

# Django media
location /media  {
alias 你的目录/Mxonline/media;  # 指向django的media目录
}

location /static {
alias 你的目录/Mxonline/static; # 指向django的static目录
}

# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass  django;
include uwsgi_params; # the uwsgi_params file you installed
}
}
           

软链接过去

sudo ln -s 你的目录/Mxonline/conf/nginx/uc_nginx.conf /etc/nginx/conf.d/
           

启动nginx

sudo service nginx restart


ps aux|grep nginx
           

在django的setting文件中,添加下面一行内容:

STATIC_ROOT = os.path.join(BASE_DIR, "static")
           

运行命令

python manage.py collectstatic
           

配置uwsgi

cd MxOnline/conf/

vim uwsgi.ini
           

粘贴内容

# mysite_uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /home/bobby/Projects/MxOnline
# Django's wsgi file
module          = MxOnline.wsgi
# the virtualenv (full path)

# 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:8000
# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum          = true
virtualenv = /home/bobby/.virtualenvs/mxonline

logto = /tmp/mylog.log
           

用uwsgi启动配置文件

uwsgi -i uwsgi.ini & 
           

window下host文件

c:/windows/system32/drivers/etc/HOSTS
           

kill uwsgi进程

pkill -f uwsgi
           

然后uwsgi会自动重启

debug改为False

centos问题

权限问题

vim /etc/nginx/nginx.conf
           

修改用户名为root

重启nginx

pkill -f nginx

nginx
           

云服务器部署的问题

腾讯云默认25端口是关闭的,所以需要手动打开

用户名菜单下拉,选择25端口解封

阿里云也是类似的问题