前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Ubuntu18.04搭建vue+django项目

Ubuntu18.04搭建vue+django项目

原创
作者头像
用户10782352
修改2023-10-12 16:28:17
3140
修改2023-10-12 16:28:17
举报
文章被收录于专栏:我的部署记录我的部署记录

公司要求按照生产环境进行部署,不能使用runserver方式启动django,查了一圈一般都是uwsgi,但是我弄不成功,有个同事之前公司使用了gunicorn+supervisor,试了试成功了。整理步骤记录下来。

基本信息

操作系统:Ubuntu18.04

部署项目:vue+django+mysql5.7

安装软件:mysql5.7+python3.7+nginx+gunicorn+supervisor

文章从操作系统安装后开始记录。

安装ssh

我需要shell连接系统,安装18.04后,没有ssh,所以需要安装ssh。

步骤:更新apt,安装ssh包。

代码语言:shell
复制
hy@ubuntu:~$ sudo apt update
hy@ubuntu:~$ sudo apt upgrade
hy@ubuntu:~$ sudo apt-get install openssh-server openssh-client -y

安装mysql5.7

参考:https://www.jianshu.com/p/7a0df58e315b

安装命令

代码语言:shell
复制
hy@ubuntu:~$ sudo apt install mysql-server mysql-client libmysqlclient-dev -y

初始化

代码语言:shell
复制
hy@ubuntu:~$ mysql_secure_installation
# 1 提示是否安装验证插件
Securing the MySQL server deployment.

Enter password for user root: 
Error: Access denied for user 'root'@'localhost'
hy@ubuntu:~$ sudo mysql_secure_installation
# 安全的MySQL服务器部署
Securing the MySQL server deployment.
# 使用空白密码连接到MySQL
Connecting to MySQL using a blank password.
# VALIDATE PASSWORD PLUGIN可用于测试密码并提高安全性
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
# 它检查密码的强度,并允许用户只设置那些足够安全的密码。
# 你想设置VALIDATE PASSWORD插件吗
# 按y | Y表示是,任何其他键为否
Press y|Y for Yes, any other key for No: n
# 2 设置root密码
Please set the password for root here.

New password: 

Re-enter new password: 
# 3 处理匿名账户
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
# 删除匿名用户? (按y | Y表示是,任何其他键表示否):
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.

# 4 是否允许root管理员从远程登录
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
# 禁止远程登录? (按y | Y表示是,任何其他键表示否):
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n

 ... skipping.
 # 5 删除测试数据库
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.

# 删除测试数据库并访问它? (按y | Y表示是,任何其他键表示否):
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.
# 6 重新加载权限表
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
# 现在重新加载权限表? (按y | Y表示是,任何其他键表示否):
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : yes
Success.

All done!

查看mysql状态active(running)表示运行中)

代码语言:shell
复制
hy@ubuntu:~$ systemctl status mysql
● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset:
   Active: active (running) since Mon 2023-10-09 20:07:19 PDT; 18min ago
 Main PID: 1904 (mysqld)
    Tasks: 29 (limit: 4620)
   CGroup: /system.slice/mysql.service
           └─1904 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.

Oct 09 20:07:19 ubuntu systemd[1]: Starting MySQL Community Server...
Oct 09 20:07:19 ubuntu systemd[1]: Started MySQL Community Server.

开启远程访问

代码语言:shell
复制
hy@ubuntu:~$ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
#注释bind-address=127.0.0.1
#保存退出:wq

vim无法使用,则安装sudo apt install vim -y

授权用户及设置密码(添加了远程访问用户admin)

代码语言:shell
复制
#连接mysql
hy@ubuntu:~$ sudo mysql

#授权root、设置密码
mysql> grant all on *.* to root@'%' identified by '123456' with grant option; 
Query OK, 0 rows affected, 1 warning (0.00 sec)
#刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
#授权admin、设置密码
mysql> grant all on *.* to admin@'%' identified by 'nimda' with grant option;  
Query OK, 0 rows affected, 1 warning (0.00 sec)
#刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
#退出
mysql> quit;
Bye
#重启服务
hy@ubuntu:~$ sudo systemctl restart mysql


设置字符集

代码语言:shell
复制
hy@ubuntu:~$ sudo mysql
mysql> SHOW VARIABLES WHERE Variable_name LIKE 'character%' OR Variable_name LIKE 'collation%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
| collation_connection     | utf8_general_ci            |
| collation_database       | latin1_swedish_ci          |
| collation_server         | latin1_swedish_ci          |
+--------------------------+----------------------------+
11 rows in set (0.00 sec)
mysql> quit;

#修改mysql配置文件
hy@ubuntu:/etc/nginx/conf.d$ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

[mysqld]
init_connect=’SET collation_connection = utf8_unicode_ci’
init_connect=’SET NAMES utf8’
character-set-server=utf8
collation-server=utf8_unicode_ci

#保存退出 :wq

#再次查看字符集
hy@ubuntu:~$ sudo mysql
mysql> SHOW VARIABLES WHERE Variable_name LIKE 'character%' OR Variable_name LIKE 'collation%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
| collation_connection     | utf8_unicode_ci            |
| collation_database       | utf8_unicode_ci            |
| collation_server         | utf8_unicode_ci            |
+--------------------------+----------------------------+
11 rows in set (0.00 sec)

安装nginx

代码语言:javascript
复制
hy@ubuntu:/etc/supervisor/conf.d$ sudo apt install nginx

安装完成后,直接访问http://ip,出现Welcome to nginx! 安装成功!

部署vue项目

我的项目通过xftp上传到/opt/app/dist

代码语言:shell
复制
#创建配置文件
hy@ubuntu:/etc/nginx/conf.d$ sudo touch donghuan.conf
hy@ubuntu:/etc/nginx/conf.d$ sudo vim donghuan.conf
#编辑文件
server{
        listen 8000;
        server_name 127.0.0.1;
        server_name 192.168.1.204;
        location / {
      root /opt/app/dist;
        }
}
#保存退出 :wq
#重新加载nginx服务
hy@ubuntu:/etc/nginx/conf.d$ systemctl reload nginx
#重新启动nginx
hy@ubuntu:/etc/nginx/conf.d$ systemctl restart nginx

浏览器访问http://192.168.1.204:8000/,查看到前端页面,部署成功!

安装python3.7

Ubuntu18.04自带python3.6,根据项目需要,升级到3.7

参考:https://blog.csdn.net/A33280000f/article/details/125848983

代码语言:shell
复制
#查看当前python3版本
hy@ubuntu:/$ python3 -V
#1 安装python3.7
hy@ubuntu:/$ sudo apt install python3.7
#2 配置python3默认为3.7
hy@ubuntu:/$ which python3.7
/usr/bin/python3.7
hy@ubuntu:/$ which python3.6
/usr/bin/python3.6
hy@ubuntu:/$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1
update-alternatives: using /usr/bin/python3.7 to provide /usr/bin/python3 (python3) in auto mode
hy@ubuntu:/$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 2
update-alternatives: using /usr/bin/python3.6 to provide /usr/bin/python3 (python3) in auto mode
hy@ubuntu:/$ sudo update-alternatives --config python3
There are 2 choices for the alternative python3 (providing /usr/bin/python3).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.6   2         auto mode
  1            /usr/bin/python3.6   2         manual mode
  2            /usr/bin/python3.7   1         manual mode

Press <enter> to keep the current choice[*], or type selection number: 2  #选择2 默认python为3.7
update-alternatives: using /usr/bin/python3.7 to provide /usr/bin/python3 (python3) in manual mode
#3 查看python3版本
hy@ubuntu:/$ python3 -V
Python 3.7.5
#4 安装pip
#我直接安装pip,在pip3 list时报错“ModuleNotFoundError: No module named 'apt_pkg'”,
#查资料需要先修改文件名,参见:https://www.cnblogs.com/laigongzi/p/13662114.html
hy@ubuntu:/$ cd /usr/lib/python3/dist-packages/
hy@ubuntu:/usr/lib/python3/dist-packages$ sudo cp apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.cpython-37m-x86_64-linux-gnu.so 
hy@ubuntu:/usr/lib/python3/dist-packages$ sudo apt install python3-pip -y
hy@ubuntu:/usr/lib/python3/dist-packages$ pip3 list #显示出列表说明成功

安装python虚拟环境

参考:https://www.cnblogs.com/hicoffee/p/17499149.html

有个坑的说明:之前不打算装虚拟环境,直接安装了gunicorn,结果没办法启动项目,总是提示python2.7中没有ginicorn....后来查资料又装了gunicorn3,成功启动项目了.....再后面开始安装supervisor,添加上gunicorn3启动项目的配置后,项目无法启动,总是提示,gunicorn啥啥啥的,查了一堆资料,说supervisor不能跟gunicorn3用....疯了,后来想虚拟环境里面没有python2.7,gunicorn应该能用,supervisor也就能用了,后面试了一圈,幸好确实能用。中间很混乱,也不知道是猜测正确还是瞎猫碰死耗子。

代码语言:shell
复制
#可以配置上权限配置上权限sudo chmod 777 -R /opt/
#安装venv,注意是3.7,我之前直接装的python3-venv,后面无法使用
hy@ubuntu:/opt$ sudo apt install python3.7-venv -y
hy@ubuntu:/opt$ mkdir venv
hy@ubuntu:/opt$ python3 -m venv venv
#激活
hy@ubuntu:/opt$ source venv/bin/activate
(venv) hy@ubuntu:/opt$ 
#成功!

部署django项目

通过xftp上传我的项目donghuan到opt目录下。

数据库先连接到其他服务器,后面再配置本机的的数据库。

代码语言:shell
复制
hy@ubuntu:/opt$ source venv/bin/activate
#在虚拟环境中安装依赖包
(venv) hy@ubuntu:/opt$ pip3 install -r /opt/app/donghuan/r.txt
#runserver运行
(venv) hy@ubuntu:/opt$ python3 /opt/app/donghuan/manage.py runserver 0.0.0.0:
9000
...
Starting development server at http://0.0.0.0:9000/
Quit the server with CONTROL-C.
#退出虚拟环境
(venv) hy@ubuntu:/opt$ deactivate

安装Gunicorn

代码语言:shell
复制
hy@ubuntu:/opt$ source venv/bin/activate
(venv) hy@ubuntu:~$ pip3 install gunicorn
#使用gunicorn启动django项目
(venv) hy@ubuntu:~$ gunicorn --pythonpath /opt/app/donghuan/ donghuan.wsgi:application --bind 0.0.0.0:9000

访问http:/ip:9000,成功!

安装Supervisor

参考:https://www.cnblogs.com/pythonliuwei/p/14505598.html

代码语言:shell
复制
#1 安装supervisor
hy@ubuntu:~$ sudo apt install supervisor
#2 添加donghuan(我的项目)启动的配置文件
hy@ubuntu:~$ cd /etc/supervisor/conf.d
hy@ubuntu:/etc/supervisor/conf.d$ sudo touch donghuan.conf
hy@ubuntu:/etc/supervisor/conf.d$ sudo vim donghuan.conf
#编辑文件内容(第3-7行都不知道什么作用)
[program:donghuan]
command=/opt/venv/bin/gunicorn --pythonpath /opt/app/donghuan/ donghuan.wsgi:application --bind 0.0.0.0:8000
directory=/opt/app/donghuan
user=root  
autostart=true
autorestart=true
startsecs=10
redirect_stderr=true
stdout_logfile=/var/log/supervisor/out.log
stderr_logfile=/var/log/supervisor/err.log
default-character-set=utf8
#保存退出
:wq
#3 重新加载(我发现这个命令会直接启动程序)
hy@ubuntu:/etc/supervisor/conf.d$ sudo supervisorctl reload
Restarted supervisord
#4supervisorctl启动donghuan(我的项目,配置文件的名字)
hy@ubuntu:/etc/supervisor/conf.d$ sudo supervisorctl start donghuan
donghuan: started
#5 查看supervisor运行的进程
hy@ubuntu:/etc/supervisor/conf.d$ sudo supervisorctl status
donghuan                         RUNNING   pid 6162, uptime 0:00:24

启动命令说明

Nginx

代码语言:shell
复制
#启动nginx
systemctl start nginx
#停止nginx
systemctl stop nginx
#重新启动nginx
systemctl restart nginx
#重新加载nginx服务
systemctl reload nginx
#禁用nginx服务在启动时启动
systemctl disable nginx
#重新启动nginx
systemctl enable nginx

Supervisor

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

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 基本信息
  • 安装ssh
  • 安装mysql5.7
  • 安装nginx
  • 部署vue项目
  • 安装python3.7
  • 安装python虚拟环境
  • 部署django项目
  • 安装Gunicorn
  • 安装Supervisor
  • 启动命令说明
相关产品与服务
云数据库 MySQL
腾讯云数据库 MySQL(TencentDB for MySQL)为用户提供安全可靠,性能卓越、易于维护的企业级云数据库服务。其具备6大企业级特性,包括企业级定制内核、企业级高可用、企业级高可靠、企业级安全、企业级扩展以及企业级智能运维。通过使用腾讯云数据库 MySQL,可实现分钟级别的数据库部署、弹性扩展以及全自动化的运维管理,不仅经济实惠,而且稳定可靠,易于运维。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档