{"id":22400829,"url":"https://github.com/praneethsonu/flask-installation","last_synced_at":"2026-01-06T08:03:22.429Z","repository":{"id":236263126,"uuid":"792261493","full_name":"praneethsonu/Flask-Installation","owner":"praneethsonu","description":null,"archived":false,"fork":false,"pushed_at":"2025-02-18T15:05:28.000Z","size":13,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-18T16:22:41.170Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/praneethsonu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2024-04-26T09:56:50.000Z","updated_at":"2025-02-18T15:05:32.000Z","dependencies_parsed_at":"2024-04-26T10:43:14.689Z","dependency_job_id":"4175bc8a-0206-46e4-afb6-bdcab3d62b5c","html_url":"https://github.com/praneethsonu/Flask-Installation","commit_stats":null,"previous_names":["praneethsonu/flask-installation"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/praneethsonu%2FFlask-Installation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/praneethsonu%2FFlask-Installation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/praneethsonu%2FFlask-Installation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/praneethsonu%2FFlask-Installation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/praneethsonu","download_url":"https://codeload.github.com/praneethsonu/Flask-Installation/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245755681,"owners_count":20667027,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-05T08:14:49.312Z","updated_at":"2026-01-06T08:03:17.399Z","avatar_url":"https://github.com/praneethsonu.png","language":"HTML","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Flask-Installation\n\n**STEP 1: Install Python 3 and pip and Required dependencies**\n\n```bash\nsudo yum install python3 -y\n```\n\n```bash\nsudo yum install gcc python3-devel -y\n```\n**Install Virtualenv and activate it (Optional step but recommended to use)**\n\n```bash\nsudo pip3 install virtualenv\n```\n\n**Navigate to the project path and Activate the virtual environment**\n\nMy present working directory is /home/ec2-user and am creating my project directory in this location. \n\n```bash\nmkdir myflask-proj\n```\n\n```bash\ncd /home/ec2-user/myflask-proj\n```\n\nWhen you run below command, it will create a directory named venv (or whatever name you specified) in the current working directory. Inside this directory, it will set up a complete Python environment, including a copy of the Python interpreter, the standard library, and various supporting files\n\n```bash\npython3 -m venv venv\n```\nAfter creating the virtual environment, you need to activate it. Activation is a process that modifies your shell's environment to use the Python interpreter and other tools from the virtual environment. You can observer (venv) in terminal.\n\n```bash\nsource venv/bin/activate\n```\n\n**Step 2 : Install Flask and Dependencies**\n\n```bash\npip install Flask gunicorn\n```\n\n**Create a sample flask application**\n\nCreate a file with name app.py and add below content.\n\n```bash\nfrom flask import Flask, render_template\n\napp = Flask(__name__)\n\n@app.route('/')\ndef home():\n    return render_template('index.html', title='Home', content='Hello, Flask!')\n\nif __name__ == '__main__':\n    app.run(debug=True)\n```\n\nNow create a folder and name it as \"templates\" and create the required webpages.\n\n```bash\nmkdir templates\n```\n\ninside the templates folder create index.html file and add below content.\n\n```bash\n\u003c!DOCTYPE html\u003e\n\u003chtml lang=\"en\"\u003e\n\u003chead\u003e\n    \u003cmeta charset=\"UTF-8\"\u003e\n    \u003cmeta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"\u003e\n    \u003cmeta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"\u003e\n    \u003ctitle\u003e{{ title }}\u003c/title\u003e\n\u003c/head\u003e\n\u003cbody\u003e\n    \u003ch1\u003e{{ content }}\u003c/h1\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n\n```\n\nalso, create a file with name 500.html in templates path\n\n```bash\ntouch /home/ec2-user/myflask-proj/templates/500.html\n```\n\nprovide read permissions to the flask app content\n\n```bash\nsudo chmod -R +r /home/ec2-user/myflask-proj\n```\n\n\n**Step 3: Run Flask Application**\n\nThis command runs Gunicorn with 2 worker processes, binding to all available network interfaces on port 5000. Adjust the number of workers based on your server's resources. Make sure your ec2 instance is opened with port 5000.\n\n```bash\ngunicorn -w 1 -b 0.0.0.0:5000 app:app\n```\n\nAbove process will run flask application, but when you press Ctrl+C this stop working.\n\n\n**Step 4 : Set Up Nginx as a Reverse Proxy (Optional step but recommended to use)**\n\nInstall nginx\n\n```bash\nsudo amazon-linux-extras install nginx1\n```\n\nCreate a new Nginx configuration file:\n\n```bash\nvim /etc/nginx/conf.d/flask-app.conf\n```\n\nadd below content to the flask-app.conf file, Make sure you adjust the paths of your project accordingly.\n\n```bash\nserver {\n    listen 80;\n    server_name ec2-52-66-143-134.ap-south-1.compute.amazonaws.com;\n\n    location / {\n        proxy_pass http://127.0.0.1:5000;\n        proxy_set_header Host $host;\n        proxy_set_header X-Real-IP $remote_addr;\n        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n    }\n\n    location /static {\n        alias /home/ec2-user/myflask-proj/static;\n    }\n\n    location /favicon.ico {\n        alias /home/ec2-user/myflask-proj/favicon.ico;\n    }\n\n    error_page 500 502 503 504 /500.html;\n    location = /500.html {\n        root /home/ec2-user/myflask-proj/templates;\n    }\n}\n```\n\nNow adjust the nginx configuration file as below:\n\n**Note** : you can add below one line to the configuration file. For reference am sharing entire nginx configuration here.\n\nAdd this **server_names_hash_bucket_size 128;** under http. \n\nNginx conf file path : /etc/nginx/nginx.conf\n\n\n```bash\n\n# For more information on configuration, see:\n#   * Official English Documentation: http://nginx.org/en/docs/\n#   * Official Russian Documentation: http://nginx.org/ru/docs/\n\nuser nginx;\nworker_processes auto;\nerror_log /var/log/nginx/error.log;\npid /run/nginx.pid;\n\n# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.\ninclude /usr/share/nginx/modules/*.conf;\n\nevents {\n    worker_connections 1024;\n}\n\nhttp {\n    log_format  main  '$remote_addr - $remote_user [$time_local] \"$request\" '\n                      '$status $body_bytes_sent \"$http_referer\" '\n                      '\"$http_user_agent\" \"$http_x_forwarded_for\"';\n\n    access_log  /var/log/nginx/access.log  main;\n\n    sendfile            on;\n    tcp_nopush          on;\n    tcp_nodelay         on;\n    keepalive_timeout   65;\n    types_hash_max_size 4096;\n\nserver_names_hash_bucket_size 128;\n\n    include             /etc/nginx/mime.types;\n    default_type        application/octet-stream;\n\n    # Load modular configuration files from the /etc/nginx/conf.d directory.\n    # See http://nginx.org/en/docs/ngx_core_module.html#include\n    # for more information.\n    include /etc/nginx/conf.d/*.conf;\n\n    server {\n        listen       80;\n        listen       [::]:80;\n        server_name  _;\n        root         /usr/share/nginx/html;\n\n        # Load configuration files for the default server block.\n        include /etc/nginx/default.d/*.conf;\n\n        error_page 404 /404.html;\n        location = /404.html {\n        }\n\n        error_page 500 502 503 504 /50x.html;\n        location = /50x.html {\n        }\n    }\n\n# Settings for a TLS enabled server.\n#\n#    server {\n#        listen       443 ssl http2;\n#        listen       [::]:443 ssl http2;\n#        server_name  _;\n#        root         /usr/share/nginx/html;\n#\n#        ssl_certificate \"/etc/pki/nginx/server.crt\";\n#        ssl_certificate_key \"/etc/pki/nginx/private/server.key\";\n#        ssl_session_cache shared:SSL:1m;\n#        ssl_session_timeout  10m;\n#        ssl_ciphers PROFILE=SYSTEM;\n#        ssl_prefer_server_ciphers on;\n#\n#        # Load configuration files for the default server block.\n#        include /etc/nginx/default.d/*.conf;\n#\n#        error_page 404 /404.html;\n#            location = /40x.html {\n#        }\n#\n#        error_page 500 502 503 504 /50x.html;\n#            location = /50x.html {\n#        }\n#    }\n\n}\n```\n\nrun below command to test the nginx syntax errors or misconfigurations.\n\n```bash\nsudo nginx -t\n```\n\nNow, restart the nginx server. verify the log file for error. \n\n\n```bash\nsudo systemctl restart nginx\n```\n\nif you are getting forbidden error, provide read permissions to the flask app content again.\n\n```bash\nsudo chmod -R +r /home/ec2-user/myflask-proj\n```\n\n***To run Gunicorn in the background more persistently, you can use the nohup command.***\n\n```bash\nnohup gunicorn -w 1 -b 127.0.0.1:5000 app:app \u003e gunicorn.log 2\u003e\u00261 \u0026\n```\n\nIf you want to stop the Gunicorn process, you can use the pkill command:\n\n```bash\npkill gunicorn\n```\n\nnginx log file verification.\n\n```bash\nsudo tail -n 20 /var/log/nginx/error.log\n```\n\nTo verify gunicon logs\n\n```bash\ntail -f gunicorn.log\n```\n\n=====================\n\nif you want to try with some other code, you can replace the app.py and copy remaining data to \"templates\" folder.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpraneethsonu%2Fflask-installation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpraneethsonu%2Fflask-installation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpraneethsonu%2Fflask-installation/lists"}