centos6.5安装nginx-1.8.0,nginx-1.8.0


1.Nginx安装环境:

  gcc: 安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gccyum install gcc-c++

  pcre:PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginxhttp模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。yum install -y pcre pcre-devel 注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。

  zlib:zlib库提供了很多种压缩和解压缩的方式,nginx使用zlibhttp包的内容进行gzip,所以需要在linux上安装zlib库。yum install -y zlib zlib-devel

  openssl:OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。yum install -y openssl openssl-devel

2.清除系统中的httpd痕迹:
  yum remove httpd
  rm -rvf /etc/httpd

3.创建www用户和用户组:
groupadd www
useradd -s /sbin/nologin -g www www

5.解压、配置、编译、安装nginx:
tar zxvf nginx-1.8.0.tar.gz -C /usr/src/
cd /usr/src/nginx-1.8.0/
./configure --prefix=/usr/local/nginx \
--user=www \
--group=www \
--with-mail \
--with-mail_ssl_module \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_dav_module \
--with-http_sub_module \
--with-http_spdy_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-pcre
make && make install

6.编辑nginx.conf配置文件:
vim /usr/local/nginx/conf/nginx.conf
user www www;
worker_processes auto;
pid /home/www/pid/nginx.pid;
worker_rlimit_nofile 51200;

events
    {
        use epoll;
        worker_connections 51200;
        multi_accept on;
    }
http
    {
        include mime.types;
        default_type application/octet-stream;
        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;
        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 256k;
      
        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;
        gzip_proxied expired no-cache no-store private auth;
        gzip_disable "MSIE [1-6]\.";

        server_tokens off;
        log_format access '$remote_addr - $remote_user [$time_local] "$request" '
        '$status $body_bytes_sent "$http_referer" '
        '"$http_user_agent" $http_x_forwarded_for';
    server
        {
            listen 80 default;
            server_name 127.0.0.1;
            access_log /home/www/log/access.log access;
            error_log /home/www/log/error.log error;
            index index.html index.htm index.php;
            root /home/www/html/;
            error_page 404 /404.html;

            location ~ [^/]\.php(/|$)
            {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi.conf;
            }
            location /nginx_status
            {
                stub_status on;
                access_log off;
            }
            location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
            {
                expires 30d;
            }
            location ~ .*\.(js|css)?$
            {
                expires 12h;
            }
        }
    include vhost/*.conf;
    }
7.创建目录并修改权限:
mkdir -p /home/www/log
mkdir -p /home/www/pid
mkdir -p /home/www/html
chown -R www:www /home/www/log
chown -R www:www /home/www/pid
chown -R www:www /home/www/html
chown -R www:www /usr/local/nginx

8.测试启动Nginx

./nginx

9.创建测试页面和错误页面,并赋予权限:
touch /home/www/html/index.html
cat>/home/www/html/index.html<<EOF
This is the test Page for Nginx !!!
EOF
touch /home/www/html/404.html
cat>/home/www/html/404.html<<EOF
Error Page for Nginx !!!
EOF
chown -R www:www /home/www/html

10.测试:
http://localhost/
http://localhost/error

11.防火墙开启80端口:

vim /etc/sysconfig/iptablse.conf
添加 -A INPUT -p tcp --dport 80 -j ACCEPT

12.重启防火墙

service iptables restart

相关内容