Fork me on GitHub
## 1 阿里云购买

```python
# 1 写好的项目,在本地跑,别问访问不到,没有公网ip
# 2 远程连接阿里云的服务器47.103.156.13(xshell,finalShell)
# 3 ssh连接
# 4 配置前后端项目
# 5 编译前端项目,要把前端项目传到服务器(可以用软件)
scp -r dist root@47.103.156.13:~
# 6
```



## 2 云服务器安装redis

```python
1)前往用户根目录
>: cd ~

2)下载redis-5.0.5
>: wget http://download.redis.io/releases/redis-5.0.5.tar.gz
>: scp -r C:\Users\dell\Desktop\pkg\redis-5.0.5.tar.gz root@39.99.192.127:~

3)解压安装包
>: tar -xf redis-5.0.5.tar.gz

4)进入目标文件
>: cd redis-5.0.5

5)编译环境
>: make

6)复制环境到指定路径完成安装
>: cp -r ~/redis-5.0.5 /usr/local/redis


9)建立软连接
>: ln -s /usr/local/redis/src/redis-server /usr/bin/redis-server
>: ln -s /usr/local/redis/src/redis-cli /usr/bin/redis-cli

10)后台运行redis
>: cd /usr/local/redis
>: redis-server &

ctrl + c 停止

11)测试redis环境
>: redis-cli
ctrl + c

12)关闭redis服务
>: pkill -f redis -9
```



## 3 云服务器安装mysql

```python
1)前往用户根目录
>: cd ~

2)下载mysql57
>: wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm

也可以本地上传,这条命令要在本地终端上执行
>: scp -r C:mysql57-community-release-el7-10.noarch.rpm root@39.99.192.127:~

3)安装mysql57
>: yum -y install mysql57-community-release-el7-10.noarch.rpm
>: yum -y install mysql-community-server

4)启动mysql57并查看启动状态
>: systemctl start mysqld.service
>: systemctl status mysqld.service

5)查看默认密码并登录
>: grep "password" /var/log/mysqld.log
>: mysql -uroot -p

6)修改密码
>: ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';
```



## 4 云服务器安装python

```python
1)前往用户根目录
>: cd ~

2)下载 或 上传 Python3.6.7
# 服务器终端
>: wget https://www.python.org/ftp/python/3.6.7/Python-3.6.7.tar.xz

# 本地终端,给服务器上传
>: scp -r 本地Python-3.6.7.tar.xz ssh root@39.99.192.127:服务器路径
>: scp -r C:\Users\dell\Desktop\pkg\Python-3.6.7.tar.xz ssh root@39.99.192.127~

3)解压安装包
>: tar -xf Python-3.6.7.tar.xz

4)进入目标文件
>: cd Python-3.6.7

5)配置安装路径:/usr/local/python3
>: ./configure --prefix=/usr/local/python3

6)编译并安装
>: make && sudo make install

7)建立软连接:终端命令 python3,pip3
>: ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3
>: ln -s /usr/local/python3/bin/pip3.6 /usr/bin/pip3

8)删除安装包与文件:
>: rm -rf Python-3.6.7
>: rm -rf Python-3.6.7.tar.xz
```



## 5 云服务器安装nginx

```python
1)前往用户根目录
>: cd ~

2)下载nginx1.13.7
>: wget http://nginx.org/download/nginx-1.13.7.tar.gz

3)解压安装包
>: tar -xf nginx-1.13.7.tar.gz

4)进入目标文件
>: cd nginx-1.13.7

5)配置安装路径:/usr/local/nginx
>: ./configure --prefix=/usr/local/nginx

6)编译并安装
>: make && sudo make install

7)建立软连接:终端命令 nginx
>: ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx

8)删除安装包与文件:
>: cd ~
>: rm -rf nginx-1.13.7
>: rm -rf nginx-1.13.7.tar.xz

9)测试Nginx环境,服务器运行nginx,本地访问服务器ip
>: nginx
>: 服务器绑定的域名 或 ip:80


#Nginx命令
1)启动
>: nginx

2)关闭nginx
>: nginx -s stop

3)重启nginx
>: nginx -s reload

4)查看端口,强行关闭
>: ps -aux|grep nginx
>: kill <pid:进程编号>


#
Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器
Apache和Nginx最核心的区别在于 apache 是同步多进程模型,一个连接对应一个进程;而 nginx 是异步的,多个连接(万级别)可以对应一个进程

http请求转发
反向代理服务器
负载均衡
动静分离
```



## 6 nginx转发静态文件(前端项目)

```python
# 1 mv ~/dist /home/html
# 2 cd /usr/local/nginx/conf
# 3 vim 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; # 改为自己的域名,没域名修改为127.0.0.1:80
charset utf-8;
location / {
root /home/html; # html访问路径
index index.html; # html文件名称
try_files $uri $uri/ /index.html; # 解决单页面应用刷新404问题
}
}
}

esc
:wq # 保存并推出

# 重启nginx
nginx -s reload

# 前端项目就有了
```



## 7 云服务器转发动态请求(uwsgi+django项目)

```python
# 1 mkdir /home/project
# 2 cd /home/project
# 3 git clone xxxx
# 4 安装虚拟环境,创建虚拟环境,安装项目依赖
pip install -r requirements.txt (在指定的目录下)
# 5 pip install uwsgi (虚拟和真实环境都需要安装)
# 6 进行uwsgi服务配置,内容如下
vim /home/project/luffyapi/luffyapi.xml

<uwsgi>
<socket>127.0.0.1:8808</socket> <!-- 内部端口,自定义 -->
<chdir>/home/project/luffyapi/</chdir> <!-- 项目路径 -->
<module>luffyapi.wsgi</module> <!-- luffyapi为wsgi.py所在目录名-->
<processes>4</processes> <!-- 进程数 -->
<daemonize>uwsgi.log</daemonize> <!-- 日志文件 -->
</uwsgi>

# 7 去向Nginx配置目录,备份配置,完全更新配置:填入下方内容
>: vim /usr/local/nginx/conf/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; # 改为自己的域名,没域名修改为127.0.0.1:80
charset utf-8;
location / {
root /home/html; # html访问路径
index index.html; # html文件名称
try_files $uri $uri/ /index.html; # 解决单页面应用刷新404问题
}
}
# 新增的server
server {
listen 8000;
server_name 127.0.0.1; # 改为自己的域名,没域名修改为127.0.0.1:80
charset utf-8;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8808; # 端口要和uwsgi里配置的一样
uwsgi_param UWSGI_SCRIPT luffyapi.wsgi; #wsgi.py所在的目录名+.wsgi
uwsgi_param UWSGI_CHDIR /home/project/luffyapi/; # 项目路径
}
}
}


#9 mysql的root用户,登录,创建表luffyapi,创建用户,授权
1)管理员连接数据库
>: mysql -uroot -p

2)创建数据库
>: create database luffyapi default charset=utf8;

3)设置权限账号密码:账号密码要与项目中配置的一致
>: grant all privileges on luffyapi.* to 'luffyapi'@'%' identified by 'Luffy123?';
>: grant all privileges on luffyapi.* to 'luffyapi'@'localhost' identified by 'Luffy123?';
>: flush privileges;

4)退出mysql
>: quit;
# 10 数据迁移创建超级用户
python manage_pro.py makemigraions
python manage_pro.py migrate
python manage_pro.py createsuperuser


#11 启动uwsgi和nginx(后端项目就可以了)
1)启动uwsgi
>: uwsgi -x /home/project/luffyapi/luffyapi.xml
2)重启nginx
>: nginx -s reload
```

posted on 2020-09-05 14:46  OBOS  阅读(142)  评论(0编辑  收藏  举报