当前位置:   article > 正文

python Web项目服务器部署上线_服务器部署代码上线

服务器部署代码上线

文章来源:

http://projectsedu.com/2017/08/15/centos7-%E4%B8%8B%E9%80%9A%E8%BF%87nginx-uwsgi%E9%83%A8%E7%BD%B2django%E5%BA%94%E7%94%A8/

1.安装python3.6

  1. 1. 获取
  2. wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz
  3. tar -xzvf Python-3.6.2.tgz -C /tmp
  4. cd /tmp/Python-3.6.2/
  5. 2. 把Python3.6安装到 /usr/local 目录
  6. ./configure --prefix=/usr/local
  7. make
  8. make altinstall
  9. 3. 更改/usr/bin/python链接
  10. ln -s /usr/local/bin/python3.6 /usr/bin/python3

玛丽德

  1. 1. 安装
  2. sudo yum install mariadb-server
  3. 2. 启动, 重启
  4. sudo systemctl start mariadb
  5. sudo systemctl restart mariadb
  6. 3. 设置bind-ip
  7. vim /etc/my.cnf
  8. 在 [mysqld]:
  9. 下面加一行
  10. bind-address = 0.0.0.0
  11. 4. 设置外部ip可以访问
  12. 先进入mysql才能运行下面命令:
  13. mysql 直接进入就行
  14. GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
  15. FLUSH PRIVILEGES
  16. 5. 设置阿里云的对外端口
  17. 视频中有讲解这部分
  18. 6. 安装mysqlclient出问题
  19. centos 7
  20. yum install python-devel mariadb-devel -y
  21. ubuntu:
  22. sudo apt-get install libmysqlclient-dev
  23. 然后:
  24. pip install mysqlclient

3.安装nginx

https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-centos-7

4.安装virtualenvwrapper

  1. yum install python-setuptools python-devel
  2. pip install virtualenvwrapper
编辑.bashrc文件
  1. export WORKON_HOME=$HOME/.virtualenvs
  2. source /usr/local/bin/virtualenvwrapper.sh
  1. 重新加载.bashrc文件
  2. source ~/.bashrc
  3. 新建虚拟环境
  4. mkvirtualenv mxonline
  5. 进入虚拟环境
  6. workon mxonline
  7. 安装pip包
  1. 我们可以通过 pip freeze > requirements.txt 将本地的虚拟环境安装包相信信息导出来
  2. 然后将requirements.txt文件上传到服务器之后运行:
  3. pip install -r requirements.txt
  4. 安装依赖包

5.安装uwsgi

pip install uwsgi

6.测试uwsgi

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

7.配置nginx

  1. 新建uc_nginx.conf
  2. # the upstream component nginx needs to connect to
  3. upstream django {
  4. # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
  5. server 127.0.0.1:8000; # for a web port socket (we'll use this first)
  6. }
  7. # configuration of the server
  8. server {
  9. # the port your site will be served on
  10. listen 80;
  11. # the domain name it will serve for
  12. server_name 你的ip地址 ; # substitute your machine's IP address or FQDN
  13. charset utf-8;
  14. # max upload size
  15. client_max_body_size 75M; # adjust to taste
  16. # Django media
  17. location /media {
  18. alias 你的目录/Mxonline/media; # 指向django的media目录
  19. }
  20. location /static {
  21. alias 你的目录/Mxonline/static; # 指向django的static目录
  22. }
  23. # Finally, send all non-media requests to the Django server.
  24. location / {
  25. uwsgi_pass django;
  26. include uwsgi_params; # the uwsgi_params file you installed
  27. }
  28. }

8.将该配置文件加入到nginx的启动配置文件中

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

8.拉取所有需要的静态文件到同一个目录

  1. 在django的setting文件中,添加下面一行内容:
  2. STATIC_ROOT = os.path.join(BASE_DIR, "static/")
  3. 运行命令
  4. python manage.py collectstatic

9.运行nginx

sudo /usr/sbin/nginx

这里需要注意一定是直接用nginx命令启动,不要用systemctl启动nginx不然会有权限问题

10.通过配置文件启动uwsgi

  1. 新建uwsgi.ini 配置文件, 内容如下:
  2. # mysite_uwsgi.ini file
  3. [uwsgi]
  4. # Django-related settings
  5. # the base directory (full path)
  6. chdir = /home/bobby/Projects/MxOnline
  7. # Django's wsgi file
  8. module = MxOnline.wsgi
  9. # the virtualenv (full path)
  10. # process-related settings
  11. # master
  12. master = true
  13. # maximum number of worker processes
  14. processes = 10
  15. # the socket (use the full path to be safe
  16. socket = 127.0.0.1:8000
  17. # ... with appropriate permissions - may be needed
  18. # chmod-socket = 664
  19. # clear environment on exit
  20. vacuum = true
  21. virtualenv = /home/bobby/.virtualenvs/mxonline
  22. logto = /tmp/mylog.log
  23. 注:
  24. chdir: 表示需要操作的目录,也就是项目的目录
  25. module: wsgi文件的路径
  26. processes: 进程数
  27. virtualenv:虚拟环境的目录
  28. workon mxonline
  29. uwsgi -i 你的目录/Mxonline/conf/uwsgi.ini &

访问

http://你的ip地址/
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/766298
推荐阅读
相关标签
  

闽ICP备14008679号