포트 80에서 Flask를 실행하려면 어떻게해야합니까?
포트 5000을 통해 Flask 서버를 실행 중이며 괜찮습니다. http://example.com:5000 에서 액세스 할 수 있습니다 .
그러나 http://example.com 에서 간단히 액세스 할 수 있습니까? 포트를 5000에서 80으로 변경해야한다고 가정합니다. 그러나 Flask에서 시도하면이 오류 메시지가 실행될 때 나타납니다.
Traceback (most recent call last):
File "xxxxxx.py", line 31, in <module>
app.run(host="0.0.0.0", port=int("80"), debug=True)
File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 772, in run
run_simple(host, port, self, **options)
File "/usr/local/lib/python2.6/dist-packages/werkzeug/serving.py", line 706, in run_simple
test_socket.bind((hostname, port))
File "<string>", line 1, in bind
socket.error: [Errno 98] Address already in use
lsof -i :80
반품 실행
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
apache2 467 root 3u IPv4 92108840 0t0 TCP *:www (LISTEN)
apache2 4413 www-data 3u IPv4 92108840 0t0 TCP *:www (LISTEN)
apache2 14346 www-data 3u IPv4 92108840 0t0 TCP *:www (LISTEN)
apache2 14570 www-data 3u IPv4 92108840 0t0 TCP *:www (LISTEN)
apache2 14571 www-data 3u IPv4 92108840 0t0 TCP *:www (LISTEN)
apache2 14573 www-data 3u IPv4 92108840 0t0 TCP *:www (LISTEN)
이러한 프로세스를 먼저 종료해야합니까? 안전한가요? 아니면 Flask를 포트 5000에서 계속 실행하는 다른 방법이 있습니까?하지만 기본 웹 사이트 도메인이 어떻게 든 리디렉션됩니까?
따라서 apache2
포트 80에서 실행 했기 때문에 해당 오류 메시지가 발생합니다 .
이것이 개발 용인 경우 포트 5000에 그대로 둡니다.
프로덕션 용인 경우 :
권장하지 않음
apache2
먼저 멈춰라 .
설명서에 명시된대로 권장하지 않습니다.
개발 중에 내장 서버를 사용할 수 있지만 프로덕션 응용 프로그램에는 전체 배포 옵션을 사용해야합니다. 프로덕션 환경에서 내장 개발 서버를 사용하지 마십시오.
추천
- 플라스크를
HTTP
통한 프록시 트래픽apache2
.
이렇게하면 apache2
모든 정적 파일 (Flask에 내장 된 디버그 서버보다 훨씬 우수함)을 처리하고 동적 콘텐츠에 대한 역방향 프록시 역할을하여 해당 요청을 Flask에 전달할 수 있습니다.
다음 은 Apache + mod_wsgi를 사용하여 Flask를 설정하는 방법에 대한 공식 문서 링크 입니다.
편집 1-@Djack에 대한 설명
Apache2를 통한 Flask 프록시 프록시 트래픽
요청이 포트 80 ( HTTP
) 또는 포트 443 ( HTTPS
) 에서 서버로 오면 Apache 또는 Nginx와 같은 웹 서버는 요청의 연결을 처리하고 요청과 관련된 작업을 수행합니다. 이 경우 수신 된 요청은 WSGI 프로토콜의 Flask로 전달되고 Python 코드에서 처리되도록 구성되어야합니다. 이것이 "동적"부분입니다.
동적 컨텐츠를위한 리버스 프록시
위와 같이 웹 서버를 구성하면 몇 가지 장점이 있습니다.
- SSL 종료-웹 서버는 약간의 구성만으로 HTTPS 요청을 처리하도록 최적화됩니다. 파이썬에서는 "자신의 롤"을하지 마십시오. 아마도 비교가 매우 안전하지 않을 것입니다.
- 보안-인터넷 포트를 열려면 보안을 신중하게 고려해야합니다. Flask의 개발 서버는이를 위해 설계되지 않았으며이 목적을 위해 설계된 웹 서버와 비교하여 오픈 버그 또는 보안 문제가있을 수 있습니다. 잘못 구성된 웹 서버도 안전하지 않을 수 있습니다!
- 정적 파일 처리-내장 Flask 웹 서버가 정적 파일을 처리 할 수 있지만 권장하지는 않습니다. Nginx / Apache는 이미지, CSS, Javascript 파일과 같은 정적 파일을 처리하는 데 훨씬 효율적이며 Python 코드에서 처리 할 "동적"요청 (데이터베이스에서 내용을 읽거나 내용이 변경되는 경우) 만 전달합니다.
- + 더 이것은이 질문의 범위에 접해 있습니다. 더 많은 정보를 원하면이 영역에 대해 조사하십시오.
1- Stop other applications that are using port 80. 2- run application with port 80 :
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80)
For externally visible server, where you don't use apache or other web server you just type
flask run --host=0.0.0.0 --port=80
If you use the following to change the port or host:
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80)
use the following code to start the server (my main entrance for flask is app.py):
python app.py
instead of using:
flask run
If you want your application on same port i.e port=5000 then just in your terminal run this command:
fuser -k 5000/tcp
and then run:
python app.py
If you want to run on a specified port, e.g. if you want to run on port=80, in your main file just mention this:
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80)
Your issue is, that you have an apache webserver already running that is already using port 80. So, you can either:
Kill apache: You should probably do this via
/etc/init.d/apache2 stop
, rather than simply killing them.Deploy your flask app in your apache process, as flask in apache describes.
I had to set FLASK_RUN_PORT
in my environment to the specified port number. Next time you start your app, Flask will load that environment variable with the port number you selected.
You don't need to change port number for your application, just configure your www server (nginx or apache) to proxy queries to flask port. Pay attantion on uWSGI
.
set the port with app.run(port=80,debug=True)
you should set debug to true when on dev
Easiest and Best Solution
Save your .py file in a folder. This case my folder name is test. In the command prompt run the following
c:\test> set FLASK_APP=application.py
c:\test> set FLASK_RUN_PORT=8000
c:\test> flask run
----------------- Following will be returned ----------------
* Serving Flask app "application.py"
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:8000/ (Press CTRL+C to quit)
127.0.0.1 - - [23/Aug/2019 09:40:04] "[37mGET / HTTP/1.1[0m" 200 -
127.0.0.1 - - [23/Aug/2019 09:40:04] "[33mGET /favicon.ico HTTP/1.1[0m" 404 -
Now on your browser type: http://127.0.0.1:8000. Thanks
I can't start my app on 80 , it starts on 5000 just fine. I don't see apache running? what else can I check? We want to run on 80 so we can use the url w/o specifying the port.
> server.run(debug=False, host=args.host, port=args.port) File "/home/ryanicky/.local/lib/python3.6/site-packages/flask/app.py", line
> 944, in run
> run_simple(host, port, self, **options) File "/home/ryanicky/.local/lib/python3.6/site-packages/werkzeug/serving.py",
> line 1009, in run_simple
> inner() File "/home/ryanicky/.local/lib/python3.6/site-packages/werkzeug/serving.py",
> line 962, in inner
> fd=fd, File "/home/ryanicky/.local/lib/python3.6/site-packages/werkzeug/serving.py",
> line 805, in make_server
> host, port, app, request_handler, passthrough_errors, ssl_context, fd=fd File
> "/home/ryanicky/.local/lib/python3.6/site-packages/werkzeug/serving.py",
> line 698, in __init__
> HTTPServer.__init__(self, server_address, handler) File "/usr/lib64/python3.6/socketserver.py", line 456, in __init__
> self.server_bind() File "/usr/lib64/python3.6/http/server.py", line 136, in server_bind
> socketserver.TCPServer.server_bind(self) File "/usr/lib64/python3.6/socketserver.py", line 470, in server_bind
> self.socket.bind(self.server_address) OSError: [Errno 98] Address already in use [ryanicky@webstr WebSTR]$ systemctl status httpd Unit
> httpd.service could not be found.
참고URL : https://stackoverflow.com/questions/20212894/how-do-i-get-flask-to-run-on-port-80
'development' 카테고리의 다른 글
JavaScript를 사용하여 'div'표시 / 숨기기 (0) | 2020.05.26 |
---|---|
날짜 문자열이 해당 형식의 유효한 날짜인지 올바르게 판별하십시오. (0) | 2020.05.26 |
PHP 배열을 자바 스크립트로 변환 (0) | 2020.05.26 |
신속하게 for 루프를 역순으로 반복하는 방법은 무엇입니까? (0) | 2020.05.26 |
문자열에서 첫 번째 문자를 제거하는 가장 쉬운 방법은 무엇입니까? (0) | 2020.05.26 |