ubuntu18.04+gunicorn+nginx+supervisor+mysql+redis安装django项目

Ubuntu18.04 install Django project

项目准备: ECS 实例 (云服务器)

此安装部署方案适合本地ubuntu18.04系统安装和虚拟机中ubuntu18.04系统安装

1.使用ssh登录云服务器 (本地搭建忽略此步骤,从第二步开始)

ssh root@'云服务器IP' 


#假设我的云服务器IP是192.168.0.3  
ssh root@192.168.0.3
#第一次连接云服务器会询问你是yes 还是no    (输入yes)
yes
#输入root用户的密码 (假设root用户密码是admin123)
admin123 

登录到云服务器后

2.更新apt

1.apt update  
2.apt upgrade

3.使用pip3下载和安装虚拟环境virtualenv

pip3 install virtualenv

4.使用apt和安装mysql数据库

apt install mysql-server mysql-client -y

5.进入mysql数据库

#输入下面代码直接回车第一次进入根本没有密码
#进来只是试试mysql是否能正常进入
mysql -uroot -p

6.退出mysql数据库

exit

7.初始化mysql数据库

mysql_secure_installation

 

#secure enough. Would you like to setup VALIDATE PASSWORD plugin? #要安装验证密码插件吗?
#Press y|Y for Yes, any other key for No: N 
这里我选择N
#Please set the password for root here.
New password: #输入要为root管理员设置的数据库密码
Re-enter new password: #再次输入密码
    
#Remove anonymous users? (Press y|Y for Yes, any other key for No) : y #删除匿名账户
这里我选择Y

#Disallow root login remotely? (Press y|Y for Yes, any other key for No) : N #禁止root管理员从远程登录
这里我选择N 
#Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y #删除test数据库并取消对它的访问权限
这里我选择Y
#Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y #刷新授权表,让初始化后的设定立即生效
这里我选择Y

当看到下面这个说明完成了
Success
All done!

8.检查mysql服务状态

systemctl status mysql

Active: active(running) 表示运行成功

9.配置root用户远程访问mysql数据库

#使用ubuntu自带的vim 工具编辑mysqld.conf文本文件
vim /etc/mysql/mysql.conf.d/mysqld.conf
#注释掉bind-address=127.0.0.1或者更改为bind-address=0.0.0.0
bind-address=0.0.0.0
#按下键盘ESC后,按Shift键+冒号键 (:)  输入wq 回车

10.再次进入mysql数据库

#假设我的密码是admin123
mysql -uroot -padmin123

11.更新root用户相关信息

grant privileges on *.* to 'root'@'%' identifiedby 'admin123' with grant option;

12.刷新权限

flush privileges;

13.退出mysql

exit

14.重启mysql

systemctl restart mysql

15.再次进入mysql数据库

mysql -uroot -padmin123

16.新建一个Django项目使用的数据库

#假设我给我的数据库起的名字叫abcd_db
create database abcd_db charset=utf8;

17.退出mysql数据库

exit

18.使用apt安装redis

#   -y  的意思是yes  的意思加在后面就不要再一次输y了
apt install redis-server -y

19.配置redis

vim /etc/redis/redis.conf
#找到supervised 更改为如下
supervised systemd
#按ESC键后 按Shift+冒号键(:)  输入wq  回车退出

20.重启redis

service redis restart

21.查看redis的运行状态

systemctl status redis

Active: active(running) 表示运行成功

21.1控制redis

#启动redis服务
service redis start
#关闭redis服务
service redis stop
#重启redis服务
service redis restart
#连接redis客户端
redis-cli

21.2配置远程连接redis

vim /etc/redis/redis.conf
#把bind 127.0.0.1 ::1 更改为 bind 0.0.0.0
bind 0.0.0.0
#保存退出
按下ECS 后 按  Shift+: 后  wq  回车
#重启redis
service redis restart

21.3设置redis密码

vim /etc/redis/redis.conf
#找到 requirepass foobared
#将注释去掉,将后面修改成自己的密码
requirepass admin123

22.安装git

apt install git -y

23.配置git全局设置

#这是设置我的全局用户名称  #不知道请在gitee.com 创建一个新的仓库 就有了或者百度搜索一下
git config --global user.name "你的名字"
#这是设置我的全局邮箱名称
git config --global user.email "你的邮箱"

24.下载项目到服务器

#切换到存储项目的目录
cd /code
#克隆项目  #你码云上的项目或者github上的项目  如果开发的项目在你自己电脑上自己通过WinSCP这个软件传上去
git clone '项目地址'

25.创建和启动虚拟环境

#创建存放python虚拟环境的文件夹
mkdir /env
#切换到虚拟环境存储地方
cd /env
#创建项目需要使用的虚拟环境
virtualenv abcd_env
#启动虚拟环境   (假如我的虚拟环境叫dnas_env)
source abcd_env/bin/activate

26:下载项目所需的包

#切换到存储项目的目录
cd /code/abcd/
#使用pip下载项目所需要的包,项目所需的第三方库已经在requirements.txt里了
pip install -r requirements.txt
#将虚拟环境的使用库生成requirements.txt
#pip freeze > ./requirements.txt

27.进入mysql数据库创建项目所需要的新用户

#进入到mysql数据库
mysql -uroot -p你的密码
create user '用户名'@'%' identified by '密码';  # 创建用户
grant all on *.* to '用户名'@'%';  # 授权
flush privileges;  # 刷新权限

下面是个例子
#创建mysql 新用户 例子
create user 'zhangsan'@'%'identified by 'zhangsan123';
grant all on *.* to 'zhangsan'@'%';
flush privileges;
#退出mysql
exit

28.执行生成迁移文件

#执行生成迁移文件
python manage.py makemigrations
#执行迁移
python manage.py migrate

29.安装supervisor进行进程管理

apt install supervisor -y 

30.配置项目需要supervisor启动的配置文件

#切换到supervisor指定的项目启动配置文件的目录
cd /etc/supervisor/conf.d
#创建配置文件   (名字随便起 ,文件名可以叫a.conf 或者b.conf 随便自己写)
touch abcd.conf  
#使用vim 编辑配置文件
vim abcd.conf
#将配置内容写入文件中

[program:abcd]
command=/env/abcd_env/gunicorn -w 3 -b 0.0.0.0:8000 abcd.wsgi:application#这是你创建的虚拟环境下gunicorn包
directory=/code/abcd#项目存放的目录
user=root
autostart=true
autorestart=true
startsecs=10
redirect_stderr=true
stdout_logfile=/code/logs/out.log  #你要在code文件加下创建logs文件夹
stderr_logfile=/code/logs/err.log  #你要在code文件加下创建logs文件夹


#保存并退出文件
ESC  
Shift+:
 WQ 
回车

31.使用supervisorctl重新加载

#重新加载supervisor
supervisorctl reload

#Restarted supervisord  有左边这个说明启动成功

32.使用supervisorctl启动项目

#指定启动项目
supervisorctl start abcd   #这里的dnas 是你配置文件中第一行[param:这里的英文]

32.1 supervisorctl 使用方法

#启动所有项目
supervisorctl start all
#停止指定项目
supervisorctl stop abcd   #因为我的配置文件里第一行[program:abcd]  所以我是stop  abcd  如果你的配置文件里的[program:aaa]那就是 supervisorctl stop aaa
#停止所有项目
supervisorctl stop all
#重启指定项目
supervisorctl restart abcd
#重启所有项目
supervisorctl restart all

32.2 项目端口被占用解决方案

#先把supervisor停止工作
supervisorctl stop all
#查看指定端口是不是被占用了(比如我查看的端口是8000)
lsof -i :8000
#回车后发现   
"""COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
gunicorn 5016 root    5u  IPv4  68915      0t0  TCP *:8000 (LISTEN)
gunicorn 5019 root    5u  IPv4  68915      0t0  TCP *:8000 (LISTEN)
gunicorn 5021 root    5u  IPv4  68915      0t0  TCP *:8000 (LISTEN)
gunicorn 5022 root    5u  IPv4  68915      0t0  TCP *:8000 (LISTEN)"""
#说明端口是被占用了
#杀死端口
kill -9 8000   #8000项目的端口号 ,你开启的端口号是多少就写多少

#杀死端口之后重新启动项目
supervisctl start all

#项目已经被supervisor进行管理了

33.更改项目settings的DEBUG模式

#(你应该掌握vim操作文本的方式)
 #使用vim更改文件
vim /code/dnas/dnas/settings.py
# 将DEBUG=True更改为DEBUG=False

34.生成静态文件

#先切换到项目中manage.py所出的位置
cd /code/dnas/
#生成静态文件
python manage.py collectstatic
#当你在最后一行看到类似608 static files copied to '/code/dnas/static'. 说明迁移成功了
#不成功的话百度查查Django项目下的settings.py设置STATIC_ROOT

35.下载nginx来管理静态文件

apt install nginx -y

35.1检查Nginx服务的状态和版本

#查看nginx服务的状态
systemctl status nginx
#查看nginx的版本
nginx -v

35.2 nginx使用方法

#启动nginx
systemctl start nginx
#停止nginx
systemctl stop nginx
#重新启动nginx
systemctl restart nginx
#重新加载nginx服务
systemctl reload nginx
#禁用nginx服务在启动时启动
systemctl disable nginx
#重新启动nginx
systemctl enable nginx

35.2 如下说明nginx 安装成功

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

在服务器上输入你的服务器ip地址

http://你的ip地址 不加端口号

这才说明nginx安装成功

36.使用supervisorctl重新启动项目

supervisorctl restart abcd

34.配置nginx的需要xx.conf文件

#nginx的安装路径是在/etc/nginx
#我们要到cd /etc/nginx/conf.d去新增一个xx.conf(假设创建为abcd.conf)
1.      cd /etc/nginx/conf.d
2.      touch abcd.conf
4.      vim abcd.conf 



35.将如下内容复制并粘贴到abcd.conf文件

【小提示】:linux系统如何查看自己的IP地址 在黑窗口中输入ifconfig这条命令
server{
	listen 80;
	server_name 127.0.0.1;
	server_name 192.168.0.206;#这是你的服务器的IP地址,也可以在加一个server_name 加上你的域名(使用的时候将这条备注的去掉)(你本地的局域网地址)
	location / {
      proxy_pass http://127.0.0.1:8000;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
	}
	location /static {
      alias /opt/abcd/static;#假设我的django项目叫abcd  我把它放在根目录下的opt文件夹中 这是它的static配置,在粘贴的时候请把这条备注去掉
	}
	location /media {
	  alias /code/abcd/media;
	}
}

36.保存并退出

Esc    #这是Esc键
Shift+:  #这是Shift键和:键
wq  #这是w键和q键
Enter   #这是回车键

37.检查conf.d里新建的abcd.conf文件是否合格

nginx -t
#出现如下内容说明是正确的,如果出现其他情况 nginx会在弹出的内容上告诉你如何查看这些报错,告诉你哪一行你写错了
#nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
#nginx: configuration file /etc/nginx/nginx.conf test is successful

38.检查合格了就重启nginx

service nginx restart

39.重启supervisorctl

supervisorctl restart all

#或者

supervisorctl restart 你supervisor的配置文件里的名称  # 假如我的配置文件在/etc/supervisor/conf.d/abcd.conf   我的dnas.conf 第一行 [program: dnas]  那就是dnas
#指定重启某个配置文件
supervisorctl restart abcd

40.打开浏览器

#假如我的IP是192.168.0.206
#那我就在浏览器输入如下内容
http://192.168.0.206/admin/
原文地址:https://www.cnblogs.com/pythonliuwei/p/14505598.html