Nginx

Nginx

一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,特点:占有内存少,并发能力强(epoll)。

一、下载Nginx:进入 http://nginx.org/en/download.html ,下载最新的Nginx(nginx-1.3.15.tar.gz)并解压。

二、安装开发工具提供编译环境

yum groupinstall "Development Tools"

三、通过源代码安装Nginx,指定安装路径:/usr/local/nginx。

[root@bogon ~]# cd nginx-1.3.15/
[root@bogon nginx-1.3.15]# ls
auto     CHANGES.ru  configure  html     Makefile  objs    src
CHANGES  conf        contrib    LICENSE  man       README
[root@bogon nginx-1.3.15]# ./configure --prefix=/usr/local/nginx

如果有提示缺少库,可通过YUM安装。如 PCRE 的库:

[root@bogon nginx-1.3.15]# yum list | grep pcre
pcre.x86_64                            7.8-6.el6                        @base   
pcre-devel.x86_64                      7.8-6.el6                        @base   
mingw32-pcre.noarch                    8.10-2.el6.5                     base    
pcre.i686                              7.8-6.el6                        base    
pcre-devel.i686                        7.8-6.el6                        base    
pcre-static.x86_64                     7.8-6.el6                        base    
[root@bogon nginx-1.3.15]# yum install pcre-devel

安装成功提示信息:

creating objs/Makefile

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using builtin md5 code
  + sha1 library is not found
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

 四、编译

[root@bogon nginx-1.3.15]# make
[root@bogon nginx-1.3.15]# make install

五、启动Nginx

[root@bogon nginx-1.3.15]# cd /usr/local/nginx/
[root@bogon nginx]# cd sbin
[root@bogon sbin]# ./nginx

查看进程,可看出 Nginx 的结构:一个主进程和多个工作进程,工作进程是单线程的。

[root@bogon sbin]# ps aux | grep nginx
root     18346  0.0  0.0  20196   580 ?        Ss   21:29   0:00 nginx: master process ./nginx
nobody   18347  0.1  0.1  20592  1304 ?        S    21:29   0:03 nginx: worker process
root     19554  0.0  0.0 103240   828 pts/0    S+   22:13   0:00 grep nginx

查看监控

[root@bogon sbin]# netstat -tupln | grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      18346/nginx

六、停止Nginx,向 Nginx 主进程发送信号。

[root@bogon sbin]# kill -s QUIT 18346

 Nginx 配置文件

[root@bogon nginx]# cat 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;

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

}
nginx.conf

Nginx 主页:http://127.0.0.1/,访问的路径:/usr/local/nginx/html/index.html。

七、通过 httpd,测试 Nginx 性能:启动一个并发,10000次请求。

[root@bogon nginx]# yum install -y httpd
[root@bogon nginx]# ab -c 1 -n 10000 http://127.0.0.1/index.html

在打印的日志中,通过 Requests per second:    7391.48 [#/sec] (mean) 可看到每秒完成 4627 个请求。

apache命令行启动

通过命令行开关来控制Apache服务:

启动Apache服务:httpd -k start

停止Apache服务:httpd -k stop 或 httpd -k shutdown

重启Apache服务,强制它重新读取配置文件:httpd -k restart 

原文地址:https://www.cnblogs.com/xuekyo/p/3232305.html