简体   繁体   English

需要在没有特定端口的域名中运行 django 应用程序

[英]Need to run django application in a domain name without specific port

I am newbie to Django recently I created a Django app and I uploaded to the server.我最近是 Django 的新手,我创建了一个 Django 应用程序并上传到服务器。 I assigned a domain name to it.我给它分配了一个域名。 each time I run the server I need to type xyz.com:8000 to see my website.每次运行服务器时,我都需要输入xyz.com:8000才能查看我的网站。 Is there any way to resolve this issue?有没有办法解决这个问题? Also, I have doubt.另外,我有疑问。 Do I need to type python manage.py runserver 0:8000 to launch the website or it's just run automatically like PHP .我需要输入python manage.py runserver 0:8000来启动网站还是像PHP一样自动运行。

You should set up a web server!你应该设置一个网络服务器! The web server is required for any site to work.任何站点都需要 Web 服务器才能工作。 Currently the most popular are Apache and NGINX.目前最流行的是Apache和NGINX。 It is the web server that responds to user requests.它是响应用户请求的 Web 服务器。 We need to ensure the interaction of the web server and the python application.我们需要保证web服务器和python应用的交互。 Most popular solutions:最受欢迎的解决方案:

Consider an example with Nginx and Gunicorn:考虑一个 Nginx 和 Gunicorn 的例子:

Let's start by installing the Gunicorn module in a virtual environment:让我们从在虚拟环境中安装 Gunicorn 模块开始:

pip install gunicorn

Configure the gunicorn service settings for our project:为我们的项目配置 gunicorn 服务设置:

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

/etc/systemd/system/gunicorn.service: /etc/systemd/system/gunicorn.service:

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=my_user
Group=www-data
WorkingDirectory=/home/project_dir/project
ExecStart=/home/project_dir/project/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/project_dir/project/project.sock project.wsgi

[Install]
WantedBy=multi-user.target

We enable and run the gunicorn service, check its status:我们启用并运行 gunicorn 服务,检查其状态:

sudo systemctl enable gunicorn
sudo systemctl start gunicorn
sudo systemctl status gunicorn

If all is well, install the nginx web server:如果一切顺利,请安装 nginx Web 服务器:

sudo apt install nginx

Configure the project site parameters:配置项目站点参数:

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

/etc/nginx/sites-available/project: /etc/nginx/sites-available/project:

server {
    listen 80;
    server_name <server IP or domain name>;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/project_dir/project;
    }

    location /media/ {
        root /home/project_dir/project;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/project_dir/project/project.sock;
    }
}

We use Nginx as a proxy for the gunicorn Python server:我们使用 Nginx 作为 gunicorn Python 服务器的代理:

proxy_pass http://unix:/home/project_dir/project/project.sock;

Create a link in the allowed sites folder “/etc/nginx/sites-enabled”:在允许的站点文件夹“/etc/nginx/sites-enabled”中创建一个链接:

sudo ln -s /etc/nginx/sites-available/project/etc/nginx/sites-enabled

We restart the Nginx service and add permissions to the firewall:我们重启Nginx服务并添加防火墙权限:

sudo systemctl restart nginx
sudo ufw allow 'Nginx Full'

Done!完毕! You can check the operation of our site by typing the IP address of the server in the browser.您可以通过在浏览器中输入服务器的 IP 地址来检查我们网站的运行情况。 PS Sorry for my english! PS对不起我的英语! If you see an error in the text or code, please edit me.如果您在文本或代码中看到错误,请编辑我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在端口 8000:80 上的 apache+docker 上运行 django 应用程序,需要使用另一个端口运行另一个应用程序但无法正常工作 - I have django application running on apache+docker on port 8000:80,need to run another application with another port but not working 使用自定义域名为 IP:Port 使用 Nginx 和 Gunicorn 的 Flask 应用程序 - Flask application with Nginx & Gunicorn using Custom Domain Name for IP:Port 如何在不绑定IP的情况下在特定端口上运行服务 - How to run a service on a specific port without binding with the IP Django在错误的端口上运行 - Django run on the wrong port 不使用django.contrib.admin运行django应用程序 - Run django application without django.contrib.admin 在Django应用程序中查找主机和端口 - Find Host and Port in a Django Application 如何在不使用字段名称的情况下运行 django orm 查询? - How to run django orm query without using field name? 带有Django的本地网络域名 - Domain name with local network with Django 获取django中的当前域名 - Get the current domain name in django Django :您是否需要使用特定文件夹名称命名标签库(模板、静态等)? - Django : Do you need to name a tag library (templates, static, etc) with a specific folder name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM