Linux环境下Nginx安装

  • A+
所属分类:系统文档

Linux环境下Nginx安装

新建一个名为www的用户组

 [root@os11728 src]# groupadd www

 

www用户组下新建一个名为www的用户

 [root@os11728 src]# useradd -g www www

1.1.  安装 pcre

PcreNginx所必需的库。

[root@os11728 src]# wget http://sourceforge.net/projects/pcre/files/pcre/8.30/pcre-8.30.tar.gz/download

[root@os11728 src]# tar -zxvf pcre-8.30.tar.gz

[root@os11728 src]# cd pcre-8.30

[root@os11728 pcre-8.30]# ./configure –prefix=/usr/local/pcre –enable-utf8 –enable-unicode-properties –enable-pcregrep-libz –enable-pcregrep-libbz2

[root@os11728 pcre-8.30]# make

[root@os11728 pcre-8.30]# make install

1.2.  安装Nginx

[root@os11728 src]# wget http://nginx.org/download/nginx-1.3.3.tar.gz

[root@os11728 src]# tar -zxvf nginx-1.3.3.tar.gz

[root@os11728 src]# cd nginx-1.3.3

[root@os11728 nginx-1.3.3]# ./configure –user=www –group=www –prefix=/usr/local/nginx –with-http_stub_status_module –with-http_ssl_module –with-http_gzip_static_module –with-pcre=/usr/local/src/pcre-8.30

[root@os11728 nginx-1.3.3]# make

[root@os11728 nginx-1.3.3]# make install

1.3.  修改Nginx配置文件

conf目录下新建了两个文件夹 extravhosts,一个是放置Nginx的扩展配置,一个是放置虚拟主机配置。

[root@os11728 nginx-1.3.3]# mkdir /usr/local/nginx/conf/extra

[root@os11728 nginx-1.3.3]# mkdir /usr/local/nginx/conf/vhosts

修改Nginx配置文件:

[root@os11728 nginx-1.3.3]# vi /usr/local/nginx/conf/nginx.conf

主要修改的内容如下:

#user  nobody;

 

user www www;

 

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;

 

    #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;

    log_format  access  ‘$remote_addr – $remote_user [$time_local] “$request” ‘

             ‘$status $body_bytes_sent “$http_referer” ‘

             ‘”$http_user_agent” $http_x_forwarded_for’;

    #access_log  logs/access_ng_finet230_cn.log  main;

access_log  logs/access_ng_finet230_cn.log  access;

 

 

    sendfile        on;

    tcp_nopush     on;

 

    #keepalive_timeout  0;

    keepalive_timeout  65;

 

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

 

    server {

        listen       8080;

        server_name  192.168.1.230;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            root   html;

            index  index.html index.htm index.php;

        }

 

        #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;

    #    server_name  localhost;

 

    #    ssl                  on;

    #    ssl_certificate      cert.pem;

    #    ssl_certificate_key  cert.key;

 

    #    ssl_session_timeout  5m;

 

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;

    #    ssl_prefer_server_ciphers   on;

 

    #    location / {

    #        root   html;

    #        index  index.html index.htm;

    #    }

    #}

    #include /usr/local/nginx/conf/extra/fastcgi.conf;

#include /usr/local/nginx/conf/extra/gzip.conf;

include /usr/local/nginx/conf/vhosts/*.conf;

 

}

 

1.4.  创建虚拟主机

 

[root@os11728 httpd-2.2.22]# vi /usr/local/nginx/conf/vhosts/www_finet230_cn.conf

内容如下:

  server {

        listen       8080;

        server_name  ng.fine230.cn finet85.cn;

 

         root  /var/www/root/ng_finet230_cn;

 

    #激活/关闭自动索引

       autoindex on;

 

        #设定索引时文件大小的单位(B,KB, MB GB)

       #默认为on,显示出文件的确切大小,单位是bytes

       #改为off后,显示出文件的大概大小,单位是kB或者MB或者GB

       autoindex_exact_size off;

 

        #开启以本地时间来显示文件时间的功能。默认为关(GMT时间)

       #默认为off,显示的文件时间为GMT时间。

       #改为on后,显示的文件时间为文件的服务器时间

       autoindex_localtime on;

 

        #charset koi8-r;

 

        location / {

            index  index.html index.htm index.php;

        }

 

        #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  /var/www/root/ng_finet230_cn;

        }

 

        # 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;

        #}

 

        #将客户端的请求转交给fastcgi

        location ~ .*\.(php|php5|shtml)?$ {

            #root           html;

            fastcgi_pass   127.0.0.1:9000;#这里指定了fastcgi进程侦听的端口,nginx就是通过这里与php交互的

            fastcgi_index  index.php;

            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

            include        fastcgi_params;

        }

 

       #网站的图片较多,更改较少,将它们在浏览器本地缓存30

       location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

       {

         expires      30d;

       }

 

       #网站会加载很多JSCSS,将它们在浏览器本地缓存1小时

       location ~ .*\.(js|css)?$

       {

         expires      1h;

       }

 

    }

 

 

    # 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;

    #    server_name  localhost;

 

    #    ssl                  on;

    #    ssl_certificate      cert.pem;

    #    ssl_certificate_key  cert.key;

 

    #    ssl_session_timeout  5m;

 

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;

    #    ssl_prefer_server_ciphers   on;

 

    #    location / {

    #        root   html;

    #        index  index.html index.htm;

    #    }

    #}

 

    server

    {

      listen  8080;

      server_name  status.ng.finet230.cn;

 

      location / {

        stub_status on;

        access_log   off;

      }

    }

 

 

/var/www/root/ng_finet230_cn目录下的所有档案与子目录的拥有者皆设为www群体的使用者www :

 [root@os11728 ~]# chown -R www:www /var/www/root/ng_finet230_cn

 

 

1.5.  Nginx的启动与关闭

启动Nginx

 [root@os11728 ~]# ulimit -SHn 65535

[root@os11728 ~]# /usr/local/nginx/sbin/nginx

 

停止Nginx

 [root@os11728 ~]# /usr/local/nginx/sbin/nginx -s stop

[root@os11728 ~]# /usr/local/nginx/sbin/nginx -s quit

 

重启Nginx

[root@os11728 ~]# /usr/local/nginx/sbin/nginx -s reload

[root@os11728 ~]# kill -HUP `cat /usr/local/nginx/logs/nginx.pid`

 

配置开机自动启动Nginx + PHP

[root@os11728 ~]# vi /etc/rc.local

在末尾增加以下内容:

ulimit -SHn 65535

/usr/local/php/sbin/php-fpm

/usr/local/nginx/sbin/nginx

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: