[英]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.