CentOS7下的Django2集成部署二:Nginx1.14.2、Mysql5.7和Python3.7的安裝
nginx
- 安裝依賴
yum install -y gcc pcre-devel zlib zlib-devel
- 默認安裝
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install -y nginx - 配置
[root@gz-ct76211 ~]# nginx -v nginx version: nginx/1.14.2 [root@gz-ct76211 ~]# systemctl enable nginx Created symlink from
- 調試
[root@gz-ct76211 ~]# elinks http://localhost --dump Welcome to nginx! If you see this page, the nginx web server is successfully installed and working. Further configuration is required. For online documentation and support please refer to [
此刻nginx基本上就已經可以正常工作了
mysql
- 安裝依賴
yum -y install ncurses-devel gcc-* bzip2-*
- 編譯安裝
- cmake
#獲取cmake wget https://github.com/Kitware/CMake/releases/download/v3.13.1/cmake-3.13.1.tar.gz #解壓 tar xf cmake-3.13.1.tar.gz #進入程序目錄 cd cmake-3.13.1 #編譯並安裝 ./configuare make make install
- boost
#獲取boost1.59 wget https://nchc.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.bz2 #解壓 tar xf boost_1_59_0.tar.bz2 #移動目錄給mysql編譯使用 mv boost_1_59_0 /usr/local/boost
- mysql
#獲取mysql wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24.tar.gz
tar xf mysql-5.7.24.tar.gzcd mysql-5.7.24
用cmake編譯,耗時有點久編譯安裝差不多1小時左右
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data/ -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DDOWNLOAD_BOOST=0 -DWITH_INNODBBASE_STORAGE_ENGINE=1 -DENABLE_LOCAL_INFILE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci \-DWITH_DEBUG=0 -DWITH_EMBEDED_SERVER=0 -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost
# -DMYSQL_USER=mysql 稍後新建mysql用戶make && make install
- cmake
- 配置
- 添加啟動文件
cp support-files/mysql.server /etc/init.d/mysql #授權 chmod 755 /etc/init.d/mysql
- 用戶配置
#添加用戶 useradd -s /sbin/nologin -r mysql #用戶授權 chown mysql.mysql /usr/local/mysql/ -R
- 設置鏈接文件
ln -sf /usr/local/mysql/bin/* /usr/bin/
ln -sf /usr/local/mysql/lib/* /usr/lib/
ln -sf /usr/local/mysql/libexec/* /usr/local/libexec
ln -sf /usr/local/mysql/share/man/man1/* /usr/share/man/man1
ln -sf /usr/local/mysql/share/man/man8/* /usr/share/man/man8
- 配置文件
#數據目錄
[root@home-ct75211 mysql-5.7.24]# mkdir -pv /usr/local/mysql/data
mkdir: created directory ‘/usr/local/mysql/data’配置/etc/my.cnf
1 [mysqld] 2 datadir=/usr/local/mysql/data 3 socket=/usr/local/mysql/mysql.sock 4 # Disabling symbolic-links is recommended to prevent assorted security risks 5 symbolic-links=0 6 # Settings user and group are ignored when systemd is used. 7 # If you need to run mysqld under a different user or group, 8 # customize your systemd unit file for mariadb according to the 9 # instructions in http://fedoraproject.org/wiki/Systemd 10 11 [mysqld_safe] 12 log-error=/var/log/mysql.log 13 pid-file=/var/run/mysql.pid 14 15 # 16 # include all files from the config directory 17 # 18 !includedir /etc/my.cnf.d
vim /etc/my.cnf - 生成root密碼
#生成默認密碼 /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
啟動MySQL
/etc/init.d/mysql start
設置root密碼
mysql_secure_installation
1 [root@home-ct75211 mysql-5.7.24]# mysql_secure_installation 2 3 Securing the MySQL server deployment. 4 5 Enter password for user root: 6 7 The existing password for the user account root has expired. Please set a new password. 8 9 New password: 10 11 Re-enter new password: 12 13 VALIDATE PASSWORD PLUGIN can be used to test passwords 14 and improve security. It checks the strength of password 15 and allows the users to set only those passwords which are 16 secure enough. Would you like to setup VALIDATE PASSWORD plugin? 17 18 Press y|Y for Yes, any other key for No: n 19 Using existing password for root. 20 Change the password for root ? ((Press y|Y for Yes, any other key for No) : y 21 22 New password: 23 24 Re-enter new password: 25 By default, a MySQL installation has an anonymous user, 26 allowing anyone to log into MySQL without having to have 27 a user account created for them. This is intended only for 28 testing, and to make the installation go a bit smoother. 29 You should remove them before moving into a production 30 environment. 31 32 Remove anonymous users? (Press y|Y for Yes, any other key for No) : y 33 Success. 34 35 36 Normally, root should only be allowed to connect from 37 ‘localhost‘. This ensures that someone cannot guess at 38 the root password from the network. 39 40 Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y 41 Success. 42 43 By default, MySQL comes with a database named ‘test‘ that 44 anyone can access. This is also intended only for testing, 45 and should be removed before moving into a production 46 environment. 47 48 49 Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y 50 - Dropping test database... 51 Success. 52 53 - Removing privileges on test database... 54 Success. 55 56 Reloading the privilege tables will ensure that all changes 57 made so far will take effect immediately. 58 59 Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y 60 Success. 61 62 All done!
View Code
- 添加啟動文件
- 調試
1 [root@home-ct75211 mysql-5.7.24]# mysql -u root -p 2 Enter password: 3 Welcome to the MySQL monitor. Commands end with ; or \g. 4 Your MySQL connection id is 5 5 Server version: 5.7.24 Source distribution 6 7 Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. 8 9 Oracle is a registered trademark of Oracle Corporation and/or its 10 affiliates. Other names may be trademarks of their respective 11 owners. 12 13 Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. 14 15 mysql> show databases; 16 +--------------------+ 17 | Database | 18 +--------------------+ 19 | information_schema | 20 | mysql | 21 | performance_schema | 22 | sys | 23 +--------------------+ 24 4 rows in set (0.00 sec) 25 26 mysql>
mysql -u root -p
python
- 安裝依賴
yum install -y gcc-* openssl-* libffi-devel sqlite-devel
- 下載安裝
wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tar.xz
#解壓 tar xf Python-3.7.1.tar.xz #進入目錄 cd Python-3.7.1 #配置 ./configure --enable-optimizations --with-openssl=/usr/bin/openssl #編譯安裝 make -j4 make install
- 調試
... ...
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-10.0.1 setuptools-39.0.1
[root@home-ct75211 Python-3.7.1]# python3
Python 3.7.1 (default, Dec 14 2018, 11:39:37)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> - 升級pip
1 [root@home-ct75211 ~]# pip3 install --upgrade pip 2 pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available. 3 Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by ‘SSLError("Can‘t connect to HTTPS URL because the SSL module is not available.")‘: /simple/pip/ 4 Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by ‘SSLError("Can‘t connect to HTTPS URL because the SSL module is not available.")‘: /simple/pip/ 5 Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by ‘SSLError("Can‘t connect to HTTPS URL because the SSL module is not available.")‘: /simple/pip/ 6 Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by ‘SSLError("Can‘t connect to HTTPS URL because the SSL module is not available.")‘: /simple/pip/ 7 Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by ‘SSLError("Can‘t connect to HTTPS URL because the SSL module is not available.")‘: /simple/pip/ 8 Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host=‘pypi.org‘, port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can‘t connect to HTTPS URL because the SSL module is not available.")) - skipping 9 Requirement already up-to-date: pip in /usr/local/lib/python3.7/site-packages (10.0.1) 10 pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available. 11 Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host=‘pypi.org‘, port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can‘t connect to HTTPS URL because the SSL module is not available.")) - skipping
pip3 install --upgrade pip其實在編譯的時候已經有提示了,下面把Module/Setup重新編輯下
[root@home-ct75211 Python-3.7.1]# vim Modules/Setup
#把下邊這段話的#去掉 211 SSL=/usr/local/ssl 212 _ssl _ssl.c 213 -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl 214 -L$(SSL)/lib -lssl -lcrypto再次 make -j4 && make install ,然後再升級pip
[root@home-ct75211 Python-3.7.1]# pip3 install --upgrade pip Collecting pip ... ... Installing collected packages: pip Found existing installation: pip 10.0.1 Uninstalling pip-10.0.1: Successfully uninstalled pip-10.0.1 Successfully installed pip-18.1
pip3 install --upgrade pip - virtualenv
[root@home-ct75211 Python-3.7.1]# pip3 install virtualenv Collecting virtualenv Downloading ... ... Installing collected packages: virtualenv Successfully installed virtualenv-16.1.0
pip3 install virtualenv#創建py3web虛擬環境 [root@home-ct75211 ~]# virtualenv -p python3 py3web Running virtualenv with interpreter /usr/local/bin/python3 Using base prefix ‘/usr/local‘ New python executable in /root/py3web/bin/python3 Also creating executable in /root/py3web/bin/python Installing setuptools, pip, wheel... done.
#進入py3web虛擬環境,順便安裝django2 [root@home-ct75211 ~]# source py3web/bin/activate (py3web) [root@home-ct75211 ~]# pip3 install django==2.* ... ... Installing collected packages: pytz, django Successfully installed django-2.1.4 pytz-2018.7
#退出虛擬環境 (py3web) [root@home-ct75211 ~]# deactivate
[root@home-ct75211 ~]# - uwsgi
- pip安裝
[root@home-ct75211 ~]# pip3 install uwsgi Collecting uwsgi Downloading ... ... Installing collected packages: uwsgi Running setup.py install for uwsgi ... done Successfully installed uwsgi-2.0.17.1
- 制作啟動腳本
[root@home-ct75211 ~]# mkdir /etc/uwsgi [root@home-ct75211 ~]# vim /etc/uwsgi/uwsgi.ini
[uwsgi] uid = root gid = root socket = 127.0.0.1:9090 # 啟動主進程 master = true # 多站模式 vhost = true # 多站模式時不設置??模塊和?件 no-site = true # ?進程數 workers = 2 # 平滑的重啟 reload-mercy = 10 # 退出、重啟時清理?件 vacuum = true # 開啟10000個進程後, ?動respawn下 max-requests = 1000 # 將進程的總內存量控制在512M limit-as = 512 buffer-size = 30000 # pid?件,?於下?的腳本啟動、停?該進程 pidfile = /var/run/uwsgi9090.pid daemonize = /var/log/uwsgi9090.log
/etc/uwsgi/uwsgi.ini# 啟動 [root@home-ct75211 ~]# uwsgi --ini /etc/uwsgi/uwsgi.ini [uWSGI] getting INI configuration from /etc/uwsgi/uwsgi.ini # 關閉 [root@home-ct75211 ~]# cat /var/run/uwsgi9090.pid 26516 [root@home-ct75211 ~]# kill -9 26516
加入腳本管理
1 #!/bin/sh 2 DESC="uwsgi daemon" 3 NAME=uwsgi 4 DAEMON=/usr/local/bin/uwsgi 5 CONFIGFILE=/etc/uwsgi/$NAME.ini 6 PIDFILE=/var/run/${NAME}9090.pid 7 SCRIPTNAME=/etc/init.d/$NAME 8 FIFOFILE=/tmp/uwsgififo 9 set -e 10 [ -x "$DAEMON" ] || exit 0 11 12 do_start() { 13 if [ ! -f $PIDFILE ];then 14 $DAEMON $CONFIGFILE || echo -n "uwsgi running" 15 else 16 echo "The PID is exist..." 17 fi 18 } 19 20 do_stop() { 21 if [ -f $PIDFILE ];then 22 $DAEMON --stop $PIDFILE || echo -n "uwsgi not running" 23 rm -f $PIDFILE 24 echo "$DAEMON STOPED." 25 else 26 echo "The $PIDFILE doesn‘t found" 27 fi 28 } 29 30 do_reload() { 31 if [ -p $FIFOFILE ];then 32 echo w > $FIFOFILE 33 else 34 $DAEMON --touch-workers-reload $PIDFILE || echo -n "uwsgi can‘t reload" 35 fi 36 } 37 38 do_status() { 39 ps aux|grep $DAEMON 40 } 41 42 case "$1" in 43 status) 44 echo -en "Status $NAME: \n" 45 do_status 46 ;; 47 start) 48 echo -en "Starting $NAME: \n" 49 do_start 50 ;; 51 stop) 52 echo -en "Stopping $NAME: \n" 53 do_stop 54 ;; 55 reload|graceful) 56 echo -en "Reloading $NAME: \n" 57 do_reload 58 ;; 59 *) 60 echo "Usage: $SCRIPTNAME {start|stop|reload}" >&2 61 exit 3 62 ;; 63 esac 64 exit 0
vim /etc/init.d/uwsgi#授權 [root@home-ct75211 html]# chmod 755 /etc/init.d/uwsgi #關閉 [root@home-ct75211 html]# /etc/init.d/uwsgi stop Stopping uwsgi: /usr/local/bin/uwsgi STOPED. #啟動 [root@home-ct75211 html]# /etc/init.d/uwsgi start Starting uwsgi: [uWSGI] getting INI configuration from /etc/uwsgi/uwsgi.ini #查看 [root@home-ct75211 html]# netstat -ntpl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:9090 0.0.0.0:* LISTEN 26572/uwsgi tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 26560/nginx: master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1081/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1380/master tcp6 0 0 :::3306 :::* LISTEN 2425/mysqld tcp6 0 0 :::22 :::* LISTEN 1081/sshd tcp6 0 0 ::1:25 :::* LISTEN 1380/master
- 整合Nginx測試
#進入虛擬環境 [root@home-ct75211 ~]# source ~/py3web/bin/activate #創建my_django項目 (py3web) [root@home-ct75211 ~]# django-admin.py startproject my_django (py3web) [root@home-ct75211 ~]# cd my_django (py3web) [root@home-ct75211 my_django]# ll total 4 -rw-r--r--. 1 root root 0 Dec 14 21:37 db.sqlite3 -rwxr-xr-x. 1 root root 541 Dec 14 21:35 manage.py drwxr-xr-x. 3 root root 93 Dec 14 21:37 my_django # 用Django的內置web服務訪問下 (py3web) [root@home-ct75211 my_django]# python manage.py runserver 192.168.23.211:8000 Performing system checks... System check identified no issues (0 silenced). You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions. Run ‘python manage.py migrate‘ to apply them. December 15, 2018 - 02:46:48 Django version 2.1.4, using settings ‘my_django.settings‘ Starting development server at http://192.168.23.211:8000/
[root@home-ct75211 ~]# elinks http://192.168.23.211:8000/ --dump DisallowedHost at / Invalid HTTP_HOST header: ‘192.168.23.211:8000‘. You may need to add ‘192.168.23.211‘ to ALLOWED_HOSTS. 。。。
需要ALLOWED_HOSTS加入* vim my_django/settings.py ALLOWED_HOSTS = [‘*‘] ,編輯後再訪問下,就可以了
[root@home-ct75211 ~]# elinks http://192.168.23.211:8000/ --dump [1]django View [2]release notes for Django 2.1 The install worked successfully! Congratulations! You are seeing this page because [3]DEBUG=True is in your settings file and you have not configured any URLs. [4]Django Documentation Topics, references, & how-to‘s [5]Tutorial: A Polling App Get started with Django [6]Django Community Connect, get help, or contribute
elinks http://192.168.23.211:8000/ --dump# 用Nginx來訪問
(py3web) [root@home-ct75211 ~]# mv my_django /usr/share/nginx/html(py3web) [root@home-ct75211 ~]# vim /etc/nginx/conf.d/my_django.conf
1 server { 2 listen 80; 3 server_name www.my-django.cc; 4 5 #charset koi8-r; 6 7 #access_log logs/host.access.log main; 8 9 location / { 10 include uwsgi_params; 11 uwsgi_pass 127.0.0.1:9090; 12 uwsgi_param UWSGI_SCRIPT my_django.wsgi; 13 uwsgi_param UWSGI_CHDIR /usr/share/nginx/html/my_django; 14 index index.html index.htm; 15 client_max_body_size 35m; 16 #uwsgi_cache_valid 1m; 17 #uwsgi_temp_file_write_size 64k; 18 #uwsgi_busy_buffers_size 64k; 19 #uwsgi_buffers 8 64k; 20 #uwsgi_buffer_size 64k; 21 #uwsgi_read_timeout 300; 22 #uwsgi_send_timeout 300; 23 #uwsgi_connect_timeout 300; 24 } 25 26 #error_page 404 /404.html; 27 28 # redirect server error pages to the static page /50x.html 29 # 30 error_page 500 502 503 504 /50x.html; 31 location = /50x.html { 32 root html; 33 } 34 35 36 }
/etc/nginx/conf.d/my_django.conf(py3web) [root@home-ct75211 ~]# systemctl restart nginx
配置本地域名
1 (py3web) [root@home-ct75211 ~]# vim /etc/hosts 2 3 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 5 127.0.0.1 www.my-django.cc 6 127.0.0.1 www.my-nginx.cc 7 127.0.0.1 www.my-blog.cc
(py3web) [root@home-ct75211 ~]# vim /etc/hosts由於這裏使用的是virtualenv環境,在uwsgi的配置文件中還要加入虛擬環境的pythonpath
(py3web) [root@home-ct75211 ~]# vim /etc/uwsgi/uwsgi.ini # 最後的文件內容
(py3web) [root@home-ct75211 ~]# vim /etc/uwsgi/uwsgi.ini
- pip安裝
重啟uwsgi服務
[root@home-ct75211 ~]# /etc/init.d/uwsgi stop Stopping uwsgi: /usr/local/bin/uwsgi STOPED. [root@home-ct75211 ~]# /etc/init.d/uwsgi start Starting uwsgi: [uWSGI] getting INI configuration from /etc/uwsgi/uwsgi.ini [root@home-ct75211 ~]# elinks http://www.my-django.cc --dump [1]django View [2]release notes for Django 2.1 The install worked successfully! Congratulations! You are seeing this page because [3]DEBUG=True is in your settings file and you have not configured any URLs. [4]Django Documentation Topics, references, & how-to‘s [5]Tutorial: A Polling App Get started with Django [6]Django Community Connect, get help, or contribute References Visible links 1. https://www.djangoproject.com/ 2. https://docs.djangoproject.com/en/2.1/releases/ 3. https://docs.djangoproject.com/en/2.1/ref/settings/#debug 4. https://docs.djangoproject.com/en/2.1/ 5. https://docs.djangoproject.com/en/2.1/intro/tutorial01/ 6. https://www.djangoproject.com/community/
大功告成!!!
CentOS7下的Django2集成部署二:Nginx1.14.2、Mysql5.7和Python3.7的安裝