1 上线前准备
-修改前端访问的后端地址
export default {
BASE_URL: 'http://8.130.18.221:8000/api/v1/'
}
-把vue项目编译成 html,css,js
npm run build
-把dist文件夹压缩,待命
2 阿里云购买
-项目跑在服务器上
-购买公网ip:别人访问我们的ip,就能访问到我们的项目
-购买域名:备案---》访问域名---》转发到你的ip
-按量付费---》冲100块---》买4台
-https://ecs.console.aliyun.com/home
http://www.hostbuf.com/t/988.html
不下这个软件:打开cmd :ssh root@8.130.18.221 输入密码
一堆linux命令
2.1 上线架构图
3 安装git
yum install git -y
yum -y groupinstall "Development tools"
yum groupinstall “Development Tools” --setopt=group_package_types=mandatory,default,optional
yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel psmisc libffi-devel -y
4 安装mysql
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-server_5.7.42-1ubuntu18.04_amd64.deb-bundle.tar
tar -xvf mysql-server_5.7.42-1ubuntu18.04_amd64.deb-bundle.tar /opt/mysql/
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get install libaio1
sudo apt-get install libtinfo5
sudo apt-get install apt-utils
sudo apt-get install libnuma1
sudo apt-get -f install
sudo apt-get -f install libmecab2
sudo dpkg -i mysql-common_5.7.42-1ubuntu18.04_amd64.deb
sudo dpkg-preconfigure mysql-community-server_5.7.42-1ubuntu18.04_amd64.deb
root Lin12345?
sudo dpkg -i libmysqlclient20_5.7.42-1ubuntu18.04_amd64.deb
sudo dpkg -i libmysqlclient-dev_5.7.42-1ubuntu18.04_amd64.deb
sudo dpkg -i libmysqld-dev_5.7.42-1ubuntu18.04_amd64.deb
sudo dpkg -i mysql-community-client_5.7.42-1ubuntu18.04_amd64.deb
sudo dpkg -i mysql-client_5.7.42-1ubuntu18.04_amd64.deb
sudo dpkg -i mysql-common_5.7.42-1ubuntu18.04_amd64.deb
sudo dpkg -i mysql-community-server_5.7.42-1ubuntu18.04_amd64.deb
sudo dpkg -i mysql-server_5.7.42-1ubuntu18.04_amd64.deb
service mysql start
-- 密码是在安装的时候设置的密码
mysql -uroot -p
-- 查询所有库
show databases;
update mysql.user set host='%' where user='root';
flush privileges;
--------------------------------------------------------------------------------
cd ~
wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql57-community-release-el7-10.noarch.rpm
yum install mysql-community-server --nogpgcheck -y
systemctl start mysqld
systemctl status mysqld
grep "password" /var/log/mysqld.log
mysql -uroot -p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Lin12345?';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Lin12345?' WITH GRANT OPTION;
FLUSH PRIVILEGES;
use mysql;
select User,authentication_string,Host from user;
systemctl stop firewalld.service
5 安装redis
wget https://packages.redis.io/redis-stack/redis-stack-server-6.2.6-v7.rhel7.x86_64.tar.gz
tar -xf redis-stack-server-6.2.6-v7.rhel7.x86_64.tar.gz
mv redis-stack-server-6.2.6-v7/ redis
cd redis/bin
./redis-server
-方式一:把bin路径加入到环境变量
-方式二:使用软连接, /usr/bin/ 本身在环境变量 ,在任意路径敲redis-server redis-cli都能找到
ln -s /root/redis/bin/redis-server /usr/bin/redis-server
ln -s /root/redis/bin/redis-cli /usr/bin/redis-cli
cd /bin 然后输入:
ls |grep redis
redis-server &
wget http://download.redis.io/releases/redis-6.2.6.tar.gz
wget http://download.redis.io/releases/redis-7.0.2.tar.gz
tar -xf redis-7.0.2.tar.gz
cd redis-6.2.6
make
cp -r ~/redis-7.0.2 /usr/local/redis
vim /usr/local/redis/redis.conf
在它里面把 daemonize 后面的no改为 yes
>: esc
>: :wq
ln -s /usr/local/redis/src/redis-server /usr/bin/redis-server1
ln -s /usr/local/redis/src/redis-cli /usr/bin/redis-cli1
>: cd /usr/local/redis
>: redis-server1 ./redis.conf
redis-cli
pkill -f redis -9
redis-cli shutdown
6 安装python解释器
yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel psmisc libffi-devel zlib* libffi-devel -y
cd ~
wget https://registry.npmmirror.com/-/binary/python/3.9.10/Python-3.9.10.tgz
tar -xf Python-3.9.10.tgz
cd Python-3.9.10
./configure --prefix=/usr/local/python39
yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel psmisc libffi-devel zlib* libffi-devel -y
make && make install
ln -s /usr/local/python39/bin/python3 /usr/bin/python3.9
ln -s /usr/local/python39/bin/pip3 /usr/bin/pip3.9
python 2.x pip
python3 3.6 pip3
python3.9 3.9 pip3.9
rm -rf Python-3.9.10
rm -rf Python-3.9.10.tar.xz
wget https://www.python.org/ftp/python/3.11.2/Python-3.11.2.tgz
tar -xf Python-3.11.2.tgz
cd Python-3.11.2
./configure --prefix=/usr/local/python311
yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel psmisc libffi-devel zlib* libffi-devel -y
make && make install
cd /usr/local/python311/bin
ln -s /usr/local/python311/bin/python3 /usr/bin/python3
ln -s /usr/local/python311/bin/pip3 /usr/bin/pip3
python 2.x pip
python3 3.6 pip3
python3.11 3.11 pip3.11
rm -rf Python-3.11.2
rm -rf Python-3.11.2.tgz
7 安装虚拟环境
7.1 配置pip源
https://www.cnblogs.com/liuqingzheng/articles/17168816.html
1)创建pip配置路径
mkdir ~/.pip
2)进入目录编辑配置文件:填入下方内容
cd ~/.pip && vim pip.conf
[global]
index-url = https://mirrors.aliyun.com/pypi/simple
[install]
use-mirrors =true
mirrors =https://mirrors.aliyun.com/pypi/simple
trusted-host =mirrors.aliyun.com
7.2 安装虚拟环境
python3 -m venv virtualenv pip3 install virtualenv
1)安装依赖
>: pip3.9 install virtualenv
>: pip3.11 install virtualenvwrapper
2)建立虚拟环境软连接
>: ln -s /usr/local/python39/bin/virtualenv /usr/bin/virtualenv
3)配置虚拟环境:填入下方内容
>: vi ~/.bash_profile
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3.9
source /usr/local/python39/bin/virtualenvwrapper.sh
esc
>: :wq
source ~/.bash_profile
/root/.virtualenvs
mkvirtualenv -p python3.9 luffy
8 安装uwsgi
1)在真实环境下安装
pip3.9 install uwsgi
2)建立软连接
ln -s /usr/local/python39/bin/uwsgi /usr/bin/uwsgi
9 安装nginx
- 做请求转发
- 静态资源代理
- 负载均衡
cd ~
wget http://nginx.org/download/nginx-1.13.7.tar.gz
tar -xf nginx-1.13.7.tar.gz
cd nginx-1.13.7
./configure --prefix=/usr/local/nginx
make && make install
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
cd ~
rm -rf nginx-1.13.7
rm -rf nginx-1.13.7.tar.xz
nginx
服务器绑定的域名 或 ip:80
/usr/local/nginx/html
ps aux | grep nginx
10 部署前端项目
yum install lrzsz -y
yum install unzip -y
rz
unzip dist.zip
mv /root/dist /home/html
cd /usr/local/nginx/conf
mv nginx.conf nginx.conf.bak
vi nginx.conf
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;
}
}
}
nginx -s reload
11 部署后台项目
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'luffy_api.settings.prod')
pip3 freeze > requirements.txt
mkdir /home/project
cd /home/project
git clone https://gitee.com/liuqingzheng/luffy_api.git
进入到虚拟环境
workon luffy
cd /home/project/luffy_api
pip install -r requirements.txt
yum install python3-devel -y
yum install mysql-devel --nogpgcheck -y
pip install mysqlclient
pip install uwsgi
mysql -uroot -p
create database luffy default charset=utf8;
grant all privileges on luffy.* to 'luffy'@'%' identified by 'Luffy123?';
grant all privileges on luffy.* to 'luffy'@'localhost' identified by 'Luffy123?';
flush privileges;
quit;
python manage.py makemigrations
python manage.py migrate
-写一个uwsgi的配置文件,在项目路径下,新建一个 luffyapi.xml
<uwsgi>
<socket>127.0.0.1:8888</socket>
<chdir>/home/project/luffy_api/</chdir>
<module>luffy_api.wsgi</module>
<processes>4</processes>
<daemonize>uwsgi.log</daemonize>
</uwsgi>
-使用uwsgi启动
uwsgi -x luffyapi.xml
-查看是否正常运行
ps aux |grep uwsgi
cd /usr/local/nginx/conf
vi nginx.conf
server {
listen 8000;
server_name 127.0.0.1;
charset utf-8;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8888;
uwsgi_param UWSGI_SCRIPT luffy_api.wsgi;
uwsgi_param UWSGI_CHDIR /home/project/luffy_api/;
}
}
nginx -s reload
11.1 配置后台admin访问
STATIC_ROOT = '/home/project/luffy_api/luffy_api/static/'
mkdir /home/project/luffy_api/luffy_api/static
python manage_prod.py collectstatic
server {
listen 8000;
server_name 127.0.0.1;
charset utf-8;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8888;
uwsgi_param UWSGI_SCRIPT luffy_api.wsgi;
uwsgi_param UWSGI_CHDIR /home/project/luffy_api/;
}
location /static {
alias /home/project/luffy_api/luffy_api/static;
}
}
12 安全组