n5321 | 2024年9月2日 16:57

Tags: 运维


ip地址

124.220.90.88

这是我的服务器的地址

我在腾讯云中搭建了一个ubuntu,建立了一个django project.不过我实际上需要在windows的PC下面做development,how to do that?

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC2+ubwookOsuChehRLKSWyw7aPXOJ38WYRkbRJM6gkdV0GomaLt24DHVwkn57cAMF4v/TYxTBcVNdPzDBBtVR3kZ7ix44EZoznjX4VYTh8q5lUrsKU/ct8bFYWeWgezYBxAUYEo28qgr6KzG4jSv4t4jXYcbH4PeCrMpO6LLjaY7JVAQ/BuTGzt9FnkYO8F0Ngj6HrOLuQRiTBKGh9QZ/VEUFYPLCW7BmJaiqRY8B3bLNaBaaV2L+5hCdKd+fifH791G5sCoqMVsxBIVXKLbIWOkpMFZki75Rb8124Asj2PSiBU5+ISUCZ4ou5VBlVnml3/ibPoAENVYzQNm1QQeHJ skey-2a65nl17

应用ubuntu来搭建一个django网站,在腾讯云上

更新服务器和安装基础软件
sudo apt update
sudo apt upgrade -y
没有什么必要

sudo apt install python3-pip python3-dev libpq-dev postgresql postgresql-contrib nginx curl -y
这个里面对版本的控制怎么实现的?


sudo apt install python3-pip python3-dev libpq-dev postgresql postgresql-contrib nginx curl -y

安装和配置 Python 虚拟环境:
sudo apt install python3-venv -y
mkdir ~/myproject
cd ~/myproject
python3 -m venv myprojectenv
source myprojectenv/bin/activate

pip install django gunicorn psycopg2-binary -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

这里安装的三个东西gunicorn是什么?
psycopg2-binary 是一个 Python 库,用于连接和操作 PostgreSQL 数据库。它是 psycopg2 的一个二进制版本,旨在简化安装过程,避免了编译本地库的需要。以下是一些详细信息:
早几天用过,但是现在也基本上不记得了。



django-admin startproject myproject .

python manage.py migrate
python manage.py createsuperuser

python manage.py collectstatic
python manage.py runserver 0.0.0.0:8000



http://124.220.90.88/

登录这个,提示:

Welcome to !

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.

配置 Gunicorn


gunicorn --workers 3 myproject.wsgi:application

sudo nano /etc/nginx/sites-available/myproject

server {
  listen 80;
  server_name 124.220.90.88;  

  location = /favicon.ico { access_log off; log_not_found off; }
  location /static/ {
      alias /home/lighthouse/myproject/static/;  
  }

  location / {
      include proxy_params;
      proxy_pass http://unix:/home/lighthouse/myproject/myproject.sock;
  }
}

启用配置并重启 Nginx:

sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled
sudo nginx -t
sudo systemctl restart nginx

13. 配置 Gunicorn 服务
创建 Gunicorn systemd 服务文件:

sudo nano /etc/systemd/system/gunicorn.service

添加以下内容:
[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=lighthouse
Group=lighthouse
WorkingDirectory=/home/lighthouse/myproject
ExecStart=/home/yourusername/myproject/myprojectenv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/yourusername/myproject/myproject.sock myproject.wsgi:application

[Install]
WantedBy=multi-user.target

启动并启用 Gunicorn 服务:

sudo systemctl daemon-reload
sudo systemctl restart gunicorn



sudo systemctl start gunicorn
sudo systemctl enable gunicorn

关于Gunicorn

Gunicorn 是一个 Python WSGI HTTP 服务器,用于将 Python Web 应用(如 Django 或 Flask)托管在生产环境中。它是一个轻量级、易于配置且高效的服务器,常用于与 Nginx 配合使用。

Gunicorn 的主要功能

  1. WSGI 兼容:支持 WSGI 标准,允许与 Django、Flask 等 WSGI 兼容的应用一起使用。

  2. 并发处理:通过多进程或多线程方式处理多个请求,提高处理能力。

  3. 高效:优化了处理大量并发请求的能力,适合高负载的生产环境。

基本使用

1. 安装 Gunicorn

在你的虚拟环境中安装 Gunicorn:

配合 Nginx 使用

Nginx 作为反向代理服务器,可以将请求转发到 Gunicorn。你需要配置 Nginx 来代理到 Gunicorn 使用的 Unix 套接字文件。以下是 Nginx 配置的简要示例:

确保在更改 Nginx 配置后重新加载 Nginx:

2024/8/24

image-20240824205723242

-XDSac-SYtv6J8j

scp -r /path/to/your/local/django/project

username@server_ip:/path/to/remote/directory

scp -r F:\pythonCode\ubuntunTry ubuntu@124.220.90.88:/home/lighthouse/projects

scp -r ubuntu@124.220.90.88: /home/lighthouse/myproject F:\pythonCode\ubuntunTry01

scp ubuntu@124.220.90.88:/home/lighthouse/myproject/test.txt .

scp ubuntu@124.220.90.88:/home/lighthouse/myproject/test.txt \F:\pythonCode\ubuntunTry01

通过

ssh ubuntu@124.220.90.88

可以远程控制腾讯云

使用 SCP 从远程服务器传输文件到 Windows: 在你的 Windows 终端(如 PowerShell 或命令提示符)中运行以下命令:

bash
复制代码
scp -r ubuntu@124.220.90.88:/home/lighthouse/myproject F:\pythonCode\ubuntunTry

用这个办法可能更好一点

使用 Git 进行同步的常见流程

  1. 初始化仓库: 在你的项目目录中初始化一个 Git 仓库:

    bash
    复制代码
    git init
  2. 添加远程仓库: 将云端服务器作为远程仓库添加:

    bash
    复制代码
    git remote add origin ubuntu@124.220.90.88:/home/lighthouse/myproject.git

    git config --global user.name "N5321"
    git config --global user.email "n5321@163.com"
  3. 提交和推送更改: 在本地进行开发后,提交代码并推送到远程仓库:

    bash复制代码git add .
    git commit -m "Your commit message"
    git push origin master
  4. 在服务器上获取最新代码: 在服务器上,使用 git pull 获取最新的代码更新:

    bash
    复制代码
    git pull origin master

宝塔面板

=============注意:首次打开面板浏览器将提示不安全=================

请选择以下其中一种方式解决不安全提醒 1、下载证书,地址:https://dg2.bt.cn/ssl/baota_root.pfx,双击安装,密码【www.bt.cn 2、点击【高级】-【继续访问】或【接受风险并继续】访问 教程:https://www.bt.cn/bbs/thread-117246-1-1.html mac用户请下载使用此证书:https://dg2.bt.cn/ssl/mac.crt

========================面板账户登录信息==========================

【云服务器】请在安全组放行 24928 端口 外网面板地址: https://124.220.90.88:24928/85c41698 内网面板地址: https://10.0.16.12:24928/85c41698 username: sh6uikrk password: 6d20e88f

浏览器访问以下链接,添加宝塔客服 https://www.bt.cn/new/wechat_customer