{"id":676,"date":"2025-03-31T20:40:28","date_gmt":"2025-03-31T12:40:28","guid":{"rendered":"https:\/\/virtualcardx.com\/?p=676"},"modified":"2025-12-01T17:30:37","modified_gmt":"2025-12-01T09:30:37","slug":"apache%e4%b8%8bhttps%e5%8f%8d%e5%90%91%e4%bb%a3%e7%90%86uvicorn","status":"publish","type":"post","link":"https:\/\/virtualcardx.com\/en\/2025\/03\/31\/apache\u4e0bhttps\u53cd\u5411\u4ee3\u7406uvicorn\/","title":{"rendered":"Apache https reverse proxy Uvicorn"},"content":{"rendered":"<p class=\"wp-block-paragraph\">Uvicorn is a Python web server and development server that supports a variety of frameworks such as Flask, Django, and FastAPI. Uvicorn is popular among developers for its high performance and ease of use.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">I. Installation of Uvicorn<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Installing venv for local python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># apt install python-venv<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To facilitate isolation from root, create a new general user mysiteuser<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># useradd -m mysiteuser\n# passwd mysite<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Login to ssh with mysiteuser<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh mysiteuser@123.123.123.123<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Creating a python virtual environment<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ python -m venv . \/venv<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Installing uvicorn in a virtual environment<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$.\/venv\/bin\/pip install uvicorn<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">uvicorn configuration, here's an example mymodule.py for running a flask program<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/python\n\nfrom flask import Flask\n\napp = Flask(__name__)\n\n@app.route('\/')\ndef hello(): return 'Hello, World!\n        return 'Hello, World!\n\nif __name__ == '__main__': uvicorn.run(app,host='0.0.0',port=3.0.0')\n        uvicorn.run(app,host='0.0.0.0',port=3000)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Use the following command to start the server:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>. \/venv\/bin\/uvicorn mymodule:app --reload<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">where mymodule is the name of the Python module that contains the Flask application<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">II. Installation of Apache<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The Apache configuration file is located in \/etc\/apache2\/sites-available\/.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create the file mysite.conf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Save the configuration and enable it<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a2ensite mysite.conf<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Restart Apache to apply the changes<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>systemctl restart apache2<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">III. Integration<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Install apache2 support for wsgi.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apt-get install libapache2-mod-wsgi-py3<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Configure Apache, edit mysite.conf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SSLEngine On\n          SSLProxyEngine On\n          SSLProxyVerify none\n          SSLProxyCheckPeerCN off\n          SSLProxyCheckPeerName off\n          SSLCertificateFile \"opt\/ssl\/server.crt\"\n          SSLCertificateKeyFile \"opt\/ssl\/server.key\"\n\n          ProxyRequests Off\n          ProxyPreserveHost On\n\n          ProxyPass \/ http:\/\/127.0.0.1:3000\/\n          ProxyPassReverse \/ http:\/\/127.0.0.1:3000\/\n&lt;\/VirtualHost<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Installing CertBot<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>. \/venv\/bin\/pip install certbot<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Application for certificates<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>. \/venv\/bin\/certbot certonly -d mysite.conf --manual --preferred-challenges dns --server https:\/\/acme-v02.api.letsencrypt.org\/directory -- config-dir \/opt\/ssl --work-dir \/opt\/ssl --logs-dir \/opt\/ssl<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Configuring crontab<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>crontab -e<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>0 0 0 * * * * . \/venv\/bin\/certbot certonly -d mysite.conf --manual --preferred-challenges dns --server https:\/\/acme-v02.api.letsencrypt.org\/directory -- config-dir \/opt\/ssl --work-dir \/opt\/ssl --logs-dir \/opt\/ssl<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>","protected":false},"excerpt":{"rendered":"<p>This article introduces how to configure Apache https reverse proxy Uvicorn<\/p>","protected":false},"author":1,"featured_media":688,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-676","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology-share","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/virtualcardx.com\/en\/wp-json\/wp\/v2\/posts\/676","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/virtualcardx.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/virtualcardx.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/virtualcardx.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/virtualcardx.com\/en\/wp-json\/wp\/v2\/comments?post=676"}],"version-history":[{"count":0,"href":"https:\/\/virtualcardx.com\/en\/wp-json\/wp\/v2\/posts\/676\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/virtualcardx.com\/en\/wp-json\/wp\/v2\/media\/688"}],"wp:attachment":[{"href":"https:\/\/virtualcardx.com\/en\/wp-json\/wp\/v2\/media?parent=676"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/virtualcardx.com\/en\/wp-json\/wp\/v2\/categories?post=676"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/virtualcardx.com\/en\/wp-json\/wp\/v2\/tags?post=676"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}