当前位置:   article > 正文

CentOS7下部署Django项目_centos7部署django

centos7部署django

服务器

 服务器的基本配置

        随便购买一台服务器,使用CentOS系统并记住记住自己的公网IP

        去安全组放行端口

        

去Gitee新建一个代码仓库

Git

下载git,用来同步代码 下载地址:Git - Downloading Package (git-scm.com)

设置好安装路径一路下一步就行

安装好之后 桌面右键 Git Bash Here

全局配置 进入刚刚在Gitee新建的仓库

  1. git config --global user.name "用户名"
  2. git config --global user.email "邮箱"

 在Git输入从Gitee全局设置的两行代码

然后进入项目地址的文件夹 右键 Git Bash Here

初始化

git init

 配置远程地址

git remote add origin https://gitee.com/zzwxxw/xxxxx.git

本地版本提交并提交至代码仓库

  1. git add .
  2. git commit -m '提交说明...'
  3. git push origin master

但是每次提交会输入Gitee的账号密码十分不方便,可以使用SSH

在Git里输入

ssh-keygen -t rsa

查看ssh公钥并复制下来

cat ~/.ssh/id_rsa.pub

在Gitee设置找到SSH公钥,并粘贴进去,这样每次推送就不会要重复输入账号密码

在项目中创建一个 .gitignore的文件,在里面写上文件名或文件夹,可以git忽略一些文件,不要进行版本控制。 

  1. # Byte-compiled / optimized / DLL files
  2. __pycache__/
  3. *.py[cod]
  4. *$py.class
  5. # C extensions
  6. *.so
  7. # Distribution / packaging
  8. .Python
  9. build/
  10. develop-eggs/
  11. dist/
  12. downloads/
  13. eggs/
  14. .eggs/
  15. lib/
  16. lib64/
  17. parts/
  18. sdist/
  19. var/
  20. wheels/
  21. share/python-wheels/
  22. *.egg-info/
  23. .installed.cfg
  24. *.egg
  25. MANIFEST
  26. # PyInstaller
  27. # Usually these files are written by a python script from a template
  28. # before PyInstaller builds the exe, so as to inject date/other infos into it.
  29. *.manifest
  30. *.spec
  31. # Installer logs
  32. pip-log.txt
  33. pip-delete-this-directory.txt
  34. # Unit test / coverage reports
  35. htmlcov/
  36. .tox/
  37. .nox/
  38. .coverage
  39. .coverage.*
  40. .cache
  41. nosetests.xml
  42. coverage.xml
  43. *.cover
  44. *.py,cover
  45. .hypothesis/
  46. .pytest_cache/
  47. cover/
  48. # Translations
  49. *.mo
  50. *.pot
  51. # Django stuff:
  52. *.log
  53. local_settings.py
  54. db.sqlite3
  55. db.sqlite3-journal
  56. # Flask stuff:
  57. instance/
  58. .webassets-cache
  59. # Scrapy stuff:
  60. .scrapy
  61. # Sphinx documentation
  62. docs/_build/
  63. # PyBuilder
  64. .pybuilder/
  65. target/
  66. # Jupyter Notebook
  67. .ipynb_checkpoints
  68. # IPython
  69. profile_default/
  70. ipython_config.py
  71. # pyenv
  72. # For a library or package, you might want to ignore these files since the code is
  73. # intended to run in multiple environments; otherwise, check them in:
  74. # .python-version
  75. # pipenv
  76. # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
  77. # However, in case of collaboration, if having platform-specific dependencies or dependencies
  78. # having no cross-platform support, pipenv may install dependencies that don't work, or not
  79. # install all needed dependencies.
  80. #Pipfile.lock
  81. # poetry
  82. # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
  83. # This is especially recommended for binary packages to ensure reproducibility, and is more
  84. # commonly ignored for libraries.
  85. # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
  86. #poetry.lock
  87. # pdm
  88. # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
  89. #pdm.lock
  90. # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
  91. # in version control.
  92. # https://pdm.fming.dev/#use-with-ide
  93. .pdm.toml
  94. # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
  95. __pypackages__/
  96. # Celery stuff
  97. celerybeat-schedule
  98. celerybeat.pid
  99. # SageMath parsed files
  100. *.sage.py
  101. # Environments
  102. .env
  103. .venv
  104. env/
  105. venv/
  106. ENV/
  107. env.bak/
  108. venv.bak/
  109. # Spyder project settings
  110. .spyderproject
  111. .spyproject
  112. # Rope project settings
  113. .ropeproject
  114. # mkdocs documentation
  115. /site
  116. # mypy
  117. .mypy_cache/
  118. .dmypy.json
  119. dmypy.json
  120. # Pyre type checker
  121. .pyre/
  122. # pytype static type analyzer
  123. .pytype/
  124. # Cython debug symbols
  125. cython_debug/
  126. # PyCharm
  127. # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
  128. # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
  129. # and can be added to the global gitignore or merged into this file. For a more nuclear
  130. # option (not recommended) you can uncomment the following to ignore the entire idea folder.
  131. .idea/

服务器安装git

yum install git -y

 进入项目克隆或拉取代码

  1. 基于用户名和密码,需要输入用户和密码
  2. git clone https://gitee.com/wupeiqi/xxxxx.git
  3. 基于用户名和密码,直接集成用户和密码
  4. git clone https://用户名:密码@gitee.com/wupeiqi/xxxxx.git
  5. 基于秘钥:
  6. >>>ssh-keygen -t rsa
  7. >>>cat ~/.ssh/id_rsa.pub
  8. >>>拷贝公钥到代码仓库
  9. >>>git clone git@gitee.com:wupeiqi/xxxxx.git

Mysql 

在CentOS安装Mysql

CentOS 安装 MySQL - 腾讯云开发者社区-腾讯云 (tencent.com)

Python

安装Python3.9

yum install gcc -y

安装Python3相关依赖

  1. yum install zlib zlib-devel -y
  2. yum install bzip2 bzip2-devel -y
  3. yum install ncurses ncurses-devel -y
  4. yum install readline readline-devel -y
  5. yum install openssl openssl-devel -y
  6. yum install xz lzma xz-devel -y
  7. yum install sqlite sqlite-devel -y
  8. yum install gdbm gdbm-devel -y
  9. yum install tk tk-devel -y
  10. yum install mysql-devel -y
  11. yum install python-devel -y
  12. yum install libffi-devel -y
  1. cd /data/
  2. wget https://www.python.org/ftp/python/3.9.5/Python-3.9.5.tgz

注意:如果没有wget,则先安装 yum install wget -y

tar -xvf Python-3.9.5.tgz

 进入目录并编译安装

  1. cd Python-3.9.5
  2. ./configure
  3. make all
  4. make install

 配置豆瓣源(腾讯云服务器,默认腾讯源)

pip3.9 config set global.index-url https://pypi.douban.com/simple/

虚拟环境 

安装虚拟环境

pip3.9 install virtualenv

创建虚拟环境目录并创建虚拟环境 

  1. mkdir /envs
  2. virtualenv /envs/nb --python=python3.9

安装项目依赖的pip包 

  1. source /envs/nb/bin/activate
  2. pip install flask
  3. pip install pymysql
  4. pip install dbutils

uwsgi 

激活虚拟环境并安装uwsgi 

  1. source /envs/nb/bin/activate
  2. pip install uwsgi

在本地项目根目录下创建 nb_uwsgi.ini

  1. ;添加配置选择
  2. [uwsgi]
  3. ;配置和nginx连接的socket连接
  4. socket=127.0.0.1:8997
  5. ;配置项目路径,项目的所在目录
  6. chdir=/data/www/test/
  7. ;配置wsgi接口模块文件路径,也就是wsgi.py这个文件所在的目录名
  8. wsgi-file=day01/wsgi.py
  9. ;虚拟环境地址
  10. virtualenv = /envs/nb/
  11. ;#配置启动的进程数
  12. processes=4
  13. ;#配置每个进程的线程数
  14. threads=2
  15. ;#配置启动管理主进程
  16. master=True
  17. #配置存放主进程的进程号文件
  18. pidfile=uwsgi.pid
  19. ;配置dump日志记录
  20. daemonize=uwsgi.log`

把本地文件上传至代码仓库

git push 仓库地址

 服务器更新代码

git pull 仓库地址

nginx

利用nginx做反向代理和处理静态文件

yum install nginx -y

修改nginx.conf配置文件: /etc/nginx/nginx.conf 

  1. events {
  2. worker_connections 1024;
  3. }
  4. http {
  5. include mime.types;
  6. default_type application/octet-stream;
  7. sendfile on;
  8. server {
  9. listen 80;
  10. server_name 127.0.0.1:80; #改为自己的域名,没域名修改为127.0.0.1:80
  11. charset utf-8;
  12. location / {
  13. include uwsgi_params;
  14. uwsgi_pass 127.0.0.1:8997; #端口要和uwsgi里配置的一样
  15. uwsgi_param UWSGI_SCRIPT day01.wsgi; #wsgi.py所在的目录名+.wsgi
  16. uwsgi_param UWSGI_CHDIR /data/www/test/; #项目路径
  17. }
  18. location /static/ {
  19. alias /data/www/test/app01/static/; #静态资源路径
  20. }
  21. }
  22. }

启动

接下来就需要启动uwsgi和nginx

uwsgi

uwsgi --ini  nb_uwsgi.ini

 nginx

  1. # 启动
  2. systemctl start nginx
  3. systemctl stop nginx
  4. # 开机启动
  5. systemctl enable nginx

启动完之后就可以访问了 

Shell脚本

但是每次启动停止都比较麻烦,可以写个脚本

reboot.sh

  1. #!/usr/bin/env bash
  2. echo -e "\033[34m--------------------wsgi process--------------------\033[0m"
  3. ps -ef|grep nb_uwsgi.ini | grep -v grep
  4. sleep 0.5
  5. echo -e '\n--------------------going to close--------------------'
  6. ps -ef |grep nb_uwsgi.ini | grep -v grep | awk '{print $2}' | xargs kill -9
  7. sleep 0.5
  8. echo -e '\n----------check if the kill action is correct----------'
  9. /envs/nb/bin/uwsgi --ini nb_uwsgi.ini & >/dev/null
  10. echo -e '\n\033[42;1m----------------------started...----------------------\033[0m'
  11. sleep 1
  12. ps -ef |grep nb_uwsgi.ini | grep -v grep

stop.sh

  1. #!/usr/bin/env bash
  2. echo -e "\033[34m--------------------wsgi process--------------------\033[0m"
  3. ps -ef |grep nb_uwsgi.ini | grep -v grep
  4. sleep 0.5
  5. echo -e '\n--------------------going to close--------------------'
  6. ps -ef |grep nb_uwsgi.ini | grep -v grep | awk '{print $2}' | xargs kill -9
  7. sleep 0.5

 执行需要给权限

  1. chmod 755 reboot.sh
  2. chmod 755 stop.sh
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/508377
推荐阅读
相关标签
  

闽ICP备14008679号