ubuntu下,nginx + webpy + spawn-fcgi + mongodb + python3搭建服务端

一. 安装最新版本

    webpy0.40  Spawn-fcgi   Flup

二. Nginx安装

查看ubuntu的位数:uname -a 
查看ubuntu的版本:lsb_release -a 或者 cat /proc/version
我使用的环境是64位 Ubuntu 16.04。nginx依赖以下模块:
l  gzip模块需要 zlib 库
l  rewrite模块需要 pcre 库

l  ssl 功能需要openssl库

1.1.安装pcre
1.         获取pcre编译安装包,在http://www.pcre.org/上可以获取当前最新的版本
2.         解压缩pcre-xx.tar.gz包。
3.         进入解压缩目录,执行./configure
4.         make & sudo make install

1.2.安装openssl
1.         获取openssl编译安装包,在http://www.openssl.org/source/上可以获取当前最新的版本。
2.         解压缩openssl-xx.tar.gz包。
3.         进入解压缩目录,执行./config
4.         make & sudo make install

1.3.安装zlib
1.         获取zlib编译安装包,在http://www.zlib.net/上可以获取当前最新的版本。
2.         解压缩zlib-xx.tar.gz包。
3.         进入解压缩目录,执行./configure
4.         make & sudo make install

1.4.安装nginx
1.         获取nginx,在http://nginx.org/en/download.html上可以获取当前最新的版本。
2.         解压缩nginx-xx.tar.gz包。
3.         进入解压缩目录,执行./configure
4.         make & sudo make install

若安装时找不到上述依赖模块,使用--with-openssl=<openssl_dir>、--with-pcre=<pcre_dir>、--with-zlib=<zlib_dir>指定依赖的模块目录。如已安装过,此处的路径为安装目录;若未安装,则此路径为编译安装包路径,nginx将执行模块的默认编译安装。
sudo find /usr/local/ | grep "openssl.*"
/usr/local/bin/openssl
/usr/local/ssl/openssl.cnf
/usr/local/ssl/openssl.cnf.dist
/usr/local/include/openssl

./configure --with-openssl=/usr/local/bin/openssl

启动nginx之后,浏览器中输入http://localhost可以验证是否安装启动成功。

成功网页:
Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

1、验证nginx配置文件是否正确
cd /usr/local/nginx/sbin
sudo ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
说明配置文件正确!
方法二:在启动命令-c前加-t
sudo /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

2.Nginx配置
cd /usr/local/nginx/conf
tree |grep -v default
除了nginx.conf,其余配置文件,一般只需要使用默认提供即可。


2.1.nginx.conf
tarena@tedu:/usr/local/nginx/conf$ diff fastcgi.conf fastcgi_params
2d1
< fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

因此不再建议大家使用以下方式(搜了一下,网上大量的文章,并且nginx.conf的默认配置也是使用这种方式):
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
而使用最新的方式:

include fastcgi.conf;

nginx 简单配置:

#user  nobody;

worker_processes  2;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

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"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;
        #access_log  logs/host.access.log  main;

        location / {
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;  # [1]

            fastcgi_param PATH_INFO $fastcgi_script_name;        # [2]

           #此参数规定了的绑定地址:端口, 

           #使用spawn-fcgi方式启动一个程序:

# spawn-fcgi -d /path/to/www -f /path/to/www/index.py -a 127.0.0.1 -p 9002

            fastcgi_pass 127.0.0.1:9002;
            #root   html;
            #index  index.html index.htm;
        }
        location /static/ {
            if (-f $request_filename) {
            rewrite ^/static/(.*)$  /static/$1 break;
            }
        }

        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}


猜你喜欢

转载自blog.csdn.net/weixin_42193179/article/details/80337716