新服务器

96
人生苦短啊
2019.05.20 09:59* 字数 502

ubuntu更换镜像地址

#使用vim编辑
sudo vim /etc/apt/sources.list

编辑,把默认源注释掉在最前面加上这些

deb http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse

更新缓存

sudo apt-get update
sudo apt-get upgrade

1.python下载

sudo mkdir /usr/local/python3
cd /home/cnc
sudo wget --no-check-certificate https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz
sudo tar -xzvf Python-3.6.8.tgz
cd Python-3.6.8

2、接着编译安装

sudo apt-get install aptitude
sudo aptitude install tk-dev tkinter
sudo ./configure --prefix=/usr/local/python3 --enable-optimizations
sudo make && make install(可能会很久)

make报错试试

sudo aptitude install make
再不行sudo aptitude upgrade

3. 创建软链接

    sudo ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3
    sudo ln -s /usr/local/python3/bin/pip3.6 /usr/bin/pip
    如果报错文件夹已经存在,就rm -rf ***删除了之后再关联

4. 修改pip镜像

修改 ~/.pip/pip.conf (没有就创建一个文件夹及文件。文件夹要加“.”,表示是隐藏文件夹)

sudo mkdir ~/.pip
vim pip.conf

内容如下:

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host=mirrors.aliyun.com

5.安装mysql8.0

wget https://dev.mysql.com/get/mysql-apt-config_0.8.10-1_all.deb
dpkg -i mysql-apt-config_0.8.10-1_all.deb

选第一个
选8.0

然后选OK

#更新一下资源
sudo aptitude update 
# 安装mysql-server
sudo aptitude install mysql-server # 如果有提示就输入yes
# 配置密码
# 选第一个Use Strong Password Encryption (RECOMMENDED)  

数据导入
把本地数据库导出成sql文件,放在服务器上/home/cnc/back

# 进mysql命令行
mysql -uroot -p
mysql> CREATE DATABASE aerdb2 DEFAULT CHARSET=utf8 DEFAULT COLLATE utf8_unicode_ci;
mysql> use aerdb2;
mysql> source /home/cnc/back/aerdb2.sql;
# over

6.安装nginx

sudo aptitude install nginx
# 启动nginx 输入端口号即可看到nginx欢迎页面说明安装成功
service nginx start

7.安装redis

根据官网指导。如下执行shell命令:

sudo wget http://download.redis.io/releases/redis-5.0.5.tar.gz
sudotar xzf redis-5.0.5.tar.gz
sudo cd redis-5.0.5
sudo make
sudo make install PREFIX=/usr/local/redis
sudo mkdir /usr/local/redis/etc
sudo mv redis.conf /usr/local/redis/etc
sudo /usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf # 可以测试是否配置成功

解决因为强制把redis快照关闭了导致不能持久化的问题
vim /usr/local/redis/etc/redis.conf
定位到stop-writes-on-bgsave-error字符串所在位置,接着把后面的yes设置为no即可。

8.安装Supervisor

Superviosr是一个进程监管的工具。简而言之,Superviosr可以保证你的程序在服务器开机时自动启动以及程序意外终止时重新启动。通过下面的命令即可安装:

sudo aptitude install supervisor

9.把source迁移到服务器

npm run build # 打包前端生成一个dist文件夹

复制到后台根目录/back
使用ftp工具(xftp6),把整个后台文件夹back复制到/home/cnc/

10. 安装第三方包

sudo pip install -i https://pypi.doubanio.com/simple/ -r /home/cnc/back/requirements.txt requirements.txt

11.配置uwsgi

先命令行启动一下后台看看有没有问题

cd /home/cnc/back
python3 manage.py runserver
sudo pip install uwsgi

如果没有问题
在项目根目录创建uwsgi.ini文件
sudo vim uwsgi.ini

[uwsgi]
socket = /home/cnc/back/uwsgi.sock
uid=cnc
gid=cnc
workers = 3
master=true
harakiri=30
post-buffering=4096
chdir = /home/cnc/back/
touch-reload = /home/cnc/back/
py-auto-reload = 1
module= back.wsgi
daemonize = /home/cnc/back/uwsgi.log
buffer-size=65535
pidfile = /var/run/uwsgi.pid

启动命令
/usr/local/python3/bin/uwsgi -i uwsgi.ini
查看是否启动成功
ps -ef|grep uwsgi
关闭uwsgi
sudo pkill -9 uwsgi

12.nginx配置

创建一个配置文件
sudo vim /etc/nginx/sites-available/aeromet.conf

server{
        listen 9000;
        server_name  cnc;
        access_log /var/log/nginx/aeromet_access.log;
        error_log /var/log/nginx/aeromet_error.log;

        error_page 405 =200 $uri;

        charset utf-8;
        gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream;

        location / {
            root  /home/cnc/back/dist;
            rewrite .* /index.html break;
        }

        location /static { 
            alias /home/cnc/back/dist/static;
        } 
        location /aeromet/api/ {
            uwsgi_connect_timeout 30;
            include uwsgi_params;
            uwsgi_pass unix:/home/cnc/back/uwsgi.sock;

        }
        error_page  404 400 401 403 405    /jQuery404/40x.html;
    }

使其生效
sudo ln -s /etc/nginx/sites-available/aeromet.conf /etc/nginx/sites-enabled/aeromet.conf
修改 /etc/nginx/nginx.conf
user cnc;
把www替换成cnc
重启nginx
sudo service nginx restart

13.Supervisor配置

编辑/etc/supervisor/supervisord.conf,最下面加上这些配置

[program:redis]
command=redis-server /usr/local/redis/etc/redis.conf
user = cnc
numprocs=1
autostart=true
autorestart=true
# daemonize=no
stopsignal=INT
stdout_logfile=/home/cnc/back/log/redis.log
stderr_logfile=/home/cnc/back/log/redis_error.log


[program:celery.worker.default]
user = cnc

directory=/home/cnc/back
command=/usr/local/python3/bin/celery -A back worker --loglevel info --logfile log/celery_worker.log
numprocs=1
autostart=true
autorestart=true


stopsignal=INT
stdout_logfile=/home/cnc/back/log/worker.log
stderr_logfile=/home/cnc/back/log/worker_error.log

[program:celery.beat.default]
user = cnc
directory=/home/cnc/back
command=/usr/local/python3/bin/celery -A back beat --loglevel info --logfile log/celery_beat.log

numprocs=1
autostart=true
autorestart=true


stopsignal=INT
stdout_logfile=/home/cnc/back/log/beat.log
stderr_logfile=/home/cnc/back/log/beat_error.log

[program:uwsgi]
directory=/home/cnc/back
command=/usr/local/python3/bin/uwsgi -i uwsgi.ini
user = cnc
autostart=true
autorestart=true
stopsignal=INT
stdout_logfile=/home/cnc/back/log/uwsgi.log
stderr_logfile=/home/cnc/back/log/uwsgi_error.log
随笔