centos 部署django+nginx+redis+uwsgi+supervisor

系统版本

[root@localhost conf.d]# cat /etc/issue
CentOS release 6.9 (Final)
Kernel \r on an \m

[root@localhost conf.d]# uname -a
Linux localhost.localdomain 2.6.32-696.6.3.el6.x86_64 #1 SMP Wed Jul 12 14:17:22 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost conf.d]#
yum update
#这些都是报错过程中装的
yum install gcc zlib zlib-devel openssl -y openssl-devel -y pcre-devel openssl-devel  mysql-server
yum install mysql mysql-devel

安装2.7.13 Python

cd /usr/src
wget https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tgz
tar -zxvf Python-2.7.13.tgz
cd Python-2.7.13
./configure --prefix=/usr/local --enable-shared
make all
make install

替换原文件,并解决Yum依赖2.6.6的问题

mv /usr/bin/python /usr/bin/python2.6.6
ln -s /usr/local/bin/python2.7 /usr/bin/python
sed -i 's/python/python2\.6\.6/g' /usr/bin/yum

安装easy_install

cd /usr/src
wget https://pypi.python.org/packages/b5/9a/9ce1a45a076f977cb870bf0c937\
0347c9371b0e9aa9ca9859196ce58afda/setuptools-28.6.1.tar.gz#\
md5=b8df391e7532b544e16e1e4cc35a90e5
tar -xf setuptools-28.6.1.tar.gz
cd setuptools-28.6.1
python setup.py install

安装pip

cd /usr/src
wget https://pypi.python.org/packages/e7/a8/7556133689add8d1a54c0b14a\
eff0acb03c64707ce100ecd53934da1aa13/pip-8.1.2.tar.gz#\
md5=87083c0b9867963b29f7aba3613e8f4a
tar -xf pip-8.1.2.tar.gz
cd pip-8.1.2
python setup.py install

安装virtualenv

pip install virtualenv

安装nginx

rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm

yum install nginx -y

安装redis

#下载redis
curl http://download.redis.io/releases/redis-3.0.4.tar.gz -o redis-3.0.4.tar.gz
# 解压缩
tar zxvf redis-3.0.4.tar.gz
# 进入解压后目录
cd redis-3.0.4
# 使用Make 编译源文件


安装uwsgi

pip install uwsgi

新建Al4rm.py

def application(env, start_response):
  start_response('200 OK', [('Content-Type','text/html')])
  return "Hello World"
  
# 在终端运行 uwsgi --http :8001 --wsgi-file Al4rm.py
curl http://127.0.0.1:8001
查看是否有"Hello World"输出,若没有输出,请检查你的安装过程。

配置uwsgi和nginx

配置uwsgi

#项目目录新建一个Al4rm.ini文件
[uwsgi]
socket = 0.0.0.0:3031
chdir = /home/web/Al4rm/  #根据自己环境设置,项目目录,保存时把注释去掉
wsgi-file = Al4rm/wsgi.py #根据自己环境设置,wsgi.py目录,保存时把注释去掉
processes = 4
threads = 2
daemonize = /var/log/Al4rm/error.log

配置nginx

# 在/etc/nginx/conf.d新建一个Al4rm.conf文件
server {
listen 80;

charset utf-8;
access_log /var/log/Al4rm/error.log main;
#日志文件保存路径,提前新建好文件夹
index index.html;
location / {
root /home/web/Al4rm/;
# 项目路径
uwsgi_pass 127.0.0.1:3031;  上一步中配置的端口
include uwsgi_params;
}
location /static {
alias /home/web/Al4rm/static;
#资源文件路径
}

}

安装 supervisor

# supervisor 只守护后台程序?前台程序会不生效?
pip install supervisor
#生成配置文件
echo_supervisord_conf > /etc/supervisord.conf
vim /etc/supervisord.conf
在最后加 
[program:redis]  #程序名称
command=/usr/src/redis-3.0.4/src/redis-server /usr/src/redis-3.0.4/redis.conf  启动命令
autostart=true     自动开启
autorestart=true
startsecs=3
[program:nginx]
command=/usr/local/nginx/sbin/nginx -c /etc/nginx/nginx.conf   # -c 指定配置文件,nginx为前台程序 只写启动命令不生效  掉坑了半天爬不出来
autostart=true
autorestart=true
startsecs=3
[program:uwsgi]
command=/usr/local/bin/uwsgi --ini /home/web/myfingerprint/uwsgiconfig.ini # 这个守护需要把上一步配置文件中的 daemonize = /var/log/Al4rm/error.log 注释掉,转为后台程序
stdout_logfile=/var/log/supervisor/supervisor_info.log ;
stderr_logfile=/var/log/supervisor/supervisor_err.log ;
stopsignal=INT
autostart=true
autorestart=true
startsecs=3
[program:mysqld]
command=/usr/libexec/mysqld
autostart=true
autorestart=true
startsecs=3

设置开机启动项

开机启动卡了好几个小时不知道什么原因,添加rc.local手动执行 /etc/rc.d/rc.local 是没问题的重启服务器还是不生效 rc.local应该没有 uwsgi 环境变量,导致添加了rc.local之后重启不生效

# 新建Al4rm.sh
#!/bin/bash
service nginx start
service mysqld start
iptables -F
/usr/local/bin/uwsgi  /home/web/Al4rm/Al4rm.ini
nohup /usr/src/redis-3.0.4/src/redis-server &

# 在rc.local中添加
#!/bin/sh -x
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
/bin/bash 你自己的路径/start.sh 1 >> /etc/mylog  2>&1

# 1 >> /etc/mylog  2>&1 是把日志文件写入mylog文件
# chmod +x /etc/rc.d/rc.local
# chmod +x /etc/rc.local

执行如下命令报错

./configure --prefix=/usr/local --enable-shared

报错如下

checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for --enable-universalsdk... no
checking for --with-universal-archs... 32-bit
checking MACHDEP... linux2
checking EXTRAPLATDIR...
checking for --without-gcc... no
checking for --with-icc... no
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/usr/src/Python-2.7.13':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details

解决办法

yum install gcc

执行Python报错如下

python: error while loading shared  libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory

解决方法

echo '/usr/local/lib' > /etc/ld.so.conf.d/python2.7.conf
ldconfig
python -V

安装easy_install报错

[root@localhost setuptools-28.6.1]# python setup.py install
running install
running bdist_egg
running egg_info
writing requirements to setuptools.egg-info/requires.txt
writing setuptools.egg-info/PKG-INFO
writing top-level names to setuptools.egg-info/top_level.txt
writing dependency_links to setuptools.egg-info/dependency_links.txt
writing entry points to setuptools.egg-info/entry_points.txt
reading manifest file 'setuptools.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
warning: no files found matching '*' under directory 'setuptools/_vendor'
writing manifest file 'setuptools.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_py
copying setuptools.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying setuptools.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying setuptools.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying setuptools.egg-info/entry_points.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying setuptools.egg-info/requires.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying setuptools.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
creating 'dist/setuptools-28.6.1-py2.7.egg' and adding 'build/bdist.linux-x86_64/egg' to it
Traceback (most recent call last):
  File "setup.py", line 180, in <module>
    dist = setuptools.setup(**setup_params)
  File "/usr/local/lib/python2.7/distutils/core.py", line 151, in setup
    dist.run_commands()
  File "/usr/local/lib/python2.7/distutils/dist.py", line 953, in run_commands
    self.run_command(cmd)
  File "/usr/local/lib/python2.7/distutils/dist.py", line 972, in run_command
    cmd_obj.run()
  File "/usr/src/setuptools-28.6.1/setuptools/command/install.py", line 67, in run
    self.do_egg_install()
  File "/usr/src/setuptools-28.6.1/setuptools/command/install.py", line 109, in do_egg_install
    self.run_command('bdist_egg')
  File "/usr/local/lib/python2.7/distutils/cmd.py", line 326, in run_command
    self.distribution.run_command(command)
  File "/usr/local/lib/python2.7/distutils/dist.py", line 972, in run_command
    cmd_obj.run()
  File "/usr/src/setuptools-28.6.1/setuptools/command/bdist_egg.py", line 223, in run
    dry_run=self.dry_run, mode=self.gen_header())
  File "/usr/src/setuptools-28.6.1/setuptools/command/bdist_egg.py", line 465, in make_zipfile
    z = zipfile.ZipFile(zip_filename, mode, compression=compression)
  File "/usr/local/lib/python2.7/zipfile.py", line 736, in __init__
    "Compression requires the (missing) zlib module"
RuntimeError: Compression requires the (missing) zlib module

解决办法

yum install zlib
yum install zlib-devel
安装完成后,重新编译 python2.7【不需要删除,只需要重新编译,make install,安装就行了】

pip报错如下

Traceback (most recent call last):
  File "/usr/local/bin/pip", line 11, in <module>
    load_entry_point('pip==8.1.2', 'console_scripts', 'pip')()
  File "build/bdist.linux-x86_64/egg/pkg_resources/__init__.py", line 564, in load_entry_point
  File "build/bdist.linux-x86_64/egg/pkg_resources/__init__.py", line 2608, in load_entry_point
  File "build/bdist.linux-x86_64/egg/pkg_resources/__init__.py", line 2268, in load
  File "build/bdist.linux-x86_64/egg/pkg_resources/__init__.py", line 2274, in resolve
  File "/usr/local/lib/python2.7/site-packages/pip-8.1.2-py2.7.egg/pip/__init__.py", line 16, in <module>
    from pip.vcs import git, mercurial, subversion, bazaar  # noqa
  File "/usr/local/lib/python2.7/site-packages/pip-8.1.2-py2.7.egg/pip/vcs/subversion.py", line 9, in <module>
    from pip.index import Link
  File "/usr/local/lib/python2.7/site-packages/pip-8.1.2-py2.7.egg/pip/index.py", line 30, in <module>
    from pip.wheel import Wheel, wheel_ext
  File "/usr/local/lib/python2.7/site-packages/pip-8.1.2-py2.7.egg/pip/wheel.py", line 39, in <module>
    from pip._vendor.distlib.scripts import ScriptMaker
  File "/usr/local/lib/python2.7/site-packages/pip-8.1.2-py2.7.egg/pip/_vendor/distlib/scripts.py", line 14, in <module>
    from .compat import sysconfig, detect_encoding, ZipFile
  File "/usr/local/lib/python2.7/site-packages/pip-8.1.2-py2.7.egg/pip/_vendor/distlib/compat.py", line 31, in <module>
    from urllib2 import (Request, urlopen, URLError, HTTPError,
ImportError: cannot import name HTTPSHandler

解决方法

yum install openssl -y
yum install openssl-devel -y
安装完成后,重新编译 python2.7【不需要删除,只需要重新编译,make install,安装就行了】
再次安装pip正常

nginx报错

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

解决办法

yum install pcre-devel
yum install openssl-devel