CentOS搭建Django项目环境:nginx+django+supervisor+Anaconda+mysql

12 篇文章 0 订阅
11 篇文章 0 订阅

本项目所用的远程链接工具为electerm(开源且跨平台(macOS,linux,windows),同时支持ssh和sftp,支持保存多个命令,唯一的遗憾新版本不支持串口)https://github.com/electerm/electerm/releases.

nginx安装

参考链接:https://www.runoob.com/linux/nginx-install-setup.html
安装编译工具和相关库:

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

安装pcre:

cd /home
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
tar zxvf pcre-8.35.tar.gz
cd pcre-8.35
./configure
make && make install
# 查看版本
pcre-config --version

正式安装nginx:

cd /home
wget http://nginx.org/download/nginx-1.6.2.tar.gz
tar zxvf nginx-1.6.2.tar.gz
cd nginx-1.6.2
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/home/pcre-8.35
make && make install

将nginx的路径添加至环境变量

export PATH=$PATH:/usr/local/nginx/sbin

查看nginx的版本

nginx -v

nginx配置

创建 Nginx 运行使用的用户 www:

/usr/sbin/groupadd www
/usr/sbin/useradd -g www www

配置nginx.conf ,将/usr/local/nginx/conf/nginx.conf替换为以下内容

user www www;
worker_processes 2; #设置值和CPU核心数一致
error_log /usr/local/nginx/logs/nginx_error.log crit; #日志位置和日志级别
pid /usr/local/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
  use epoll;
  worker_connections 65535;
}
http
{
  include mime.types;
  default_type application/octet-stream;
  log_format main  '$remote_addr - $remote_user [$time_local] "$request" '
               '$status $body_bytes_sent "$http_referer" '
               '"$http_user_agent" $http_x_forwarded_for';
  
  #charset gb2312;
 
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m;
     
  sendfile on;
  tcp_nopush on;
  keepalive_timeout 60;
  tcp_nodelay on;
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;
  gzip on; 
  gzip_min_length 1k;
  gzip_buffers 4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types text/plain application/x-javascript text/css application/xml;
  gzip_vary on;
 
  #limit_zone crawler $binary_remote_addr 10m;

#下面是server虚拟主机的配置
server
  {
    listen 80;#监听端口
    server_name localhost;#域名
    index index.html index.htm index.php;
    root /usr/local/nginx/html;  #站点目录
    location ~ .*\.(php|php5)?$
    {
      #fastcgi_pass unix:/tmp/php-cgi.sock;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
    
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
    {
      expires 30d;
      # access_log off;
    }
    
    location ~ .*\.(js|css)?$
    {
      expires 15d;
      # access_log off;
    }
    
    access_log off;
  }
}

检查配置文件nginx.conf的正确性命令:

nginx -t

启动nginx

nginx

使用IP地址访问站点:
在这里插入图片描述

安装Anaconda3

wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-4.3.0-Linux-x86_64.sh

对应python版本3.6
在这里插入图片描述
运行安装anaconda

yum install bzip2 -y
bash Anaconda3-4.3.0-Linux-x86_64.sh

设置安装路径:
在这里插入图片描述

报错:
在这里插入图片描述
solution:

yum install bzip2 -y

配置anaconda环境变量生效:
在这里插入图片描述

source /root/.bashrc

配置conda的源以加快三方模块的安装速度:

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes

conda list 验证是否安装成功!

创建Django项目:

首先创建虚拟环境:

 conda create -n django python=3.6

出现的错误及解决方案:
在这里插入图片描述

source activate django
conda install django
django-admin startproject launch
yum install tree
cd launch 
python manage.py runserver 0.0.0.0:8080

修改settings.py 文件,允许本机访问!

在这里插入图片描述
至此,一个最小的Django项目已成功运行,接下来部署至nginx实现多线程访问!

多线程部署

  1. 安装supervisor模块
pip install supervisor
  1. 检查supervisor是否安装成功
echo_supervisord_conf

创建目录,通过命令产生默认配置文件

mkdir /usr/supervisor
echo_supervisord_conf > /usr/supervisor/supervisord.conf
  1. 为了不将所有新增配置信息全写在一个配置文件里,这里新建一个文件夹
mkdir /usr/supervisor/supervisord.d/
  1. 修改系统配置文件supervisord.conf配置文件
vi /usr/supervisor/supervisord.conf
[include]
files = /usr/supervisor/supervisord.d/*.conf

若需要web查看进程,则去掉[inet_http_server]的注释

[inet_http_server]
port=127.0.0.1:9001   ;IP按需配置   *:9001代表所有主机均可访问该页面  
username=user              
password=123
  1. 启动supervisord
supervisord -c /usr/supervisor/supervisord.conf

6.1 通过 http://ip:9001/ 查看supervisor的web界面。这是已经启动后的界面,也可以通过lsof -i:9001来查看监听

  1. 增加一个配置文件,以便supervisor用来监控程序,在/usr/supervisor/supervisord.d目录下创建django.conf文件
    django.conf的内容为:
[group:djangos]
programs=django-8080,django-8081,django-8082,django-8083

[program:django-8080]
command=/root/anaconda3/envs/django/bin/python /root/launch/manage.py runserver 0.0.0.0:8080
directory=/root/launch
user=root
autorestart=true
redirect_stderr=true
stdout_logfile=/root/launch/django.log
loglevel=info

[program:django-8081]
command=/root/anaconda3/envs/django/bin/python /root/launch/manage.py runserver 0.0.0.0:8081
directory=/root/launch
user=root
autorestart=true
redirect_stderr=true
stdout_logfile=/root/launch/django.log
loglevel=info

[program:django-8082]
command=/root/anaconda3/envs/django/bin/python /root/launch/manage.py runserver 0.0.0.0:8082
directory=/root/launch
user=root
autorestart=true
redirect_stderr=true
stdout_logfile=/root/launch/django.log
loglevel=info

[program:django-8083]
command=/root/anaconda3/envs/django/bin/python /root/launch/manage.py runserver 0.0.0.0:8083
directory=/root/launch
user=root
autorestart=true
redirect_stderr=true
stdout_logfile=/root/launch/django.log
loglevel=info
  1. supervisor开机自启动,在目录/usr/lib/systemd/system/ 新建文件supervisord.service,并添加如下配置:
[Unit]
Description=Process Monitoring and Control Daemon
After=rc-local.service nss-user-lookup.target

[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /usr/supervisor/supervisord.conf;开机启动时执行
ExecStop=/usr/bin/supervisord shutdown
ExecReload=/usr/bin/supervisord reload
killMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

8.1 启动服务:

systemctl enable supervisord

8.2 验证一下是否为开机启动

s```
systemctl is-enabled supervisord

![在这里插入图片描述](https://img-blog.csdnimg.cn/46f67c99d05340d8a1709c735c81b8b2.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAc21pbGVhcHBsZXM=,size_20,color_FFFFFF,t_70,g_se,x_16)

9. supervisor相关命令

9.1 启动/查看进程

```shell
#启动进程
supervisord -c /usr/supervisor/supervisord.conf
#查看进程
ps aux | grep supervisord #查看进程是否存在
#杀死进程
kill 杀死进程pid

9.2 客户端使用

supervisorctl  #客户端命令
status    # 查看程序状态
stop djangos:*   # 关闭 djangos组 程序
start djangos:*  # 启动 djangos组 程序
restart djangos:*    # 重启 djangos组 程序
update    # 重启配置文件修改过的程序
  1. 在主机访问不同端口下的项目即可
    在这里插入图片描述

ngnix多进程访问

  1. nginx的安装http://www.runoob.com/linux/nginx-install-setup.html

  2. 编辑/usr/local/nginx/conf/nginx.conf文件

user www www;
worker_processes 2; #设置值和CPU核心数一致
error_log /usr/local/nginx/logs/nginx_error.log crit; #日志位置和日志级别
pid /usr/local/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
  use epoll;
  worker_connections 65535;
}

http
{
  include mime.types;
  default_type application/octet-stream;
  
	# charset gb2312;
     
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m;
     
  sendfile on;
  # tcp_nopush on;
  keepalive_timeout 60;
  #gzip on;

  upstream djangos{
  server 0.0.0.0:8080;
  server 0.0.0.0:8081;
  server 0.0.0.0:8082;
  server 0.0.0.0:8083;
	
  }

  #limit_zone crawler $binary_remote_addr 10m;
 #下面是server虚拟主机的配置
 	server{
    listen 80;#监听端口
    server_name localhost;#域名
    
    location / {
    proxy_pass_header Server;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Scheme $scheme;
    proxy_pass http://djangos;
    }
    
    location /static {
    autoindex on;
    alias /var/www/static/; # 记得chmod -R 755 static/修改权限
    }
    
    location /media {
    autoindex on;
    alias /var/www/media/;
    }
    
   # access_log off;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html{
		    root html;
    }
    }
}
  1. 重启nginx服务器
    在/root/.bashrc文件末尾添加如下语句将nginx添加至环境变量
export PATH=$PATH:/usr/local/nginx/sbin
nginx -t
nginx -s reload

在这里插入图片描述
至此,项目已经完成部署了!

在centos中安装mysql

首先安装相应的工具:
yum install -y wget
安装MySQL官方的 Yum Repository
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql57-community-release-el7-10.noarch.rpm

这个安装稍微要几分钟哈…

安装mysql
yum install -y mysql-server

在这里插入图片描述

启动mysql
systemctl start  mysqld.service

查看mysql的状态:

systemctl status mysqld.service

在这里插入图片描述

配置mysql

查看mysql的初始密码:

grep "password" /var/log/mysql/mysqld.log

注意文件mysqld.log所在的路径!
在这里插入图片描述

修改mysql的密码:

mysql -uroot -p
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';

参考链接:https://blog.csdn.net/jason19905/article/details/81366202

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值