购买服务器-项目环境搭建-上线发布
购买服务器
# 购买阿里云服务器 # 短期或是测试使用,创建 按量收费 服务器,可以随时删除,删除后不再计费,但要保证账户余额100元以上
连接服务器
1)账号 >: ssh root@39.98.144.221 2)密码 >: ********
服务器命名
管理员权限
1)以下所有的服务器命令均可以在管理员权限下执行
>: sudo 命令
配置终端
1)编辑配置文件 >: vim ~/.bash_profile 2)将原来内容全部删除掉 >: ggdG 3)进入编辑状态:填入下方两行 >: i export PATH=$PATH:$HOME/bin PS1='Path:\w\n>:' 4)退出编辑状态 >: esc 5)保存修改并退出 >: :wq 6)生效配置 >: source ~/.bash_profile
重要
更新系统软件包
>>: yum update -y
>: yum -y groupinstall "Development tools" >: yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel psmisc libffi-devel
1)前往用户根目录 >: cd ~ 2)下载mysql57 >: wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm 也可以本地上传,这条命令要在本地终端上执行 >: scp -r C:\Users\dell\Desktop\pkg\mysql57-community-release-el7-10.noarch.rpm root@39.98.144.221:~ 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'; >: ALTER USER 'root'@'localhost' IDENTIFIED BY 'Owen1234?';
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.98.144.221:~ 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 7)配置redis可以后台启动:修改下方内容 >: vim /usr/local/redis/redis.conf daemonize yes 8)完成配置修改 >: esc >: :wq 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 >: redis-server & ctrl + c 11)测试redis环境 >: redis-cli ctrl + c 12)关闭redis服务 >: pkill -f redis -9
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.98.144.221:服务器路径 >: scp -r C:\Users\dell\Desktop\pkg\Python-3.6.7.tar.xz ssh root@39.98.144.221:~ 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
1)创建pip配置路径 >: mkdir ~/.pip 2)进入目录编辑配置文件:填入下方内容 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
1)在真实环境下安装 pip3 install uwsgi 2)建立软连接 ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi
1)安装依赖 >: pip3 install virtualenv >: pip3 install virtualenvwrapper 2)建立虚拟环境软连接 >: ln -s /usr/local/python3/bin/virtualenv /usr/bin/virtualenv 3)配置虚拟环境:填入下方内容 >: vim ~/.bash_profile VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 source /usr/local/python3/bin/virtualenvwrapper.sh 4)退出编辑状态 >: esc 5)保存修改并退出 >: :wq 6)更新配置文件内容 >: source ~/.bash_profile 7)虚拟环境默认根目录:~/.virtualenvs
1)创建虚拟环境 >: mkvirtualenv test_venv 2)安装依赖 >: pip install django 3)前往目标目录,创建项目工作目录,再进入 >: cd /home >: mkdir project >: cd project 4)创建Django项目,并进入 >: django-admin startproject test_site >: cd test_site 5)完成项目配置:修改下方几行内容 >: vim /home/project/test_site/test_site/settings.py ALLOWED_HOSTS = ['*'] #DATABASES = { # 'default': { # 'ENGINE': 'django.db.backends.sqlite3', # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), # } #} 6)跑原生服务 >: python3 manage.py runserver 0.0.0.0:80
""" 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)删除安装包与文件: >: rm -rf nginx-1.13.7 >: rm -rf nginx-1.13.7.tar.xz 9)测试Nginx环境,服务器运行nginx,本地访问服务器ip >: nginx >: 服务器绑定的域名 或 ip:80 """
1)启动 >: nginx 2)关闭nginx >: nginx -s stop 3)重启nginx >: nginx -s reload 4)查看端口,强行关闭 >: ps -aux|grep nginx >: kill <pid:进程编号>
1)在项目的虚拟环境安装uwsgi >: workon test_venv >: pip install uwsgi 2)项目根目录配置uwsgi:填入下方内容 >: vim /home/project/test_site/test_site.xml <uwsgi> <socket>127.0.0.1:8808</socket> <!-- 内部端口,自定义 --> <chdir>/home/project/test_site/</chdir> <!-- 项目路径 --> <module>test_site.wsgi</module> <!-- test_site为wsgi.py所在目录名--> <processes>4</processes> <!-- 进程数 --> <daemonize>uwsgi.log</daemonize> <!-- 日志文件 --> </uwsgi> 3)完成项目配置:修改下方几行内容 >: vim /home/project/test_site/test_site/settings.py DEBUG = False ALLOWED_HOSTS = ['*'] 4)去向Nginx配置目录,备份配置,完全更新配置:填入下方内容 >: cd /usr/local/nginx/conf >: cp nginx.conf nginx.conf.bak >: vim nginx.conf >: ggdG >: i events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; 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 test_site.wsgi; #wsgi.py所在的目录名+.wsgi uwsgi_param UWSGI_CHDIR /home/project/test_site/; # 项目路径 } } } 5)启动uwsgi >: uwsgi -x /home/project/test_site/test_site.xml 6)启动nginx >: nginx 7)浏览器测试:http://39.98.144.221/admin 8)关闭uwsgi所有进程 >: pkill -f uwsgi -9
base_url: 'http://39.98.144.221:8000', // 设置公网ip
1)本地项目打包,前往luffycity项目目录下 >: cnpm run build 2)上传 >: scp -r dist root@39.98.144.221:~ 3)移动并重命名 mv ~/dist /home/html 4)去向Nginx配置目录,备份配置,完全更新配置:填入下方内容 >: cd /usr/local/nginx/conf >: cp nginx.conf nginx.conf.bak >: vim nginx.conf >: ggdG >: i 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问题 } } }
1)需要做上线修改的内容 DEBUG = False ALLOWED_HOSTS = [ '39.98.144.221' # 公网ip地址 ] CORS_ORIGIN_ALLOW_ALL = True # 允许所有跨域 CORS_ORIGIN_WHITELIST = [ ] UP_BASE_URL = 'http://39.98.144.221:8080' END_BASE_URL = 'http://39.98.144.221:8000'
1)需要做上线修改的内容 os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'luffyapi.settings.prod')
1)在项目的虚拟环境安装uwsgi >: mkvirtualenv luffy >: workon luffy # 走下方 pip导入导出依赖 说明,将本地的环境依赖同步到服务器环境中 >: pip install uwsgi 2)项目根目录配置uwsgi:填入下方内容 >: mkdir /home/project # 注:将后台项目移至到/home/project,可以上传,也可以git,项目设置公开(私密需要配置ssl) >: cd /home/project && git clone https://gitee.com/doctor_owen/luffyapi.git >: 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> ---- 3)配置上线项目的settings:见后台项目部署准备视频 4)去向Nginx配置目录,备份配置,完全更新配置:填入下方内容 >: vim /usr/local/nginx/conf/nginx.conf 5)在原来基础上添加一个server events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; 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/; # 项目路径 } } } 见下方配置:pip环境 + 数据库设置 + django2.0源码 配置完成后再进行往下(配置教程在下方) 5)启动uwsgi >: uwsgi -x /home/project/luffyapi/luffyapi.xml 6)启动nginx >: nginx -s stop >: nginx >: nginx -s reload 7)浏览器测试:http://39.98.144.221:8000/xadmin 8)关闭uwsgi所有进程 >: pkill -f uwsgi -9
1) 本地导出项目环境,上传线上,导入到线上环境中 本地操作 # 桌面新建env文件夹,开启终端进入文件夹,执行下方命名 >: cd Desktop\env >: pip3 freeze > packages.txt # 注:把xadmin删掉 >: scp -r packages.txt root@39.98.144.221:~ 服务器操作 # 进入虚拟环境 >: workon luffy # 导入 >: pip3 install -r packages.txt # 安装xadmin >: pip install https://codeload.github.com/sshwsfc/xadmin/zip/django2
1.管理员连接数据库 >: mysql -uroot -pOwen1234? 2.创建数据库 >: create database luffy default charset=utf8; # 3.设置权限账号密码 # 拥有公网或局域网,其他主机连mysql >: grant all privileges on luffy.* to 'luffy'@'%' identified by 'Luffy123?'; # 要是本机连mysql连不上,再增加localhost域,本机就可以登录了 >: grant all privileges on luffy.* to 'luffy'@'localhost' identified by 'Luffy123?'; # 设置完有权限限制的账号后一定要刷新权限 >: flush privileges; 4.退出mysql quit 5.修改 prod.py | manage.py >: vim /home/project/luffyapi/luffyapi/settings/prod.py "PASSWORD": "Luffy123?" >: vim /home/project/luffyapi/manage.py os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'luffyapi.settings.prod') 6.源码修改 >: vim /root/.virtualenvs/luffy/lib/python3.6/site-packages/django/db/backends/mysql/base.py # 36,37行注释 >: vim /root/.virtualenvs/luffy/lib/python3.6/site-packages/django/db/backends/mysql/operations.py # 146行添加 query = query.encode() 7.数据库迁移 >: cd /home/project/luffyapi/ >: python3 manage.py makemigrations >: python3 manage.py migrate 8.创建超级用户 >: python3 manage.py createsuperuser # 账号密码:admin|admin123
""" # >: vim /home/project/luffyapi/luffyapi/settings/prod.py # 在STATIC_URL下方再添加两句 STATIC_URL = '/static/' STATIC_ROOT = '/home/project/luffyapi/luffyapi/static' # 服务器的绝对路径 STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),) # 小luffyapi下要有static文件夹 # >: esc # >: :wq """
可能错误 >: mkdir /home/project/luffyapi/luffyapi/static >: python3 /home/project/luffyapi/manage.py collectstatic
>: vim /usr/local/nginx/conf/nginx.conf events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; 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/; # 项目路径 } # 新增的配置静态文件 location /static { alias /home/project/luffyapi/luffyapi/static; } } 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
>: pkill -f uwsgi -9
>: uwsgi -x /home/project/luffyapi/luffyapi.xml
>: nginx -s reload
# 1、真实环境和虚拟环境都要安装uwsgi,将真实环境下的uwsgi建立软连接 # 2、redis服务一定要后台启动:redis-server & # 3、uwsgi启动django项目一定要进入虚拟环境下,因为环境都是安装在虚拟环境中 # 4、服务器的日志都会被记录在于uwsgi配置文件 luffyapi.xml 同类目下的 uwsgi.log 中
>: mysql -uluffy -pLuffy123?
>: use luffy
INSERT INTO luffy_teacher(id, orders, is_show, is_delete, created_time, updated_time, name, role, title, signature, image, brief) VALUES (1, 1, 1, 0, '2019-07-14 13:44:19.661327', '2019-07-14 13:46:54.246271', 'Alex', 1, '老男孩Python教学总监', '金角大王', 'teacher/alex_icon.png', '老男孩教育CTO & CO-FOUNDER 国内知名PYTHON语言推广者 51CTO学院2016\2017年度最受学员喜爱10大讲师之一 多款开源软件作者 曾任职公安部、飞信、中金公司、NOKIA中国研究院、华尔街英语、ADVENT、汽车之家等公司'); INSERT INTO luffy_teacher(id, orders, is_show, is_delete, created_time, updated_time, name, role, title, signature, image, brief) VALUES (2, 2, 1, 0, '2019-07-14 13:45:25.092902', '2019-07-14 13:45:25.092936', 'Mjj', 0, '前美团前端项目组架构师', NULL, 'teacher/mjj_icon.png', '是马JJ老师, 一个集美貌与才华于一身的男人,搞过几年IOS,又转了前端开发几年,曾就职于美团网任高级前端开发,后来因为不同意王兴(美团老板)的战略布局而出家做老师去了,有丰富的教学经验,开起车来也毫不含糊。一直专注在前端的前沿技术领域。同时,爱好抽烟、喝酒、烫头(锡纸烫)。 我的最爱是前端,因为前端妹子多。'); INSERT INTO luffy_teacher(id, orders, is_show, is_delete, created_time, updated_time, name, role, title, signature, image, brief) VALUES (3, 3, 1, 0, '2019-07-14 13:46:21.997846', '2019-07-14 13:46:21.997880', 'Lyy', 0, '老男孩Linux学科带头人', NULL, 'teacher/lyy_icon.png', 'Linux运维技术专家,老男孩Linux金牌讲师,讲课风趣幽默、深入浅出、声音洪亮到爆炸'); INSERT INTO luffy_course_category(id, orders, is_show, is_delete, created_time, updated_time, name) VALUES (1, 1, 1, 0, '2019-07-14 13:40:58.690413', '2019-07-14 13:40:58.690477', 'Python'); INSERT INTO luffy_course_category(id, orders, is_show, is_delete, created_time, updated_time, name) VALUES (2, 2, 1, 0, '2019-07-14 13:41:08.249735', '2019-07-14 13:41:08.249817', 'Linux'); INSERT INTO luffy_course(id, orders, is_show, is_delete, created_time, updated_time, name, course_img, course_type, brief, level, pub_date, period, attachment_path, status, students, sections, pub_sections, price, course_category_id, teacher_id) VALUES (1, 1, 1, 0, '2019-07-14 13:54:33.095201', '2019-07-14 13:54:33.095238', 'Python开发21天入门', 'courses/alex_python.png', 0, 'Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土&&&Python从入门到入土', 0, '2019-07-14', 21, '', 0, 231, 120, 120, 0.00, 1, 1); INSERT INTO luffy_course(id, orders, is_show, is_delete, created_time, updated_time, name, course_img, course_type, brief, level, pub_date, period, attachment_path, status, students, sections, pub_sections, price, course_category_id, teacher_id) VALUES (2, 2, 1, 0, '2019-07-14 13:56:05.051103', '2019-07-14 13:56:05.051142', 'Python项目实战', 'courses/mjj_python.png', 0, '', 1, '2019-07-14', 30, '', 0, 340, 120, 120, 99.00, 1, 2); INSERT INTO luffy_course(id, orders, is_show, is_delete, created_time, updated_time, name, course_img, course_type, brief, level, pub_date, period, attachment_path, status, students, sections, pub_sections, price, course_category_id, teacher_id) VALUES (3, 3, 1, 0, '2019-07-14 13:57:21.190053', '2019-07-14 13:57:21.190095', 'Linux系统基础5周入门精讲', 'courses/lyy_linux.png', 0, '', 0, '2019-07-14', 25, '', 0, 219, 100, 100, 39.00, 2, 3); INSERT INTO luffy_course_chapter(id, orders, is_show, is_delete, created_time, updated_time, chapter, name, summary, pub_date, course_id) VALUES (1, 1, 1, 0, '2019-07-14 13:58:34.867005', '2019-07-14 14:00:58.276541', 1, '计算机原理', '', '2019-07-14', 1); INSERT INTO luffy_course_chapter(id, orders, is_show, is_delete, created_time, updated_time, chapter, name, summary, pub_date, course_id) VALUES (2, 2, 1, 0, '2019-07-14 13:58:48.051543', '2019-07-14 14:01:22.024206', 2, '环境搭建', '', '2019-07-14', 1); INSERT INTO luffy_course_chapter(id, orders, is_show, is_delete, created_time, updated_time, chapter, name, summary, pub_date, course_id) VALUES (3, 3, 1, 0, '2019-07-14 13:59:09.878183', '2019-07-14 14:01:40.048608', 1, '项目创建', '', '2019-07-14', 2); INSERT INTO luffy_course_chapter(id, orders, is_show, is_delete, created_time, updated_time, chapter, name, summary, pub_date, course_id) VALUES (4, 4, 1, 0, '2019-07-14 13:59:37.448626', '2019-07-14 14:01:58.709652', 1, 'Linux环境创建', '', '2019-07-14', 3); INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (1, 1, 0, '2019-07-14 14:02:33.779098', '2019-07-14 14:02:33.779135', '计算机原理上', 1, 2, NULL, NULL, '2019-07-14 14:02:33.779193', 1, 1); INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (2, 1, 0, '2019-07-14 14:02:56.657134', '2019-07-14 14:02:56.657173', '计算机原理下', 2, 2, NULL, NULL, '2019-07-14 14:02:56.657227', 1, 1); INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (3, 1, 0, '2019-07-14 14:03:20.493324', '2019-07-14 14:03:52.329394', '环境搭建上', 1, 2, NULL, NULL, '2019-07-14 14:03:20.493420', 0, 2); INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (4, 1, 0, '2019-07-14 14:03:36.472742', '2019-07-14 14:03:36.472779', '环境搭建下', 2, 2, NULL, NULL, '2019-07-14 14:03:36.472831', 0, 2); INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (5, 1, 0, '2019-07-14 14:04:19.338153', '2019-07-14 14:04:19.338192', 'web项目的创建', 1, 2, NULL, NULL, '2019-07-14 14:04:19.338252', 1, 3); INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (6, 1, 0, '2019-07-14 14:04:52.895855', '2019-07-14 14:04:52.895890', 'Linux的环境搭建', 1, 2, NULL, NULL, '2019-07-14 14:04:52.895942', 1, 4);
本文来自博客园,作者:游走De提莫,转载请注明原文链接:https://www.cnblogs.com/Gaimo/p/11791798.html