编译安装Nginx

10 篇文章 0 订阅

安装make:

yum -y install gcc automake autoconf libtool make

安装g++:

yum -y install gcc gcc-c++

PCRE库:

Nginx需要PCRE(Perl Compatible Regular Expression),因为Nginx的Rewrite模块和Http核心模块都会使用到PCRE正则表达式语法。其下载地址为http://www.pcre.org/,我们也可以通过yum来安装。

yum -y install pcre pcre-devel

zlib库:

zlib库提供了压缩算法,Nginx很多地方都会用到gzip算法。其下载地址为zlib Home Site,也可以通过yum安装。

yum -y install zlib zlib-devel

OpenSSL:

Nginx中如果服务器提供安全页面,就需要用到OpenSSL库。其下载地址为/index.html,也可以通过yum安装。

yum -y install openssl openssl-devel

下载安装

wget https://nginx.org/download/nginx-1.12.2.tar.gz

tar zxf nginx-1.12.2.tar.gz

cd nginx-1.12.2

./configure

------------

#这里建议加上ssl模块,即

./configure --with-http_ssl_module

------------------------------

make && make install

make install没有提示error就没问题

Nginx会默认安装在/usr/local/nginx目录,我们cd到/usr/local/nginx/sbin/目录,存在一个Nginx二进制可执行文件。直接运行就可以启动Nginx。

启动:/usr/local/nginx/sbin/nginx 

关闭:/usr/local/nginx/sbin/nginx  -s stop

重启:/usr/local/nginx/sbin/nginx -s reload

设置成服务并自启动

原始/usr/local/nginx/conf/nginx.conf配置大致如下

```


#user  nobody;
worker_processes  1;

#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;
    include /website/nginx-conf/*.conf;  #支持多站点
    
    #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 / {
            root   html;
            index  index.html index.htm;
        }

}

```

修改nginx的配置文件来支持php文件的解析

vi /usr/local/nginx/conf/nginx.conf

去掉location ~ \.php$ {节点的注释

修改nginx的默认web路径(原来为/usr/local/nginx/html)

vi /usr/local/nginx/conf/nginx.conf

修改成/website/www

在原有的location项下面添加一个location项来处理静态文件(css,js,图片)

#静态资源处理
location ~ .*\.(js|css|gif|jpg|jpeg|png|bmp|swf|mp3|mp4|ttf|woff|svg|eot|txt|html|htm)$
      {
          root /website/www/nzg;
          if (-f $request_filename) {
           expires 1d;
           break;
           }
      }

url重写,隐藏index.php

同上需要另外配置一个location项

    location / {
        root /website/www/nzg;
        index index.php;
        if (!-e $request_filename){   #方式1
            rewrite ^/(.*) /index.php last;
        }
        #try_files $uri $uri/ /index.php$is_args$args;  #方式2
    }

这里可使用两种方式来隐藏index.php

常用server节点配置

    server {
        listen       80;
        #server_name  47.52.110.110;
        server_name mytest.cn;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /website/61group;
            index  index.php index.html index.htm;
            if (!-e $request_filename){   #方式1
                rewrite ^/(.*) /index.php last;
            }
        }

        #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;
        #}
        location ~ \.php$ {
            root /website/61group;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }

        location ~ .*\.(js|css|gif|jpg|jpeg|png|bmp|swf|mp3|mp4|ttf|woff|svg|eot|txt|html|htm)$
        {
            root /website/61group;
            if (-f $request_filename) {
                expires 1d;
                break;
            }
        }
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

进入到/website/www目录

vi index.php

添加

<?php phpinfo();?>

保存

再重启nginx即可在浏览器浏览即可

--------

配置本机的hosts文件

添加

192.168.1.222     mytest.cn

#我是在虚拟机里面安装nginx的,其中192.168.1.222 是虚拟机系统的内网ip,如果nginx安装在本机的话就使用127.0.0.1

---------

如果报错403解决办法

将web目录设置为777权限,-R表示向下递归

chmod -R 777 /website/www

=============不同的子配置文件配置不同的站点===

在主配置文件 nginx.conf中的http节点中添加

include /website/nginx-conf/*.conf;

#这里的目录是自定义目录

http {
    #其他
    include /website/nginx-conf/*.conf;
}

子配置文件的内容只需要server节点

最后需要重新加载配置

执行  service nginx reload

=======关闭访问日志=====

#access_log  logs/access.log  main;
access_log off;

Nginx优化

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值