您的位置:首页 > 运维架构 > Nginx

Nginx安装与配置

2017-07-17 16:38 211 查看

Linux(CentOS)下,下载安装Nginx并配置

一、yum安装Nginx

运行命令yum -y install nginx 然后自动安装

 

Yum安装&编译安装的区别:

①编译安装使用的是源码编译后进行安装,这样可以在各个Linux系统下使用,而yum安装只能在redhat或centos系统使用;

②编译安装的源码包可以获取官方最新发行的版本,而yum安装的版本较低。

 

二、编译安装Nginx

1、准备工作

 

选首先安装这几个软件:GCC,PCRE(Perl Compatible Regular Expression),zlib,OpenSSL。

Nginx是C写的,需要用GCC编译;

Nginx的Rewrite和HTTP模块会用到PCRE;

Nginx中的Gzip用到zlib;

 

1.1 gcc的安装

用命令“# gcc”,查看gcc是否安装;如果出现“gcc: no input files”信息,说明已经安装好了。

否则,就需要用命令“# yum install gcc”,进行安装了!(这要确保linux与互联网是联通的!!!)一路可能需要多次输入y,进行确认。

安装好后,可以再用命令“#gcc”测试,或者用命令“# gcc -v”查看其版本号。

同样方法,用如下命令安装PCRE,zlib,OpenSSL(其中devel,是develop开发包的意思);yum install 和 yum install -y的区别是:-y就是一路可能需要多次输入y变成自动输入!

 

1.2 安装 PCRE

yum install -y pcre pcre-devel命令安装PCRE

pcre-config --version命令检测一下

 

1.3 安装zlib

yum install -y zlib zlib-devel命令安装zlib

 

1.4 安装OpenSSL

yum install -y openssl openssl-devel命令安装openssl

 

2. 下载并安装nginx

首先,检测wget命令是否有效(一般linux最小化安装时,wget不会默认被安装。yum安装:yum -y install wget)

 


2.1 创建目录(nginx-src)并进去;然后,从官方地址(http://nginx.org/)下载,解压,配置,编译,安装:

# mkdir nginx-src && cd nginx-src  

# wget http://nginx.org/download/nginx-1.7.3.tar.gz 

 


   

# tar -xzf nginx-1.7.3.tar.gz   

# cd nginx-1.7.3  

 


# ./configure  

 


./configure是源代码安装的第一步,主要的作用是对即将安装的软件进行配置,检查当前的环境是否满足要安装软件的依赖关系,但并不是所有的tar包都是源代码的包,用ls看看有没有configure这个文件,也许你下的是二进制的包,如果是二进制的包,解压后直接就能使用

 

# make  

Make这一步是用来编译源代码软件,make是一个工程化的工具,通过makefile可以简化整个工程的编译任务,提高开发效率。从网上下载的开放源代码软件,绝大多数都需要使用make指令来完成编译工作。

 

# make install  

 在此补充一下编译安装的知识,如下图所示

 


 

# whereis nginx  

 


 

 2.2 默认的安装路径为:/usr/
d070
local/nginx;跳转到其目录下sbin路径下,便可以启动或停止它了。

 


 

然后#./nginx -h

 


 

在默认的情况下,Nginx被安装在目录/usr/local/nginx/

                Nginx的二进制文件路径为/usr/local/nginx/sbin/nginx

                Nginx的配置文件路径为/usr/local/nginx/conf/nginx.conf

 


              Nginx网页文件存放在 html 中; Nginx日志文件存放在 logs 中

2.3 Nginx 配置

配置nginx.conf ,

将/usr/local/nginx/conf/nginx.conf替换为以下内容

cat /usr/local/nginx/conf/nginx.conf

#############仅供参考,建议查阅资料,自主配置####最后附录有资料########

user nobody;

worker_processes 2; #设置值和CPU核心数一致

error_log /usr/local/nginx/logs/nginx_error.log crit; #日志位置和日志级别

pid /usr/local/nginx/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.

worker_rlimit_nofile 65535;

events

{

  use epoll;

  worker_connections 65535;

}

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

  

#charset gb2312;

     

  server_names_hash_bucket_size 128;

  client_header_buffer_size 32k;

  large_client_header_buffers 4 32k;

  client_max_body_size 8m;

     

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

  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;

 

  #limit_zone crawler $binary_remote_addr 10m;

 #下面是server虚拟主机的配置

 server

  {

    listen 80;#监听端口

    server_name localhost;#域名

    index index.html index.htm index.php;

    root /usr/local/nginx/html;#站点目录

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

    {

      #fastcgi_pass unix:/tmp/php-cgi.sock;

      fastcgi_pass 127.0.0.1:9000;

      fastcgi_index index.php;

      include fastcgi.conf;

    }

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

    {

      expires 30d;

  # access_log off;

    }

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

    {

      expires 15d;

   # access_log off;

    }

    access_log off;

  }

 

}

 

检查配置文件ngnix.conf的正确性命令:

/usr/local/nginx/sbin/nginx -t

 


 

2.4 启动 Nginx

Nginx 启动命令如下:

/usr/local/nginx/sbin/nginx

如果遇到这种情况:

 


 

按照如下步骤执行

 


 

最后,继续/usr/local/nginx/sbin/nginx启动

附:Nginx 其他命令

/usr/local/nginx/sbin/nginx -s reload     # 重新载入配置文件

/usr/local/nginx/sbin/nginx -s reopen     # 重启 Nginx/usr/local/nginx/sbin/nginx -s stop # 停止 

 

2.5 编写/etc/init.d/nginx脚本,添加到系统服务




 

使用命令“# vi /etc/init.d/nginx”,打开编辑器,输入如下内容:

    #!/bin/sh  

    # chkconfig: 2345 85 15  

    # Startup script for the nginx Web Server  

    # description: nginx is a World Wide Web server.   

    # It is used to serve HTML files and CGI.  

    # processname: nginx  

    # pidfile: /usr/local/nginx/logs/nginx.pid  

    # config: /usr/local/nginx/conf/nginx.conf  

      

    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin  

    DESC="nginx deamon"  

    NAME=nginx  

    DAEMON=/usr/local/nginx/sbin/$NAME  

    SCRIPTNAME=/etc/init.d/$NAME  

      

    test -x $DAEMON || exit 0  

      

    d_start(){  

      $DAEMON || echo -n "already running"  

    }  

      

    d_stop(){  

      $DAEMON -s quit || echo -n "not running"  

    }  

      

      

    d_reload(){  

      $DAEMON -s reload || echo -n "can not reload"  

    }  

      

    case "$1" in  

    start)  

      echo -n "Starting $DESC: $NAME"  

      d_start  

      echo "."  

    ;;  

    stop)  

      echo -n "Stopping $DESC: $NAME"  

      d_stop  

      echo "."  

    ;;  

    reload)  

      echo -n "Reloading $DESC conf..."  

      d_reload  

      echo "reload ."  

    ;;  

    restart)  

      echo -n "Restarting $DESC: $NAME"  

      d_stop  

      sleep 2  

      d_start  

      echo "."  

    ;;  

    *)  

      echo "Usage: $ScRIPTNAME {start|stop|reload|restart}" >&2  

      exit 3  

    ;;  

    esac  

      

    exit 0  

 

然后,给这个文件可执行权限:

# chmod  +x  /etc/init.d/nginx

 

接着,在当前runlevel下增加新的服务名称

chkconfig --add nginx

 

最后,显示nginx服务配置

chkconfig --list nginx

 


 

从输出中可以看出,在runlevel 0、runlevel 1、runlevel 6下,nginx服务是关闭的。

用runlevel命令可以显示当前运行级别。


 

好了,现在可以使用 start,stop 这些参数控制Nginx服务了

service  nginx  start #开启服务

service  nginx  stop  #停止服务

service  nginx restart #重启服务

service  nginx reload #重装服务

 

记得查看防火墙配置,并修改防火墙

(vi /etc/sysconfig/iptables),开放80端口。


 

重启防火墙,再查看端口

 


 

最后,访问服务器的IP

 


 

 

 

 


 

 

附一:/usr/local/nginx/conf/nginx.conf 安装时的默认配置

 

user  nobody;

worker_processes  8;

 

#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 / {

            root   html;

            index  index.html index.htm;

        }

 

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

    #    }

    #}

 

}

附二:Linux 7个运行级别说明

 




 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: