In addition to returning Hello World! Finally html, I also made the code to confirm that something can be written to S3.
EC2(Amazon Linux)
Since the sample code is written to S3, assign a put_object-enabled role to S3.
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"
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
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!"
start nginx
$ service nginx start
start uwsgi
$ cd /var/www/uwsgi
$ uwsgi --ini uwsgi.ini
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.
I stopped with ** control + c **.
I stopped at either of the following.
$ kill -9 `cat uwsgi.pid`
$ killall -9 uwsgi
Could not get from cgi.FieldStorage. Obtained from QUERY_STRING in the application argument env.
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
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