centos基于Nginx安装及相关配置

一、Nginx服务的基本简介

Nginx由俄罗斯的lgor Sysoev开发,专为性能优化而开发,其最知名的优点就是它的稳定性和低系统资源消耗、以及对HTTP并发连接的高处立能力(单台物理服务器可支持30000~50000个并发请求)。正因为如此,大量提供社交网络、新闻资讯、电子商务及虚拟主机等服务的企业纷纷选择Nginx来提供Web服务。

搭建Web服务如果是为了解析静态网页、动态网页等、不需要太多的功能,那么Nginx绝对是首选。

二、编译安装Nginx服务

mobaxterm的网盘链接:https://pan.baidu.com/s/1TaEaDZmuHWkde0LJAREdEg
提取码:gfe1
复制这段内容后打开百度网盘手机App,操作更方便哦

nginx的网盘链接:https://pan.baidu.com/s/1b8onp63OIUKX83Z9EsBr3g
提取码:b59a
复制这段内容后打开百度网盘手机App,操作更方便哦

将这两个下载下来,用mobaxterm远程控制虚拟机,将nginx压缩包上传到/root目录下

1.编译安装Nginx服务

1)安装支持软件

Nginx的配置及运行需要pcre(支持正则表达式)、zlib(支持压缩)等软件包的支持,因此应先安装这些软件的开发包,以便提供提供相应的功能,确保Nginx的安装顺利完成:

[root@VM_0_16_centos ~]# yum -y install pcre-devel zlib-devel

2)创建运行用户、组

[root@VM_0_16_centos ~]# useradd -M -s /sbin/nologin nginx

3)编译安装Nginx

[root@VM_0_16_centos ~]# tar zxf nginx-1.12.0.tar.gz -C /usr/src^C
[root@VM_0_16_centos ~]# cd /usr/src/nginx-1.12.0/
[root@VM_0_16_centos nginx-1.12.0]#  ./configure --prefix=/usr/local/nginx \
>  --user=nginx --group=nginx --with-http_stub_status_module
[root@VM_0_16_centos nginx-1.12.0]#  make && make install

4)优化路径

[root@VM_0_16_centos nginx-1.12.0]#  ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
2.Nginx服务的运行控制

1)检查Nginx服务配置文件

与Apache的主程序httpd相似,Nginx也是使用“-t”选项对其配置文件进行语法检查。若要检查位于其他位置的配置文件,可使用“-c”选项来指定路径。

[root@VM_0_16_centos nginx-1.12.0]#  nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

2)启动、停止Nginx服务

[root@VM_0_16_centos nginx-1.12.0]# nginx
[root@VM_0_16_centos nginx-1.12.0]#  netstat -anpt | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      4472/nginx: master
//默认tcp协议端口80
[root@VM_0_16_centos nginx-1.12.0]#
[root@localhost ~]#curl http://122.51.14.35   //可以使用curl命令进行文本浏览器检查
[root@VM_0_16_centos nginx-1.12.0]# killall -s HUP nginx  //重新加载nginx配置文件
[root@VM_0_16_centos nginx-1.12.0]#  killall -s QUIT nginx  //停止nginx服务

3)添加Nginx服务为系统服务

为了使Nginx服务的启动、停止、重载等操作更加方便,可以编写Nginx服务脚本。脚本内容如下:

[root@VM_0_16_centos ~]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20
PROG="/usr/local/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
        $PROG
;;
stop)
        kill -s QUIT $(cat $PIDF)
;;
restart)
        $0 stop
        $0 start
;;
reload)
        kill -s HUP $(cat $PIDF)
;;
*)
        echo "Usage: $0 {start|stop|restart|reload}"
        exit 1
esac
        exit 0
[root@VM_0_16_centos ~]#  chmod +x /etc/init.d/nginx
[root@VM_0_16_centos ~]# chkconfig --add nginx
[root@VM_0_16_centos ~]#  systemctl start nginx
3.Nginx服务配置文件详解
[root@VM_0_16_centos ~]# vim /usr/local/nginx/conf/nginx.conf
......省略部分
events {
    use epoll;                                                     //使用epoll模型
    worker_connections  1024;                         //每个进程处理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 {                                                    //Web服务的监听配置
        listen       80;                                       //监听地址及端口
        server_name  localhost;                     //网站名称
        charset utf-8;                                      //网页的默认字符集
        location / {                                          //根目录配置(必须存在)
            root   html;                                     //网站根目录的位置
            index  index.html index.php;         //默认首页
        }
        error_page   500 502 503 504  /50x.html;              //内部错误的反馈页面
        location = /50x.html {                                              //错误页面配置
            root   html;
        }
 
    }
}
.......省略部分
location /status{
        stub_status on;
        access_log  off;
        }

重新启动nginx:

[root@VM_0_16_centos ~]# systemctl restart nginx

用户测试访问:此时输入http://122.51.14.35,访问的就nginx主页了。

nginx android安装包 安卓手机安装nginx_centos


也可以输入http://122.51.14.35/html/index.html访问在html下的其他网页