时间:2023-02-10 14:30:01 | 来源:建站知识
时间:2023-02-10 14:30:01 来源:建站知识
如何搭一个输入https://www.xxx.com就能访问的网站?
keyword:域名申请;域名解析;域名备案;服务器;服务器基础配置;安装SSL证书;后台搭建
目录1、整体描述2、服务器基础配置3、后端框架4、dbsqlit
yum install gcc-c++yum install -y pcre pcre-develyum install -y zlib zlib-develyum install -y openssl openssl-develwget -c https://nginx.org/download/nginx-1.16.1.tar.gztar -zxvf nginx-1.16.1.tar.gz./configure --with-http_ssl_modulemake && make installcd /usr/local/nginx./sbin/nginx (-s stop/quit/reload)./sbin/nginx -s quit && ./sbin/nginxps aux|grep nginx 查看nginx服务vim /usr/local/nginx/conf/nginx.conf 修改端口,默认80报错的情况下:(nginx: [error] open() “/usr/local/nginx/logs/nginx.pid“ failed (2: No such file or directory))执行sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
网站对应的SSL证书下载后传输:scp 1_www.domain.com_bundle.crt root@ip:/usr/local/nginx/conf/scp 2_www.domain.com.key root@ip:/usr/local/nginx/conf/
修改nginx.conf,使得nginx依赖SSL证书,参考文档:整个nginx的搭建方案https://cloud.tencent.com/document/product/400/35244location的写法规范:https://blog.csdn.net/qq_35992647/article/details/88063167
--把需要启动的网页/war等放在nginx.conf指定的路径下,启动nginx服务,Done~1、yum/pip install django2、django-admin startproject calculator3、修改calculator/settings.py中的ALLOWED_HOSTS = []为ALLOWED_HOSTS = ['*']4、python manage.py runserver 0.0.0.0:8000
上述四步可以开启服务,可以访问了。详细步骤参考:https://blog.csdn.net/qq_43467898/article/details/83187698
(3)django的数据库使用class Person(models.Model): name = models.CharField(max_length=30) age = models.IntegerField() def __str__(self): return self.name
然后是数据库生效生成变化/变化生效python manage.py makemigrationspython manage.py migrate
3、uwsgi配置yum install uwsgi详细配置参考:https://blog.csdn.net/qq_43467898/article/details/83187698yum search uwsgi-plugin-pythonyum install uwsgi-plugin-python2(坑死)新建uwsgi.ini文件并添加 plugins = python(服务端口写成sock文件)uwsgi --ini uwsgi.ini 启动uwsgi服务
nginx.conf配置添加如下,与uwsgi关联location ~ ^/calculate { // replace "path" to the path of your project uwsgi_pass unix:///"path"/calculator/calculator.sock; include /etc/nginx/uwsgi_params; }
nginx重新启动服务,OK!【conf/nginx.conf】 server { listen 443 ssl; server_name www.yzreal.com; ssl_certificate /usr/local/nginx/conf/1_www.yzreal.com_bundle.crt; ssl_certificate_key /usr/local/nginx/conf/2_www.yzreal.com.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location ~ ^/calculate { uwsgi_pass 127.0.0.1:3031; include uwsgi_params; } }【uwsgi.ini】[uwsgi]# django项目监听的socket文件(可以使用端口代替)socket = 127.0.0.1:3031# django项目所在目录chdir = .# django项目wsgi文件wsgi-file = ./calculator/wsgi.pymaster = trueprocesses = 2threads = 4vacuum = true# 通过touch reload可以重启uwsgi服务器touch-reload = ./reload# 日志输出daemonize = calculator.logplugins = python
class UserInfo(models.Model): username = models.CharField(max_length= 50) password = models.CharField(max_length= 20) age = models.IntegerField(default = 0)
然后需要生成数据库变化(每次数据库文件发生变化都要):python manage.py makemigrationspython manage.py migrate
2、查询数据库中所有tablesqlite3 db.sqlite3select name from sqlite_master;查询:select * from CalculateApi_userinfo;select * from sqlite_master where type="table" and name="CalculateApi_userinfo";修改:update CalculateApi_userinfo set order_status = '0' where id = 124;删除:delete from CalculateApi_userinfo where id = 206;.quit 导出到csv文件:sqlite3 -header -csv db.sqlite3 "select * from CalculateApi_userinfo;" > xxx.csv
3、在view视图里可以进行数据库增删查改:表名为:UserInfo结构有:username,password,agefrom blog import models #导入blog模块def db_handle(): # 增 (1)普通方式 models.UserInfo.objects.create(username='andy',password='123456',age=33) (2)字典方式 dic = {"username":"bruce","password":"123456","age":23} models.UserInfo.objects.create(**dic) # 删 models.UserInfo.objects.filter(id=2).delete() # 查 user_list_obj = models.UserInfo.objects.all() user_list_obj 结构是个 list里套dict for obj in user_list_obj: print(obj.username, obj.password, obj.age) # 改 models.UserInfo.objects.filter(id=1).update(age=18) #找到id=1的数据,将age改为18
4、查询到的数据返回给请求端def search(request): try: # get_all = UserInfo.objects.all().values() # datas 为 [{},{}] list中是dict datas = list(models.UserInfo.get_all()) result = { 'code': 0, 'message': 'success', 'data': datas, } except Exception as e: ret = 'Error: ' + str(e) return HttpResponse(ret) # 返回utf-8编码的数据 return JsonResponse(result)
关键词:服务