Py学习  »  NGINX

nginx笔记—安装配置

dreamym.wgx • 3 年前 • 229 次点击  

nginx简介

Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。
百度、京东、新浪、网易、腾讯、淘宝等都在使用nginx服务器。

Nginx的特点:

稳定性极强。7*24小时不间断运行。
Nginx提供了非常丰富的配置实例。
占用内存小,并发能力强
能承受5w并发

nginx官网: http://nginx.org/

软件包: 下载地址

安装

  • yum安装依赖关系
 yum -y install pcre-devel zlib-devel
  • 1
  • 1
  • 创建用户
 useradd -M -s /sbin/nologin nginx
  • 1
  • 1
  • 安装nginx(将下载的源码包解包安装)
tar zxvf nginx-1.12.0.tar.gz -C /usr/src/tar zxvf nginx-1.12.0.tar.gz -C /usr/src/

cd /usr/src/nginx-1.12.0/

./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

--with-http_stub_status_module:安装nginx统计模块

  • 编译安装:
    make && make install

  • 优化路径
    ln -s /usr/local/nginx/sbin/* /usr/local/sbin/

  • 创建服务脚本
    vim /etc/rc.d/init.d/nginx

#!/bin/bash
#chkconfig: 345 85 21
case $1 in
start)
        /usr/local/sbin/nginx
        ;;
stop)
        killall -9 nginx
        rm -f /var/run/nginx.pid
        ;;
restart)
        $0 stop
        $0 start
        ;;
*)
        echo "start|stop|restart"
        ;;
esac
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 赋予权限,给系统添加服务脚本
chmod +x /etc/rc.d/init.d/nginx
  • 1
  • 1
chkconfig --add nginx
  • 1
  • 1

重启服务

关闭: systemctl stop nginx
开启: systemctl start nginx

此时安装成功即可访问默认站点
在这里插入图片描述

配置

主配置文件位置: /usr/local/nginx/conf/nginx.conf

参数解释




    
user  nginx;  默认管理用户

worker_processes  1;   指定处理器数量,CPU会影响对用户请求的处理量

pid     logs/nginx.pid;    进程ID号管理文件

events {
    worker_connections  1024;		//连接数量
     use epoll;
}

http {				//虚拟主机
    include       mime.types;
服务支持的文件类型	路径:/usr/local/nginx/conf/mime.types
    default_type  application/octet-stream;
默认MIME类型
    sendfile        on;
用户提升硬盘传输,如果构建文件下载类站点,则关闭此选项,用户平衡网络接口的I/O传输
    keepalive_timeout  65;
保持会话超时时间,0表示不保持会话
    #gzip  on;		//支持压缩文件

    server {			//web站点配置
        listen       80;		//监听端口
        server_name  localhost;			//主机头部名称(域名)
        charset utf-8;				//语言类型
			
       location / {		//站点配置
            root   html;	
站点页面根目录,默认位置:/usr/local/nginx/html/index.html
            index  index.html index.htm;		//主页索引文件
        }
 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

创建网站

修改配置文件即可,IP端口号均可自己修改
在这里插入图片描述

mkdir -p /var/www/baidu
mkdir -p /var/www/sohu
vim /var/www/baidu/index.html	     内容:baidu(自定)
vim /var/www/sohu/index.html	     内容:sohu(自定)
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4
  • 没有DNS时,编辑虚拟主机文件
    vim /etc/hosts
192.168.1.1 www.baidu.com
192.168.1.1 www.sohu.com
  • 1
  • 2
  • 1
  • 2

重启服务访问验证

systemctl restart nginx
  • 1
  • 1

firefox http://www.baidu.com
firefox http://www.sohu.com

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/105823
 
229 次点击