파이썬 서버를 간단하게 셋팅하는 방법을 공유합니다. 데이터 분석용 모듈을 다수 포함하고 있는 Anaconda2 버전의 파이썬을 사용하였고, 프로세스 관리를 위해 Supervisor를 사용 하였습니다. 그리고 웹서버로 Nginx를 사용하였습니다. 본 셋팅은 가장 기초적인 상태의 셋팅이지만, 향후 다양하게 운영할 수 있는 여지를 포함하고 있기 때문에 소규모의 상용 서비스에도 활용이 가능합니다.
- Build new VM
- AWS 기준으로 EC2 Market Place에서 CentOS 검색하시면 CentOS7이 가장 상위에 검색됩니다.
- 본 포스팅은 nano 사이즈(ssd 8GiB)로 테스트시 잘 동작하였습니다.
- 기본적인 AWS 셋팅은 생략하겠습니다. (ssh key, security group등)
- Connect to server
chmod 500 key_filename.pem ssh centos@00.00.00.00 -i key_filename.pem
- Install Anaconda2 (version 4.3.0 64bit)
- Install bzip2
sudo yum install bzip2
- Download Anaconda2 Installer
curl -O https://repo.continuum.io/archive/Anaconda2-4.3.0-Linux-x86_64.sh
- Install Anaconda
bash Anaconda2-4.3.0-Linux-x86_64.sh
- 기본셋팅은 계속 Enter 치셔도 됩니다.
- 경로가 존재할 경우 설치되지 않습니다.
- 마지막에 경로설정 할때 yes 하셔야 정상적으로 경로가 지정됩니다. (엔터로 넘기시면 아나콘다버전으로 경로지정이 자동으로 되지 않습니다.)
- Reconnect to server for adopting PATH
- 파이썬 경로 설정 적용을 위해 재접속 합니다.
- Anaconda version update
- Anaconda 버전 최신화를 위해 업데이트 합니다.
conda update anaconda
- Anaconda 버전 최신화를 위해 업데이트 합니다.
- Install bzip2
- Install Supervisor
conda install -c anaconda supervisor
- Install Nginx
- Install Nginx
sudo yum install epel-release
sudo yum install nginx
- Registering proxy_pass on configuration file of Nginx
- Edit configuration file
sudo vi /etc/nginx/nginx.conf
location / { proxy_pass http://127.0.0.1:5000/; }
- Edit configuration file
- Starting Nginx Server
sudo systemctl start nginx.service
- Open the inner proxy network settings
sudo setsebool -P httpd_can_network_connect true
- Install Nginx
- Install Git
sudo yum install git
- Download python source code
- Git 으로 소스코드를 받아오기
git clone "path of git repository"
- Install packages of python project
python setup.py install
- Git 으로 소스코드를 받아오기
- Run python server using Supervisor
- Configuration
- Use sample configuration file
- Copy sample file
cp anaconda2/lib/python2.7/site-packages/supervisor/skel/sample.conf \ supervisord.conf
- edit file paths
- log, pid, etc..
- Copy sample file
- Use sample configuration file
- Supervisor에 서비스 등록하기
- Edit configuration file
[program:sample] directory=/home/centos/sample command=python sample.py autostart=true autorestart=true
- Start supervisord daemon
supervisord -c supervisord.conf
- Terminate supervisor for restart
ps -ef | grep supervisord | grep -v grep \ | awk '{print $2}' | xargs kill -9
- Edit configuration file
- Configuration
Leave a comment