Hello World with nginx + uwsgi + python on EC2

In addition to returning Hello World! Finally html, I also made the code to confirm that something can be written to S3.

environment

EC2(Amazon Linux)

Preparation

EC2 instance

Since the sample code is written to S3, assign a put_object-enabled role to S3.

Installation

nginx $ sudo yum install nginx

uwsgi $ sudo pip install uwsgi

you need a C compiler to build uWSGI If so, please install the C compiler. In Installing uWSGI, it was included below. $ sudo yum groupinstall "development tools"

Setting

Reference: Quickstart for Python / WSGI applications

nginx Edit /etc/nginx/nginx.conf

--Edit inside server {location / { --Add location = /favicon.ico {in server {

nginx.conf


server {

  location / {
      include uwsgi_params;
      uwsgi_pass 127.0.0.1:3031;
  }

  location = /favicon.ico{
    empty_gif;
  }

When I first tested it from the browser, it connected to uwsgi even for the favorite request, and it was double-executed. So I also specified location = /favicon.ico.

uwsgi

Prepare a directory for uwsgi appropriately $ mkdir /var/www/uwsgi

Proceed with the image that you put in this directory as follows.

uwsgi/
   index.py  #Execution source
   uwsgi.ini  #setting file
   uwsgi.log #Log output destination
   uwsgi.pid  #Process id output destination

uwsgi.ini Write options to pass at runtime. Since it became long, I wrote it in the ini file.

uwsgi.ini


[uwsgi]
master = True
socket = 127.0.0.1:3031
wsgi-file = index.py
stats = 127.0.0.1:9191
logto = uwsgi.log
pidfile = uwsgi.pid

Reference: uWSGI Options

code

In addition to returning Hello World! Finally html, I also made the code to confirm that something can be written to S3. I will use boto3 to export to s3, so I will put it in. $ sudo pip install boto3

The function called application is the entry point from uwsgi. Change the Bucket Name to your own bucket name.

index.py


# coding:utf-8

import boto3
import logging
import datetime
import uuid

handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
logger = logging.getLogger()
logger.addHandler(handler)
logger.setLevel(logging.INFO)

def main():

    # S3
    s3 = boto3.resource('s3')
    now_s = datetime.datetime.now().strftime('%Y.%m.%d-%H:%M:%S')
    key = 'test-folder/{0}-{1}.txt'.format(now_s, uuid.uuid4())
    s3.meta.client.put_object(Bucket='BucketName', Key=key, Body='test dayo')

    # log
    logger.info('test-log-dayo')



def application(env, start_response):
    main()
    start_response('200 OK', [('Content-Type','text/html')])
    return "Hello World!"

Move

Start-up

start nginx

$ service nginx start

start uwsgi

$ cd /var/www/uwsgi
$ uwsgi --ini uwsgi.ini

Verification

Http request to EC2. OK if you can confirm the following.

--The response is 200, and the html ** Hello World! ** is returned. --A file with a name like "date + date and time + uid.txt" is created in the test-folder of the bucket specified by S3. --The log says test-log-dayo.

s3に書き出されたファイル

Stop (uwsgi)

At the time of front execution

I stopped with ** control + c **.

When running in the background

I stopped at either of the following.

$ kill -9 `cat uwsgi.pid`
$ killall -9 uwsgi 

Other

Get query parameters for Get

Could not get from cgi.FieldStorage. Obtained from QUERY_STRING in the application argument env.

Get Post request body

Could not get from cgi.FieldStorage. I got it when I read wsgi.input of the argument env of application.

Reference: Accessing POST Data from WSGI

end

I'm going to touch it for a moment, but I wrote it because it took time. Linux is a man who has recently begun to touch. If something is wrong, please let me know. end.

Recommended Posts

Hello World with nginx + uwsgi + python on EC2
Python starting with Hello world!
[Note] Hello world output with python
Until Hello World with Flask + uWSGI + Nginx @ Sakura's VPS (CentOS 6.6)
Create Python + uWSGI + Nginx environment with Docker
Hello World and face detection with OpenCV 4.3 + Python
Build python environment with pyenv on EC2 (ubuntu)
Non-blocking with Python + uWSGI
hello world with ctypes
Say hello to the world with Python with IntelliJ
Create Nginx + uWSGI + Python (Django) environment with docker
Hello, World with Docker
Hello World on Django
Run Flask on CentOS with python3.4, Gunicorn + Nginx.
WebSocket with Python + uWSGI
First python ① Environment construction with pythonbrew & Hello World !!
Hello world with flask
Execute python3 system with PHP exec () on AWS EC2
Hello World with Flask + Hamlish
API with Flask + uWSGI + Nginx
Until hello world with zappa
Python --Install MySQLDB on EC2
Hello World (beginners) on Django
Deploy a Django application on EC2 with Nginx + Gunicorn + Supervisor
Hello, world! With virtual CAN communication
cout << "Hello, World! \ N" in python
Notes on using rstrip with python.
Hello World! By QPython with Braincrash
Getting started with Python 3.8 on Windows
Python #Hello World for super beginners
[Memo] Tweet on twitter with python
Getting Started with Heroku-Viewing Hello World in Python Django with Raspberry PI 3
Use python on Raspberry Pi 3 to illuminate the LED (Hello World)
Memorandum of understanding when Python is run on EC2 with Apache
Create a Python3.4 + Nginx + uWSGI + Flask Web application execution environment with haste using pyenv on Ubuntu 12.04
Run servo with Python on ESP32 (Windows)
Python to remember only with hello, worlds
[AWS] Create a Python Lambda environment with CodeStar and do Hello World
How to build Hello, World on #Nix
A memo with Python2.7 and Python3 on CentOS
Map rent information on a map with python
Follow active applications on Mac with Python
Notes on doing Japanese OCR with Python
scipy stumbles with pip install on python 2.7.8
Download files on the web with Python
Build Python environment with Anaconda on Mac
Run a batch of Python 2.7 with nohup on Amazon Linux AMI on EC2
Python execution server construction (Python + uWSGI + Django + Nginx)
Hello world
A typed world that begins with Python
How to display Hello world in python
Installing PIL with Python 3.x on macOS
Install Python 3.8, Pip 3.8 on EC2 (Amazon Linux 2)
Hello World! By QPython with Brainfu * k
Run python wsgi server on NGINX Unit
Working with GPS on Raspberry Pi 3 Python
Create a web application execution environment of Python3.4 + Nginx + uWSGI + Flask with haste using venv on Ubuntu 14.04 LTS
Get information on the 100 most influential tech Twitter users in the world with python.
Getting started with Python with 100 knocks on language processing
nginxparser: Try parsing nginx config file with Python
Hello World with gRPC / go in Docker environment