Python+Django+Ubuntu+Apache+Nginx架设服务器成功记

Django生产部署:Apache + mod_wsgi 实战指南 WSGI(Web Server Gateway Interface)是Python Web应用与服务器通信的标准协议,其核心价值在于解耦应用逻辑与HTTP服务层。mod_wsgi作为Apache官方推荐的WSGI实现,通过守护进程模式提供进程隔离、内存管理与优雅重启能力,显著提升Django等框架的生产健壮性。相比开发服务器runserver的单线程局限,或Nginx+uWSGI的多组件架构,Apache+mod_wsgi方案在LAMP运维体系中具备配置统一、日志集中、安全模块(如mod_security) 阅读详情

是看一个国外网站一步一步照着来的,复制过来

http://www.ventanazul.com/webzine/tutorials/django-deployment-guide-ubuntu


There's a time when every Django developer has to think about deployment scenarios, when I first did it last year I thought that a setup involving Lighty, Apache, mod_proxy and mod_python was a good choice but my first approach was not the best. I put Apache as the front server, handling requests for Django generated pages and passing, via mod_proxy, requests for static content to Lighty on the back. A setup where Apache had to work even for files that wasn't supposed to serve was a very bad idea.

After many helpful comments and some more reading I realized that it was better having the server for static content on the front and Apache, which still talks to Django, on the back.

I replaced Lighty with nginx, which according to many seems to be more stable, and opted for mod_wsgi instead of mod_python to make Apache talk to Django. mod_wsgi has a lower memory overhead and it's suitable for high performance sites. There's no need for mod_proxy on Apache anymore as nginx is the one in charge of the proxy work now.

This is an easy to follow and very focused guide for developers who know how to handle their servers so I won't consider security issues, memcached, Django installation, databases or basic GNU/Linux, Apache and DNS settings. Of course all of those subjects are important and you should take care of them.

The plan

This guide includes all the steps needed to:

  • Setup a domain for your Django project.
  • Create a simple directory layout for Django sites.
  • Configure Apache with mod_wsgi for Django.
  • Configure nginx.
  • Serve Django admin media files.
  • Turn on the heat and show your greatest and latest Django stuff to the world.

After following all the steps you will have a Django site running with nginx on the front and Apache on the back. nginx will manage all static content and will pass Django requests to Apache and mod_wsgi.

I have tested on three Ubuntu servers (two running 8.10, Intrepid Ibex, and one 7.10, Gutsy Gibbon) but everything should be pretty similar in other GNU/Linux distributions.

Shall we start?

Setup a domain for your Django project

Many of us run more than one website in one server, I have several with Drupal and a few with Django living in one box, so let's consider a two domains scenario: cataybea.com will host the Django project and example.com anything else. Most of the work will focus in cataybea.com, I just mention example.com to give you some context. Obviously you should replace all the domain names in this guide with yours.

We'll use a couple of private IP addresses for now and will replace them with public ones and setup in a DNS server when going live. For this guide I'll just add to /etc/hosts, which is what I usually do in my development environment:

192.168.0.180 cataybea.com192.168.0.192 example.com

I'm using 192.168.0.180 and two ports, 80 and 8080, for serving the Django site at cataybea.com. The default port 80 will be used by nginx to serve static content and Django will look for files under the /media directory. Port 8080 will be used by Apache to handle Django requests.

Create a simple directory layout for Django sites

Directory layouts depend on personal preferences, I currently use a /home/alexis/djcode directory for all my Django coding and the new project will be called cataybea; hence, we have to run:

mkdir /home/alexis/djcodecd /home/alexis/djcode/django-admin.py startproject cataybea

Now let's create some additional directories:

mkdir /home/alexis/djcode/cataybea/apachemkdir /home/alexis/djcode/cataybea/logsmkdir /home/alexis/djcode/cataybea/media

What are they for? apache will contain a Python script to setup mod_wsgi, logs will store Apache and nginx logs and media is the directory nginx will use to serve static files for our Django project.

You can later manage the whole /home/alexis/djcode/cataybea directory with your favorite version control tool.

Take note of the correct paths for using later in the configuration files.

Configure Apache with mod_wsgi for Django

I assume you already have Apache working correctly and just need to tweak a little for our Django setup. As we'll be running two web servers at once we must make sure that IP addresses and ports won't conflict. Let's edit /etc/apache2/ports.conf:

Listen 192.168.0.192:80Listen 192.168.0.180:8080#Listen 80

I have specified IP addresses and ports to listen. Notice I commented the default Listen 80 as it means Apache would listen to that port in all IP addresses. If you want to use SSL take care of the 443 port in the same way.

If you are using virtual hosts in Apache confirm it's listening to the correct IP addresses and ports, I have this in /etc/apache2/sites-enabled/example.com:

NameVirtualHost 192.168.0.192

Restart Apache and make sure your Apache sites work normally. I'm obsessed with testing at every step in the way and I suggest you are too.

sudo /etc/init.d/apache2 restart

Now it's time to add mod_wsgi to Apache. The latest versions of Ubuntu have it in the repository:

sudo apt-get install libapache2-mod-wsgi

For older versions of Ubuntu, such as Gutsy, you need to find the .deb file and use something like dpkg -i to install.

Now it's time to create the Apache configuration file for cataybea.com in /etc/apache2/sites-available/cataybea.com:

<VirtualHost 192.168.0.180:8080> ServerAdmin alexis@ventanazul.com ServerName www.cataybea.com ServerAlias cataybea.com <Directory /home/alexis/djcode/cataybea/apache/> Order deny,allow Allow from all </Directory> LogLevel warn ErrorLog /home/alexis/djcode/cataybea/logs/apache_error.log CustomLog /home/alexis/djcode/cataybea/logs/apache_access.log combined WSGIDaemonProcess cataybea.com user=www-data group=www-data threads=25 WSGIProcessGroup cataybea.com WSGIScriptAlias / /home/alexis/djcode/cataybea/apache/cataybea.wsgi</VirtualHost>

Notice the IP address and port: 192.168.0.180:8080. This site won't be accessed directly but via proxy from nginx. We'll set that up in the next section.

Now we need a Python script to configure our Django project to use mod_wsgi, create /home/alexis/djcode/cataybea/apache/cataybea.wsgi and put the following code inside:

import os, sysapache_configuration= os.path.dirname(__file__)project = os.path.dirname(apache_configuration)workspace = os.path.dirname(project)sys.path.append(workspace)sys.path.append('/usr/lib/python2.5/site-packages/django/')sys.path.append('/home/alexis/djcode/cataybea')os.environ['DJANGO_SETTINGS_MODULE'] = 'cataybea.settings'import django.core.handlers.wsgiapplication = django.core.handlers.wsgi.WSGIHandler()

Don't restart Apache yet as we need to complete a few more steps.

Configure nginx

nginx will do two things: serve static content from http://cataybea.com/media and pass all other requests to Apache. Let's install it first:

sudo apt-get install nginx

Now remove the nginx default site:

sudo rm /etc/nginx/sites-enabled/default

Apache and nginx should be using the same user, for Ubuntu this is www-data, and your /etc/nginx/nginx.conf should look like this:

user www-data;worker_processes 2;error_log /var/log/nginx/error.log;pid /var/run/nginx.pid;events { worker_connections 1024;}http { include /etc/nginx/mime.types; default_type application/octet-stream; access_log /var/log/nginx/access.log; sendfile on; tcp_nopush on; keepalive_timeout 65; tcp_nodelay on; gzip on; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*;}

worker_processes can be set to the number of cores in your server, in my case that's just 2.

Configure a the cataybea.com site on nginx by creating /etc/nginx/sites-available/cataybea.com:

server { listen 192.168.0.180:80; server_name www.cataybea.com cataybea.com; access_log /home/alexis/djcode/cataybea/logs/nginx_access.log; error_log /home/alexis/djcode/cataybea/logs/nginx_error.log; location / { proxy_pass http://192.168.0.180:8080; include /etc/nginx/proxy.conf; } location /media/ { root /home/alexis/djcode/cataybea/; }}

then running:

sudo ln -s /etc/nginx/sites-available/cataybea.com /etc/nginx/sites-enabled/cataybea.com

and finally creating the file that will take care of proxying Django requests to Apache, we'll call it /etc/nginx/proxy.conf and put this inside:

proxy_redirect off;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;client_max_body_size 10m;client_body_buffer_size 128k;proxy_connect_timeout 90;proxy_send_timeout 90;proxy_read_timeout 90;proxy_buffer_size 4k;proxy_buffers 4 32k;proxy_busy_buffers_size 64k;proxy_temp_file_write_size 64k;

Notice $host, $remote_addr and $proxy_add_x_forwarded_for are variables that will be handled by nginx, you don't need to change anything else in proxy.conf for a basic setup. When I started playing with nginx I thought these had to be replaced by me, I was wrong.

Finally restart nginx:

sudo /etc/init.d/nginx restart

and Apache:

sudo /etc/init.d/apache2 restart

That should be it! If you visit http://cataybea.com you should see some Django content (could be the welcome page, some of your views or a 404 page depending on your project status). To try the nginx media server create a test.htm file with some dummy content in /home/alexis/djcode/cataybea/media and visit http://cataybea.com/media/test.htm.

Serve Django admin media files (optional)

If you will use the Django admin application edit /home/alexis/djcode/cataybea/settings.py, make sure your database settings are correct, add 'django.contrib.admin' to the INSTALLED_APPS tuple and change the ADMIN_MEDIA_PREFIX like this:

ADMIN_MEDIA_PREFIX = '/media/admin/'

Let's enable the admin url by editing /home/alexis/djcode/cataybea/urls.py, uncommenting these two lines:

from django.contrib import adminadmin.autodiscover()

and adding this to urlpatterns:

(r'^admin/', include(admin.site.urls)),

Now let's create a symbolic link to the media files for the Django's administration section:

cd /home/alexis/djcode/cataybea/medialn -s /usr/lib/python2.5/site-packages/django/contrib/admin/media/ admin

And finally let's create the administration user and the required tables in the database:

cd /home/alexis/djcode/cataybea/mediapython manage.py syncdb

As with every other modifications in your Django code you will have to reload Apache for changes to take effect:

sudo /etc/init.d/apache2 reload

Visit http://cataybea.com/admin/,including the trailing slash, and you should be in business.

Show your Django projects to the world

The nginx and Apache setup I've just described is pretty easy to implement and seems to be the preferred method among many Django developers. I'm using it for a new project I'm working at, you may have already guessed this, cataybea.com. The site will be an online diary (ok, ok, a blog) where my wife and I will share some stories and tips about raising our two daughters, it will be fun.

Now that you have everything ready for deploying your next project I suggest you keep working with Django's development server until you have everything ready to move to Apache and nginx. The development server refreshes automatically after every change in your code so you can avoid frequent Apache reloads.



Django生产部署:Postgres+Nginx+Gunicorn Ubuntu实战指南 Web应用部署本质上是将Python Web框架接入操作系统级服务的系统工程。理解WSGI协议、进程模型与分层代理原理,是构建高可用Django服务的基础;Postgres提供ACID事务与连接池能力,Nginx承担静态资源服务、SSL终止与反向代理,Gunicorn作为轻量级应用服务器执行Django代码——三者协同构成现代Python Web服务的标准范式。该架构显著提升并发处理能力、安全隔离性与运维可控性,广泛应用于中大型Django项目上线、云服务器部署及DevOps流水线集成。本文聚焦Ubuntu 阅读详情

相关推荐

Django生产部署:Docker+Nginx+Let’s Encrypt全链路实战

Web应用生产部署的核心在于环境一致性、流量治理与安全可信。Docker通过容器镜像固化Python运行时与依赖,解决‘本地能跑,线上报错’的环境漂移问题;Nginx作为反向代理承担SSL终止、静态资源服务、请求限流与安全防护,避免Django直接暴露于公网;Let’s Encrypt基于ACME协议实现HTTPS证书的自动化申请与续期,让全站加密从运维负担变为默认能力。这三者协同构成现代Django应用可扩展、可观测、可信赖的基础设施底座,广泛应用于SaaS后台、API服务与企业级CMS等真实场景。

weixin_30415113的博客 858

ubuntu搭建apache+python+django+mysql web开发环境

我是在ubuntu12.04的环境下配置的,系统默认安装python2.7.3。 还需要安装apache,mod_python,django以及mysql 一.安装apache2 1 sudoapt-get install apache2 安装完成。 运行如下命令重启下: 1 sudo/

穆穆里的专栏 8999

Django ASGI部署实战:Uvicorn+Postgres+Nginx高性能架构

ASGI(异步服务器网关接口)是现代Python Web应用实现高并发与实时能力的基础协议,它突破了传统WSGI同步阻塞模型的性能瓶颈,使Django原生支持WebSocket、长轮询和流式响应。其核心原理在于将IO等待交由异步事件循环调度,实现单线程内数千连接的高效复用。技术价值体现在显著提升吞吐量(实测RPS提升4倍)、降低内存占用(协程轻量于多进程)及简化实时功能开发。典型应用场景包括教育SaaS后台、实时通知系统、高频API服务等。本文聚焦Django 4.2在Ubuntu环境下的生产级ASGI部署

weixin_30527323的博客 451

在ubuntu下配置apache运行python脚本

2008-12-05    常用的简单命令 sudo apt-get remove --purge apache apache2 (彻底删除)   sudo /etc/init.d/apache2 restart sudo /etc/init.d/apache2 start sudo /etc/init.d/apache2 stop   sudo makedir /home/...

weixin_33739627的博客 227

Ubuntu搭建apache+python+django+mysql web开发环境

详细描述Ubuntu下如何搭建python的web开发环境

避坑指南:英飞凌TC3XX的MCMCAN初始化常见错误及解决方法

本文针对英飞凌TC3XX系列MCU的MCMCAN模块初始化难题,深入剖析了寄存器配置时序、总线监控模式误用、中断映射错误及Message RAM地址冲突四大常见陷阱。通过实战代码示例和诊断流程,提供了清晰的避坑指南和解决方案,帮助嵌入式开发者高效解决CAN FD通信故障,确保汽车电子与工业控制项目的稳定运行。

weixin_29193259的博客 255

ubuntu安装apache web网站服务器并解析python文件

  sudo apt-get install apache2 ### 配置 python 解析 # 安装mod_python sudo apt-get install libapache2-mod-Python # 配置: # 1. 在 /etc/apache2/mods-available/ 下新建一个文件python.conf,输入如下内容 &lt;Directory /var/ww...

orDream的博客 747

PlutoSDR在matlab中的环境配置(包括驱动和硬件支持包)

双击下载到的驱动,除了点击接受协议和设置安装路径,其他只要点击下一步或者安装即可。登录或者注册你的账号,选择你的matlab版本(这里我的是2023b),点击下一步。设置安装路径,并选择我接受,点击下一步,即可安装完成,并在安装路径中得到。完成后,打开设备管理器,在端口和通用串行总线设备一栏,即可看见下图。点击进入,选择相应的系统安装包,这里我选择的Windows。可看到我们要安装的Pluto支持包,安装即可。右击桌面matlab,选择打开文件所在位置。右上角搜索pluto,选择并点击下一步。

weixin_53160338的博客 4885

ubuntu下配置apache和mod_python

这几天做毕设,要用到这两样东西。但是下载后不会配置,因为第一次用apache,python也学得不怎样。在网上各种找帮助。有的说修改/etc/apache2/httpd.conf。但是我下载的没有发现这个文件。只有apache2.conf。              但是自己看了网上一些相关资料,虽然和我下载的有点不一样,但也终于配置好了。方法如下:            1.先安装apach

u010320710的专栏 667

思科—OSPF多区域配置实验

实验目的:配置OSPF多区域,使得不同网段的PC互相ping通。 网络拓扑如下: R0的配置 Router>en Router#conf t Router(config)#int g0/1 Router(config-if)#ip add 192.168.1.1 255.255.255.0 Router(config-if)#no shut Router(config-if)#exit Router(config)#int g0/0 Router(config-if)#ip add 192.168.

m0_46237205的博客 1万+

AS5600磁编码器选型与安装避坑指南:磁铁形状、间隙距离到底怎么选?(附实测数据)

本文详细解析了AS5600磁编码器的选型与安装要点,包括磁铁形状、间隙距离等关键参数的选择,并附有实测数据支持。特别针对Arduino和I2C接口的应用场景,提供了硬件选型和机械安装的实用指南,帮助开发者避免常见陷阱,提升系统精度和稳定性。

weixin_27038245的博客 305

ubuntu django项目部署

sudo /etc/init.d/apache2 restart # 重启Apache。sudo /etc/init.d/apache2 stop # 停止Apache。sudo /etc/init.d/apache2 start # 启动Apache。cat /var/log/apache2/error.log # 查看错误日志。修改 /etc/nginx/sites-enabled下的 default。Listen 80 改成 Listen 1125。3)wsgi.py文件修改。

qq_38202196的博客 2033

程序进入hardfault之后查找原因的方法

如果在MCU运行程序的时候,进入了hardfault,该怎么办?

smart_boy__的博客 770

Django生产部署黄金组合:Postgres+Nginx+Gunicorn详解

Django作为主流Python Web框架,其生产环境部署需兼顾稳定性、安全性和可维护性。核心原理在于组件职责解耦:Postgres提供ACID事务与JSONB原生支持,保障数据强一致;Nginx以事件驱动模型高效处理静态资源与反向代理;Gunicorn专注WSGI协议执行,轻量可靠。该技术栈的价值不仅体现在高并发承载能力,更在于清晰的分层设计——便于独立监控、权限隔离与渐进式升级(如替换为Uvicorn或CDN)。典型应用场景涵盖SaaS后台、中台系统及高可靠性Web服务,尤其适合不依赖K8s弹性伸缩但

weixin_34288121的博客 1306

Ansys Zemax与Speos协同设计:从HUD光学优化到驾驶环境可视化全流程解析

本文深入解析了如何利用Ansys Zemax与Speos进行协同设计,实现从HUD光学系统核心优化到驾驶环境可视化验证的全流程。文章强调了HUD设计是光学性能与视觉感知的深度耦合,通过Zemax进行精密光学设计,再利用Speos进行系统级仿真与感知模拟,最终完成从虚拟到现实的闭环验证,有效提升HUD产品的开发效率与可靠性。

weixin_29199873的博客 265

【反卷积算法】Richardson-Lucy (RL) :通过不断迭代更新图像估计值

Richardson-Lucy(RL)反卷积算法:是一种基于最大似然估计(Maximum Likelihood Estimation, MLE)的图像去卷积方法,常用于去模糊和图像增强。通过不断地更新图像估计值,来最小化模糊图像和恢复图像之间的误差,逐步消除模糊,最终接近真实的原始图像,每次迭代都会根据当前估计的图像来修正下次迭代的结果。

📷 图像处理践行者 | 传统算法 · 深度学习 · 多模态 | 分享实战经验与技术洞见,探索从像素到智能的无限可能。 3059

ubuntu配置django+nginx+uwsgi踩坑记

新手学习前后端,用了华为云服务器。测试项目之前的环境是ubuntu下django+apache2,最后解决不了出现的编码问题,然后转用nginx,最后成功运行,说说过程及学习体会。 django+nginx+uwsgi网上教程一大堆,配置也非常灵活,开始都把我绕晕了,怎么网上说的都不同,最后终于发现,其实本质上都是相同的,要理解他们各自的作用就行了。 django是一套web开发框架,它自己也...

yqpkilo的专栏 260

微信小程序 - 实现人脸识别对比+人脸相似度数据,用户从相册选择或拍照上传两张图片进行人脸对比得到人脸相似度及判断是否是同一个人,微信小程序检测人脸相似度(免费纯前端方案+调用第三方API接口方案)

微信小程序实现人脸识别对比,微信小程序人脸对比 人脸对比识别 人脸对比相似度,实现获取手机摄像头权限,调用相机拍照实现人脸识别相似度对比,小程序人脸相似度对比,快速识别两张照片的相似程度(用百分比)图片相似度对比在线测试,图片相似度对比,微信小程序人脸相似度对比分析,计算两张人脸的相似度,从而判断是否同一个人,并给出相似度评分。小程序如何实现人脸相似度比对,小程序纯前端怎么对比两张图片的相似度,计算两幅图像的相似度,图像相似度对比方法,调用百度AI的人脸识别,微信小程序中实现人脸识别认证,实现人脸检测、人脸

🏆 前端领域优质创作者 4881

Ubuntu 18.04 部署 Django 4.2 的稳定环境构建方法论

Django 是 Python 生态中最主流的 Web 开发框架,其生产部署需兼顾版本兼容性、依赖隔离与系统安全性。在已进入 EOL 状态的 Ubuntu 18.04 上,系统自带的 Python 3.6.9 与老旧软件源无法满足 Django 4.2+(要求 Python ≥ 3.8)的运行基础,直接 apt 安装将导致安全漏洞(如 CVE-2023-24580)、依赖污染及系统工具崩溃等高危问题。因此,现代工程实践强调采用 pyenv 管理多版本 Python、virtualenv 实现项目级依赖隔离、

dichengyan0013的博客 641

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

ubuntu+apache+python+django部署

 由于任务需要,在公司服务器部署的nginx+django服务要在阿里云上也部署一份,防止因公司断网儿服务无法访问;阿里云是ubuntu14.04+apache2.4的环境,需要安装需要的环境加以部署。 ubuntu14.04 默认python 2.7 ,现需要环境为python 3.5,需要安装python3.5 sudoadd-apt-repositoryppa:fk...

weixin_30302609的博客 304

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

Django生产部署:Docker+Nginx+Let‘s Encrypt全栈实战

Web应用部署本质上是将开发态代码转化为高可用、可扩展、安全可信的运行态服务。其核心原理在于环境隔离、流量分发与传输加密三大机制协同——Docker通过容器化实现运行时环境的一致性与不可变性,Nginx以事件驱动模型承担反向代理、静态资源服务与请求过滤等关键职责,Let's Encrypt则依托ACME协议自动化完成HTTPS证书的申请、验证与续期,构建零信任网络下的基础信任链。该技术组合已成为Python Web生产部署的事实标准,广泛应用于SaaS、企业后台及高并发API服务等场景,尤其适配Django

weixin_33895516的博客 462

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

Django项目实战学习(一)Ubuntu 配置并实战

ubuntu配置django并进行实战

qq_2975670963的博客 3657

Ubuntu 18.04上Django+React客户管理系统实战部署

Web全栈开发中,前后端分离架构是提升交互体验与系统可维护性的基础范式。其核心原理在于服务端专注数据建模、API契约与安全管控,前端聚焦状态管理、动态渲染与用户响应。该模式显著提升高并发场景下的页面流畅度与操作实时性,广泛应用于CRM、ERP及内部管理平台等B2B业务系统。在老旧硬件或受限运维环境中(如仅支持Python 3.6的Ubuntu 18.04),技术选型需兼顾兼容性与工程稳定性:Django REST Framework提供开箱即用的权限、过滤与序列化能力,React 17.x凭借轻量Hooks

csxc65837的博客 350

Django生产部署:Docker+Nginx+Let’s Encrypt最小可行闭环

Web应用生产部署的核心在于环境一致性、流量可控性与安全自动化。Docker通过镜像固化Python运行时与依赖,彻底解决‘本地能跑线上崩’的环境漂移问题;Nginx以事件驱动架构高效处理静态资源、反向代理及HTTPS终止,显著提升首屏加载与并发承载能力;Let’s Encrypt结合Certbot实现ACME协议自动验证与证书续期,将HTTPS从运维负担转化为无人值守的定时任务。三者协同构成现代Django应用的基础设施基线——既保障高可用与低延迟,又大幅压缩部署与维护成本。本文聚焦这一经17个中型项目验

weixin_33739541的博客 545

ubuntu18.04+Nginx+uwsgi+Django介绍和部署

一:Nginx/uwsgi/Django介绍 1.1 Nginx简介 Nginx 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。在高连接并发的情况下,Nginx是Apache服务器不错的替代品。 Nginx 一般监听服务器的80或者443端口(listen 80 就代表监听80端口),因为一般直接访问网站都是通过这两个端口...

许进进的博客 6860

django项目部署-python服务器搭建

对于在ubuntu和window上部署django项目实现后的总结,主要注意事项。

u010278162的博客 1670
上一篇: Ubuntu 安装MySQL数据库,Windows 安装MySQL WorkBench进行远程管理,Django远程连接 手记
下一篇: Django 上传文件出现 OSError Permission denied的解决办法
Wally_Yu
博客等级 码龄19年 71粉丝 28原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址