← 回到文章列表

Ubuntu Nginx 安装配置

发现其实Nginx有时候挺神器的 原本的apache2+tomcat7双端口的server 用Nginx整合到了80端口用 这样就不需要开多个vps 浪费空间时间以及金钱啦~ PHP方面: 先是安装Nginx

发现其实Nginx有时候挺神器的
原本的apache2+tomcat7双端口的server
用Nginx整合到了80端口用
这样就不需要开多个vps
浪费空间时间以及金钱啦~


PHP方面:
先是安装Nginx

apt-get install nginx

再是安装mysql以及php

apt-get install php5-cli php5-cgi mysql-server php5-mysql

安装FastCgi PHP解析器

apt-get install spawn-fcgi

启动Nginx

service nginx restart

安装的Nginx在启动时
会自动加载/etc/nginx/conf.d中的配置文件
所以我们想要增加虚拟伺服器的话
直接在其中添加配置文档即可

先编辑/etc/nginx/nginx.conf文档
将server块中的代码如下编辑
使直接ip访问代理到/usr/share/nginx/www/index.html文件上

        server {
                listen         80;
                server_name     _;
        #       access_log      /var/log/nginx/access.log main;
                server_name_in_redirect  off;
                location / {
                        root  /usr/share/nginx/www;
                        index index.html;
                }
        }

开启Gzip压缩
Nginx会对Text类型自动Gz压缩
但是并不会对xml css js压缩
所以我们要去掉下面代码前的注视
将代码生效

       gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application$

然后我们就开始新建虚拟伺服器
在conf.d中新增yumao.name.conf文档
在其中写入以下内容

server {
        listen  80;
        #傳入域名
        server_name  yumao.name www.yumao.name;
        #非www域名進行301跳轉
        if ($host != 'www.yumao.name' ) {
                rewrite  ^/(.*)$  http://www.yumao.name/$1  permanent;
        }
        #設置www根目錄 以及僞靜態設置
        location / {
            root   /var/www/yumao.name;
            index  index.php index.html index.htm;
            #ignored: "-" thing used or unknown variable in regex/rew
            if (!-f $request_filename){
                set $rule_1 1$rule_1;
            }
            if (!-d $request_filename){
                set $rule_1 2$rule_1;
            }
            if ($rule_1 = "21"){
                rewrite /. /index.php last;
            }
        }
        #使用FastCgi解釋php文件
        location ~ \.php$ {
            root /var/www/yumao.name;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        /etc/nginx/fastcgi_params;
        }
}

启动FastGgi 监听端口9000

spawn-fcgi -a 127.0.0.1 -p 9000 -C 10 -u www-data -f /usr/bin/php-cgi

在/var/www/yumao.name中新建index.php
编写内容如下

<?php phpinfo(); ?>

保存后重启Nginx

service nginx restart

使用域名访问即可获得php信息
顺带nginx的用户也是www-data
使用以下命令对www资料夹进行权限分配

chown www-data:www-data -R /var/www/

为了每次开机都可以运行fastcgi
可以将fastcgi的启动命令
添加入/etc/rc.local文档中
JSP方面:
安装jre

apt-get install openjdk-7-jre

在官网下载Tomcat
解压至/var/www/teemo.name
运行/var/www/teemo.name/bin/startup.sh驱动tomcat

在/etc/nginx/conf.d中添加teemo.name.conf
内容如下:

server {
        listen  80;
        #傳入域名
        server_name  teemo.name www.teemo.name;
        #非www域名進行301跳轉
        if ($host != 'www.teemo.name' ) {
                rewrite  ^/(.*)$  http://www.teemo.name/$1  permanent;
        }
        #將所有teemo.name傳入的請求都轉發至Tomcat
        location / {
            proxy_pass http://localhost:8080;
        }
}

重启Nginx

service nginx restart

使用teemo.name域名访问
即可获得Tomcat默认页面

在这里找找

输入关键词,找一篇过去的记录。