A story about myself, who is ignorant of CentOS and Web server construction, set foot on Flask, Nginx, and uWSGI. The procedure is summarized here.
[](A separate article about the various hardships that occurred in the process ↓)
You should replace it as appropriate and read it.
ʻUser: username
proj: project directory
test.py`: Write code to run Flask. Placed directly under proj
$ cd /home/user/proj
Use Flask to create an environment that makes it easy to create web applications in Python.
$ sudo yum install epel-release #Put the repository
$ sudo yum install python3-devel #Insert python3
$ pip3 install pipenv #Introduced pipenv
$ export PIPENV_VENV_IN_PROJECT=true #Set to create a virtual environment in the project directory
$ pipenv --python 3 #Creating a virtual environment
$ ls -a #Check the file list
If you can confirm that .venv
is created, it's OK.
This is the minimum required.
$ pipenv install flask markupsafe
If you don't want to worry about migration, install with pipenv
instead of pip
.
If you have other required modules, install them with pipenv install
as well.
Create test.py
.
$ vi test.py
Enter the insert mode with ʻi and write the following. When you finish writing, exit with ʻEsc
→: wq
.
test.py
from flask import *
app = Flask(__name__)
@app.route("/")
def main():
return 'Hello, World!\n'
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0', port=80)
It is a code that only outputs'Hello, World!'When the top page is accessed.
No matter how many modules you install, it doesn't start without entering the virtual environment.
$ pipenv shell #Enter the virtual environment
Run test.py
!
$ python test.py
Use a browser such as Google Chrome to enter the IP address of the server that executed this to access it.
When "Hello, World!" Is displayed, it's OK.
Return to the terminal and press Ctrl + C
to stop execution.
Introduction of Web server using Nginx. [](If you are already using Apache etc. and want to take this opportunity to break away from Apache here.)
First, open nginx.repo
with vim etc.
$ sudo vi /etc/yum.repos.d/nginx.repo
Describe the following.
nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
Set the firewall to allow HTTP services.
$ sudo firewall-cmd --add-service=http --zone=public --permanent
$ sudo firewall-cmd --reload
$ sudo systemctl start nginx #Immediate start
$ lsof -i:80 #OK if nginx is displayed
uWSGI is an application server that acts as an intermediary between Flask and Nginx.
$ pipenv install uwsgi
$ vi test_uwsgi.ini
Describe the following.
test_uwsgi.ini
[uwsgi]
base = /home/user/proj
app = test
module = %(app)
virtualenv = /home/user/proj/.venv
pythonpath = %(base)
socket = /tmp/test_uwsgi.sock
chmod-socket = 666
callable = app
logto = /home/user/proj/log/%n.log
If you want to arrange it yourself ↓ Summary of uWSGI ini file
Also create a directory for logs.
$ mkdir log
Create a configuration file for Nginx under the proj
directory.
$ vi test_nginx.conf
Describe the following.
test_nginx.conf
server {
listen 80;
server_name [IP address];
charset utf-8;
location / { try_files $uri @yourapplication; }
location @yourapplication {
include uwsgi_params;
uwsgi_pass unix:/tmp/test_uwsgi.sock;
}
}
Place a symbolic link so that Nginx can refer to it at startup.
sudo ln -s /home/user/proj/test_nginx.conf /etc/nginx/conf.d/
Be sure to specify it with an absolute path.
SELinux seems to constantly monitor and control access inside Linux. To minimize the damage when it is invaded by a PC. If you get an error when starting Nginx, you may be able to solve it by doing the following.
$ setenforce 0
This turns off access control. It seems that just going into Permissive mode does not mean that it will be completely turned off.
Edit the configuration file itself so that it will not be restored even if you restart.
$ vi /etc/selinux/config # "SELINUX=enforcing"To"SELINUX=permissive"Rewrite to
Is security safe without using SELinux? It's good to use it, but I concluded that it's okay if you set the firewall firmly.
$ sudo systemctl restart nginx #You may get an error here if you don't turn off SELinux
$ uwsgi --ini test_uwsgi.ini #Start uWSGI from the configuration file
Reload the browser and if'Hello, World!'Is displayed, it's OK.
Make these start automatically when the server starts.
vi /etc/systemd/system/uwsgi.service
Describe the following.
uwsgi.service
[Unit]
Description=uWSGI
After=syslog.target
[Service]
ExecStart=/home/user/proj/.venv/bin/uwsgi --ini /home/user/proj/test_uwsgi.ini
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all
[Install]
WantedBy=multi-user.target
$ cat /lib/systemd/system/nginx.service #OK if the one you just wrote is displayed
$ sudo systemctl daemon-reload # uwsgi.Reload service
$ sudo systemctl enable uwsgi nginx #Enable uWSGI and Nginx services
Try restarting the server and check with your browser. If "Hello, World!" Is displayed, it's OK. Thank you for your hard work! !!
-Until Hello World with Flask + uWSGI + Nginx @ Sakura's VPS (CentOS 6.6) -ConoHa VPS (CentOS 7.6) with Flask + Nginx + uWSGI Web application (multiple) execution environment construction -Hello World with Nginx and uWSGI -UWSGI setting memo when operating properly
Recommended Posts