• 初始化服务器基础环境, 搭建开发环境, 项目上线


    初始化服务器基础环境

    设置服务器安全组

    • 授权对象: 0.0.0.0/0, 允许所有ip及端口访问

    • ssh协议默认端口号: 22, http协议的默认端口号: 80, https协议默认端口号: 443

    '''
    连接服务器: ssh root@39.99.156.25
    
    编辑文件: vim ~/.bash_profile, 类似于windows的环境变量
    	ggdG: 删除文件中的全部内容
    	
    	export PATH=$PATH:$HOME/bin
    	PS1='Path:w
    >:'
    
    重新加载文件: source ~/.bash_profile, [ˈprəʊfaɪl], 轮廓
    
    yum -y update 
    yum -y groupinstall "Development tools" 
    yum -y install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel psmisc libffi-devel 
    '''
    

    搭建开发环境

    安装mysql

    '''
    上传本地安装包: scp -r 本地安装包路径 root@39.99.156.25:~ 
    
    安装: 
    	yum -y install mysql57-community-release-el7-10.noarch.rpm 
    	yum -y install mysql-community-server
    
    启动mysql57并查看启动状态:
    	systemctl start mysqld.service
    	systemctl status mysqld.service
    
    查看默认密码并登录:
    	grep "password" /var/log/mysqld.log
    	mysql -uroot -p
    
    修改密码:
    	ALTER USER 'root'@'localhost' IDENTIFIED BY 'Cql123456';
    
    密码出现问题: https://www.cnblogs.com/black-fact/p/11613361.html
    '''
    

    安装redis

    '''
    上传本地压缩包: scp -r 本地压缩包路径 root@39.99.156.25:~ 
    
    解压: tar -xf redis-5.0.5.tar.gz 
    
    进入目标文件夹: cd redis-5.0.5
    
    编译: make
    
    将编译后的文件夹复制到指定路径安装: cp -r ~/redis-5.0.5 /usr/local/redis 
    
    修改配置文件: vim /usr/local/redis/redis.conf
    	daemonize yes
    
    建立软链接:
    	ln -s /usr/local/redis/src/redis-server /usr/bin/redis-server 
    	ln -s /usr/local/redis/src/redis-cli /usr/bin/redis-cli
    
    后台启动redis: redis-server &
    
    关闭redis服务: pkill -f redis -9
    '''
    

    安装python3.6

    '''
    上传本地压缩包: scp -r 本地压缩包路径 root@39.99.156.25:~
    
    解压: tar -xf Python-3.6.7.tar.xz
    
    进入目标文件夹: cd Python-3.6.7
    
    配置安装路径: ./configure --prefix=/usr/local/python3
    
    编译并安装: make && sudo make install
    
    建立软链接:
    	ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3
    	ln -s /usr/local/python3/bin/pip3.6 /usr/bin/pip3
    
    删除安装包和压缩包
    	rm -rf Python-3.6.7
    	rm -rf Python-3.6.7.tar.xz
    
    配置pip源
    	mkdir ~/.pip
    	cd ~/.pip && vim pip.conf
    	
    	[global]
    	index-url = http://pypi.douban.com/simple
    	[install]
    	use-mirrors =true
    	mirrors =http://pypi.douban.com/simple/
    	trusted-host =pypi.douban.com
    '''
    

    安装uwsgi

    '''
    在真实环境下安装: pip3 install uwsgi
    
    建立软链接: ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi
    '''
    

    虚拟环境管理模块

    '''
    安装:
    	pip3 install virtualenv
    	pip3 install virtualenvwrapper
    
    编辑文件: vim ~/.bash_profile
        export WORKON_HOME=~/.virtualenvs  # 虚拟环境的存放目录
        export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
        export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/python3/bin/virtualenv
        source /usr/local/python3/bin/virtualenvwrapper.sh  # 打开终端自动启用
    
    重新加载文件: source ~/.bash_profile
    
    参考: https://www.cnblogs.com/freely/p/8022923.html
    '''
    

    安装nginx

    '''
    上传本地压缩包: scp -r 本地压缩包路径 root@39.99.156.25:~
    
    解压:tar -xf nginx-1.13.7.tar.gz
    
    进入目标文件夹: cd nginx-1.13.7
    
    配置安装路径: ./configure --prefix=/usr/local/nginx
    
    编译并安装: make && sudo make install
    
    建立软链接: ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
    
    删除安装包和压缩包
    	rm -rf nginx-1.13.7
    	rm -rf nginx-1.13.7.tar.gz
    '''
    

    项目上线

    前端

    '''
    # ...luffycitysrcassetsjssettings.js
    export default {
        base_url: 'http://39.99.156.25:8000',  // 后端url地址
    }
    
    将本地项目打包: cnpm run build
    
    上传打包文件: scp -r 打包文件路径 root@39.99.156.25:/home/html
    
    备份文件: cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
    
    编辑文件: vim /usr/local/nginx/conf/nginx.conf
    	ggdG
    
        events {
            worker_connections  1024;
        }
        http {
            include       mime.types;
            default_type  application/octet-stream;
            sendfile        on;
            server {
                listen 80;
                server_name  127.0.0.1;  
                charset utf-8;
                location / {
                    root /home/html;  
                    index index.html; 
                    try_files $uri $uri/ /index.html;  # 解决vue单页面刷新报错
                }
            }
        }   
    
    启动nginx: nginx
    关闭nginx: nginx -s stop
    更改配置文件后重载nginx: nginx -s reload
    '''
    

    后端

    '''
    # ...luffyapiprod_manage.py
    ...
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "luffyapi.settings.prod_settings")
        ...
    
    
    # ...luffyapiluffyapiwsgi.py
    ...
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "luffyapi.settings.prod_settings")
    ...
    
    
    # ...luffyapiluffyapisettingsprod_settings.py
    ...
    DEBUG = False
    
    ALLOWED_HOSTS = ['39.99.156.25']
    
    ...
    BACKEND_BASE_URL = 'http://39.99.156.25:8000'  # 后端url根路径
    FRONTEND_BASE_URL = 'http://39.99.156.25:80'  # 前端url根路径
    ...
    
    # drf框架的配置
    REST_FRAMEWORK = {
        'DEFAULT_RENDERER_CLASSES': [
            'rest_framework.renderers.JSONRenderer',
            # 'rest_framework.renderers.BrowsableAPIRenderer',
        ],
        ...,
    }
    ...
    
    
    # 导出项目依赖: pip3 freeze > dependencies.txt
    
    # 提交项目到远程仓库: 
    	git pull rmt_luffyapi master
    	git push rmt_luffyapi master
    
    # 在服务器上创建路飞后端项目存放目录: mkdir /home/project
    
    # 将远程仓库的路飞后端项目克隆到服务器: cd /home/project && git clone https://gitee.com/cql406454833/luffyapi.git
    
    # 在服务器上创建虚拟环境: mkvirtualenv luffyapi
    # 切换到新创建的虚拟环境: workon luffyapi
    # 在虚拟环境中安装项目所需依赖:
    	pip install uwsgi
    	pip install -r /home/project/luffyapi/dependencies.txt
    
    
    创建并编辑uwsgi配置文件: vim /home/project/luffyapi/luffyapi.xml
        <uwsgi>    
           <socket>127.0.0.1:8808</socket>  <!--uwsgi服务的端口--> 
           <chdir>/home/project/luffyapi/</chdir>  <!--路飞后端项目的绝对路径-->            
           <module>luffyapi.wsgi</module>  <!--wsgi文件的相对路径--> 
           <processes>4</processes>  <!--进程数-->     
           <daemonize>uwsgi.log</daemonize>  <!--uwsgi日志文件的相对路径-->
        </uwsgi>
    
    
    nginx反向代理:
    	vim /usr/local/nginx/conf/nginx.conf
    	
        ...
        http {
            ...
            # 新增的server
            server {
                listen 8000;
                server_name  127.0.0.1;
                charset utf-8;
                location / {
                   include uwsgi_params;
                   uwsgi_pass 127.0.0.1:8808;  # 反向代理本地的8080端口
                   uwsgi_param UWSGI_SCRIPT luffyapi.wsgi;  # uwsgi依赖于wsgi
                   uwsgi_param UWSGI_CHDIR /home/project/luffyapi/;  # 路飞后端项目的绝对路径
                }
            }
        } 
        
    
    数据库设置:
    	管理员登录: mysql -uroot -pCql123456
    	创建数据库: create database luffyapi default charset=utf8;
    	创建用户并设置权限: grant all privileges on luffyapi.* to 'cql'@'%' identified by 'Cql123456';
    	保存设置: flush privileges;
    	完成路飞后端项目的数据库迁移: python /home/project/luffyapi/prod_manage.py migrate
    	创建admin后台超级用户: python /home/project/luffyapi/prod_manage.py createsuperuser
    
    
    nginx动静分离:
    	编辑文件: vim /home/project/luffyapi/luffyapi/settings/prod_settings.py
    		STATIC_ROOT = '/home/project/luffyapi/luffyapi/static'  
    		STATICFILES_DIRS = (os.path.join(BASE_DIR, "luffyapi", "static"),)
    	
    	迁移静态文件: 
    		mkdir /home/project/luffyapi/luffyapi/static
    		python /home/project/luffyapi/prod_manage.py collectstatic
    	
    	编辑文件: vim /usr/local/nginx/conf/nginx.conf
            ...
            http {
                ...
                server {
                    ...
                    # nginx动静分离
                    location /static {
                        alias /home/project/luffyapi/luffyapi/static;
                    }
                }
            }
            
    
    # 重启uwsgi:
    	pkill -f uwsgi -9
    	uwsgi -x /home/project/luffyapi/luffyapi.xml
    	
    更改配置文件后重载nginx: nginx -s reload
    
    录入数据库数据: E:feiqiuday83
    '''
    
  • 相关阅读:
    Django(app的概念、ORM介绍及编码错误问题)
    Django(完整的登录示例、render字符串替换和redirect跳转)
    Construct Binary Tree from Preorder and Inorder Traversal
    Single Number II
    Single Number
    Binary Tree Level Order Traversal II
    Binary Tree Level Order Traversal
    Binary Tree Zigzag Level Order Traversal
    Recover Binary Search Tree
    Add Binary
  • 原文地址:https://www.cnblogs.com/-406454833/p/12709363.html
Copyright © 2020-2023  润新知