大体思路:由于我搭建的是仅供局域网访问的网站,所以不需要服务器和域名,只需要一台Ubuntu系统的电脑就行。我用的是ubuntu16.04LTS系统,带有图形界面。
web服务器选用nginx:Nginx是一款轻量级的Web服务器(通过HTTP协议,将服务器上的静态文件展现给客户端)、反向代理服务器,由于它的内存占用少,启动极快,高并发能力强,在互联网项目中广泛应用。
web框架虚选用python的flask:用于动态地生成HTML文件。
web服务器和框架之间需要通信,需要遵循一套接口协议 --wsgi协议。采用gunicorn作为wsgi服务器。
为了防止未知原因导致gunicorn停止运行,采用supervisor控制进程和设置开机启动等。
开始动手前再说明写本人在搭建过程中常用的linux命令,能提高效率。
$ su #为终端获取管理员权限,方便各种安装和更改文件。
# nautilus #打开带有管理员权限的文件管理器
下面就开始安装了,下面的操作都在获取了管理员权限的终端进行,所以请提前获取root权限。安装时我决定先安装python以及flask、gunicorn等,这样能很快看到一定的效果,利用新建的test页面可以排查故障所在。
step1 : 安装python和virtualenv
# mkdir /usr/local/python3
#新建python3文件夹
# cd /usr/local/python3
#切换到/usr/local 目录下,此处可以自己选择安装目录
# wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz
#下载python安装包
# tar -xvf Python-3.6.3.tgz
#解压
# cd Python-3.6.3
# ./configure --prefix=/usr/local/python3Dir
#配置路径
# make && make install
#编译安装
# ln -s /usr/local/python3Dir/bin/python3 /usr/bin/python3.6
#建立bin下建立软连接,输入python3.6即可运行
python3.6。许多教程上直接把软连接设为python。此处不推荐,因为Ubuntu系统自带的python2.7是
python,这么更改的话系统会经常报错,后续安装也会有问题。
# ln -s /usr/local/python3Dir/bin/pip3 /usr/bin/pip3
#建立pip3的软链接
# python3.6
#测试是否安装成功
> exit()
#退出python
# pip3 --version
#查看pip3的版本
# pip3 list
#查看已经安装的包,正常情况下几乎是没有安装什么包的,
因为我们的python只是刚装上,如果有比较多的包或者报错,那么就新建一个终端打开带有权限的
文件管理器(命令在安装前的常用命令里),更改 /usr/local/python3Dir/bin/下的pip和pip3的开
头部分。把python改成python3.6
# pip3 install virtualenv
#安装在/usr/local/python3Dir/lib/python3.6/site-
packages 下
# ln -s /usr/local/python3Dir/bin/virtrualenv /usr/bin/virtrualenv
#建立软连接,方便启动
# virtrualenv –version
#显示版本即为安装成功
# cd /usr/local
# mkdir virtualenv && cd virtualenv #新建目录
# virtualenv py3env #新建虚拟环境,起名为py3env
# source py3env/bin/activate #激活虚拟环境
# python #查看是否能进入python,并看看版本是否为3.6.3
> exit() #退出python环境
# pip3 install sklearn flask gunicorn tensorflow pandas keras #安装python的一些库
# deactive #没有异常的话就可以退出虚拟环境了,有异常解决并正常安装后可以退出虚拟
环境
至此 python3.6,以及virtualenv、flask、gunicorn都安装完成了。如何要测试或者体验一下成就感,建立个test的网页,参照https://www.jianshu.com/p/484bd73f1e80?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation
step2: 获取网站代码文件
地址:https://github.com/TricoChan/Sentiment-Master
将文件clone到本地,也可以用u盘拷贝(我用的就是),然后选择性地解压到/usr/local/src/hello/sentiment/ 下,其中后两个文件夹要自己建,用图形界面的文件管理器操作很方便,解压后的效果如图:
step3: 安装配置nginx
# apt-get install nginx #安装nginx,安装完成后/etc/nginx下会生成配置文件
/etc/nginx配置文件目录
nginx.conf
这个是nginx的主配置文件,里面包含了当前目录的所有配置文件,
只不过有的是注释状态,需要的时候自行开启(后面几个常用的)
conf.d
这是一个目录,里面可以写我们自己自定义的配置文件,文件结尾一
定是.conf才可以生效(当然也可以通过修改nginx.conf来取消这个限制)
sites-enabled
这里面的配置文件其实就是sites-available里面的配置文件的软
连接,但是由于nginx.conf默认包含的是这个文件夹,所以我们在
sites-available里面建立了新的站点之后,还要建立个软连接到sites-enabled里面才行
sites-available
这里是我们的虚拟主机的目录,我们在在这里面可以创建多个虚拟主机
将nginx.conf文件重命名作为备份后,原地复制一个新的nginx.conf,这样既可以更改配置有方便恢复原来的文件。在新的nginx.conf里面输入如下:
user www www;
worker_processes auto; #设置值和CPU核心数一致
error_log /var/log/nginx/error.log; #日志位置和日志级别
pid /run/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
use epoll;
worker_connections 65535;
}
http
{
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
#charset gb2312;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
#下面是server虚拟主机的配置
server
{
listen 80;#监听端口
server_name localhost;#此处更改为你的域名或者ip地址,我没有域名故改为IP地址:
index index.html index.htm index.php;
root /usr/local/webserver/nginx/html;#站点目录
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
{
expires 30d;
# access_log off;
}
location ~ .*\.(js|css)?$
{
expires 15d;
# access_log off;
}
access_log off;
}
}
在sites-available 目录下新建sentiment_app文件,并复制以下内容:
server{
listen 80; # 监听80端口
server_name 主机ip # 主机对外域名,自己申请的域名
# 静态文件直接访问静态文件目录(这儿改成你自己的目录)
location /static{
alias /usr/local/src/hello/sentiment/static
}
# http请求转发到本地9000端口
location / {
proxy_pass http://127.0.0.1:9000;
}
}
以上几步都可以在文件管理器中完成。方便快捷
# ln -s /etc/nginx/sites-available/sentiment_app /etc/nginx/sites-enabled/sentiment_app
#在sites-enabled下建立软连接
# service nginx -t #检验配置文件的正确性
----一以下为常用命令
# service nginx reload #重启
# service nginx start #开启服务
# service nginx stop #停止
# service nginx restart #重新开始
step4: 安装配置supervisor
参考:https://www.cnblogs.com/xishuai/p/ubuntu-install-supervisor.html
# apt-get install supervisor #安装supervisor
安装成功后,会在/etc/supervisor
目录下,生成supervisord.conf
配置文件。配置文件重新写入如下:
; supervisor config file
[unix_http_server]
file=/var/run/supervisor.sock ; (the path to the socket file)
chmod=0700 ; sockef file mode (default 0700)
[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor ; ('AUTO' child log dir, default $TEMP)
; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket
; The [include] section can just contain the "files" setting. This
; setting can list multiple files (separated by whitespace or
; newlines). It can also contain wildcards. The filenames are
; interpreted as relative to this file. Included files *cannot*
; include files themselves.
[include]
files = /etc/supervisor/conf.d/*.conf
在配置文件的末尾有
[include]
files = /etc/supervisor/conf.d/*.conf。即在conf.d文件下以。conf结尾的文件都是配置文件。
在conf.d目录下新建sentiment_app.conf 文件,写入如下:
[program:sentiment]
# 指定运行的python路径 指定自己创建的虚拟环境p3env的gunicorn目录 绑定8080端口 app.py中app运行实例
command = /usr/local/virtualenv/py3env/bin/python /usr/local/virtualenv/py3env/bin/gunicorn -b 0.0.0.0:9000 app:app
# 应用运行目录
autostart = true
autorestart =true
directory = /usr/local/src/hello
# 错误日志输出目录
stdout_logfile = /usr/local/src/hello/out.log
stderr_logfile = /usr/local/src/hello/error.log
自此,配置文件完成
# supervisord #启动服务
# supervisorctl -c /etc/supervisor/supervisord.conf #启动supervisorctl
>status #c查看状态 ,如果没有显示sentiment running ,执行下面的
>start sentiment
-----常用命令还有
>reload
>stop
>restart
最后再在防火墙开启端口即可
# ufw allow 9000
# ufw allow 8000
# ufw allow 80
# ufw status #查看是否开启
如果一切顺利,网站以及完成了。输入IP:9000即可访问网站
但是 /usr/local/src/hello/sentiment/static/ 下的umi.js 文件有点问题,需要更改点东西。新的umi.js生成可以按照