目录

问题一、Centos7,放开防火墙端口访问8000,浏览器访问时仍然找不到网址


问题一、Centos7,放开防火墙端口访问8000,浏览器访问时仍然找不到网址

防火墙启动状态下开放端口

firewall-cmd --zone=public --add-port=8000/tcp --permanent

然后刷新防火墙

systemctl reload firewalld
systemctl restart firewalld


(ll_env) [root@localhost learning_log]# python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

/var/lib/jenkins/workspace/GitBlit_pull/other/Django/learning_log/applist/AutoCode/config/Config.ini
System check identified no issues (0 silenced).
March 02, 2023 - 06:40:59
Django version 3.2.13, using settings 'learning_log.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

放开端口号尝试外网访问。。。然而还是提示无法连接

【原因在于服务启动访问的IP是127.0.0.1只能是内网访问。。。】

解决:【临时访问方法
启动时加上 请求地址端口号
python manage.py runserver 0.0.0.0:8000
外网访问后,提示400错误【恭喜外网可以访问了】


Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
Invalid HTTP_HOST header: '192.168.24.228:8000'. You may need to add '192.168.24.228' to ALLOWED_HOSTS.
Bad Request: /
[02/Mar/2023 08:15:45] "GET / HTTP/1.1" 400 72625
Invalid HTTP_HOST header: '192.168.24.228:8000'. You may need to add '192.168.24.228' to ALLOWED_HOSTS.
Bad Request: /favicon.ico
[02/Mar/2023 08:15:45] "GET /favicon.ico HTTP/1.1" 400 72546


永久修改方法

通过修改 Django 项目中的 manage.py 文件设置端口号和 IP 地址。

# manage.py 文件中的 main 方法
if __name__ == "__main__":  
    from django.core.management.commands.runserver import Command as Runserver  
    Runserver.default_addr = '0.0.0.0'  # 修改默认地址  
    Runserver.default_port = '8000'  # 修改默认端口  
    main()

 重启服务serverIP变成0.0.0.0:8000,现在可以试试外网访问了

(ll_env) [root@localhost learning_log]# python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

/var/lib/jenkins/workspace/GitBlit_pull/other/Django/learning_log/applist/AutoCode/config/Config.ini
System check identified no issues (0 silenced).
March 02, 2023 - 09:19:56
Django version 3.2.13, using settings 'learning_log.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
[02/Mar/2023 09:20:02] "GET / HTTP/1.1" 200 8625

问题二:能访问了为什么又报400了???,是因为settins.py[ALLOWED_HOSTS]没匹配


(ll_env) [root@localhost learning_log]# python manage.py runserver 0.0.0.0:8000
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/var/lib/jenkins/workspace/GitBlit_pull/other/Django/ll_env/lib/python3.7/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/var/lib/jenkins/workspace/GitBlit_pull/other/Django/ll_env/lib/python3.7/site-packages/django/core/management/__init__.py", line 363, in execute
    settings.INSTALLED_APPS
  File "/var/lib/jenkins/workspace/GitBlit_pull/other/Django/ll_env/lib/python3.7/site-packages/django/conf/__init__.py", line 82, in __getattr__
    self._setup(name)
  File "/var/lib/jenkins/workspace/GitBlit_pull/other/Django/ll_env/lib/python3.7/site-packages/django/conf/__init__.py", line 69, in _setup
    self._wrapped = Settings(settings_module)
  File "/var/lib/jenkins/workspace/GitBlit_pull/other/Django/ll_env/lib/python3.7/site-packages/django/conf/__init__.py", line 170, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/local/python3/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 724, in exec_module
  File "<frozen importlib._bootstrap_external>", line 860, in get_code
  File "<frozen importlib._bootstrap_external>", line 791, in source_to_code
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/var/lib/jenkins/workspace/GitBlit_pull/other/Django/learning_log/learning_log/settings.py", line 28
    ALLOWED_HOSTS = [*]

解决:settings.py【ALLOWED_HOSTS 添加的是IP#】


ALLOWED_HOSTS = [
     '192.168.24.228', '127.0.0.1'
]

192.168.24.228 指定访问IP

127.0.0.1指定内网访问IP

问题三:本地部署一个Centos7,但无法访问远程服务器数据库服务请求IP】

Centos7为虚拟服务器部署本机应用服务需要请求远程服务器

本地win10 和 centos7 同样请求远程服务器IP,一个Ping成功,一个Ping失败

win10

Centos

 解决

1.先添加应该服务器的端口号

firewall-cmd --zone=public --add-port=33060/tcp --permanent

firewall-cmd --zone=public --add-port=3306/tcp --permanent

然后重启防火墙

systemctl reload firewalld

2.修改centos7 网关和DNS配置

vim /etc/sysconfig/network-scripts/ifcfg-enp0s3 

配置网关和DNS,与本地配置的保持一致
重点:重启network ---
service network restart

配置好DNS后,又出现情况。。。。

HTTPConnectionPool(host='url', port=port): Max retries exceeded with url: /path (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f188bac7898>: Failed to establish a new connection: [Errno -2] Name or service not known',)) 

本机的另一个DNS也添加上,刷新应用页面,又好了。。。。

TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="enp0s3"
UUID="b6408bbf-3dc6-44fd-8446-a92690c7774c"
DEVICE="enp0s3"
ONBOOT="yes"

IPADDR=192.168.24.228
NETMASK=255.255.255.0
GATEWAY=192.168.24.1
DNS1=202.96.134.133
DNS2=223.5.5.5

最终问题解决就是把DNS完整的加上!!!

其它问题:

安装Django,系统自带数据库版本比较

重新安装虚拟环境

windowslinux 路径报错,等等都可以百度一下解决

Centos整合测试服务资源,像Locust、Django都是需要手工切换指定目录,再执行命令启动的,但出现服务器挂掉重启,又要SSH连接手工启动,着实比较麻烦的事情:

所以使用sh的方法来自动执行。。。。

先找到目录编辑.sh文件


/usr/local/jenkins/satrt_server.sh

【#!/bin/bash】是sh文件标准格式nohup后台不挂断运行命令---  2>&amp;1 &amp; 这个参数必带项,没有就不是后台运行了】
内容如下:


#!/bin/bash

cd /var/lib/jenkins/workspace/GitBlit_pull/other/WB_Locust/my_locustfiles
nohup locust >/usr/local/jenkins/locust.out 2>&1 &

cd /var/lib/jenkins/workspace/GitBlit_pull/other/Django
source ll_env/bin/activate
cd learning_log
nohup python manage.py runserver >/usr/local/jenkins/djo.out 2>&1 &

重新开启 或重新连接就是执行文件

编辑.bashrc文件

[root@localhost ~]# cd ~
[root@localhost ~]# vim .bashrc


# 增加刚才新建的SH文件路径

# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi


/usr/local/jenkins/satrt_server.sh

然后执行一下.bashrc文件

[root@localhost ~]# source .bashrc

需要杀掉服务可以如下:

Centos 先安装 lsof 

[root@localhost ~]# lsof -i tcp:端口号
[root@localhost ~]# lsof -i tcp:8000
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
python  1592 root    5u  IPv4  19438      0t0  TCP *:irdmi (LISTEN)
[root@localhost ~]# lsof -i tcp:8089
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
locust  1587 root    6u  IPv6  19573      0t0  TCP *:8089 (LISTEN)



注意PID

[root@localhost ~]# kill -9 1592
[root@localhost ~]# kill -9 1587

这样就可以

原文地址:https://blog.csdn.net/vikesgao/article/details/129304375

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_11307.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注