Nginx 下载地址 :https://pan.baidu.com/s/1Vo5mtRB4HE_EcsjcJCuLdw
实验环境:
主机 | IP |
centos7.5 | 192.168.116.133 |
Nginx安装
1.应具备的基础环境
1). gcc环境
安装Nginx需要先将官网下载的源码进行编译,编译依赖gcc环境。
2).PCRE环境
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。Nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库
3).zlib环境
zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。
2.步骤
# mkdir /abc# mount.cifs //192.168.10.1/bao /abc挂载安装包# cd /abc/LAMP/# tar zxvf nginx-1.12.0.tar.gz -C /opt/解压缩安装包# yum -y install pcre-devel zlib-devel gcc gcc-c++ make安装必须的支持环境# useradd -M -s /sbin/nologin nginx创建nginx用户# cd /opt/nginx-1.12.0/切至nginx目录 ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install 编译安装nginx-----检查、启动、重启、停止--------nginx -t //检查 nginx //启动 killall -1 nginx //重启 killall -3 nginx //停止
3.测试,启动。
# systemctl disable firewalld.service# systemctl stop firewalld.service关闭防火墙# nginx启动服务# netstat -ntap | grep 80查看端口状态# elinks http://192.168.116.133使用工具查看网页是否存在,没有就使用工具YUM装# yum install elinks -y安装elinks# vim /etc/init.d/nginx 写入脚本:#!/bin/bash# chkconfig: - 99 20# description: Nginx Service Control ScriptPROG="/usr/local/nginx/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 1esac exit 0# chmod +x /etc/init.d/nginx添加脚本运行权限# chkconfig --add nginx添加nginx启动脚本为chkconfig管理的一个服务--启动--执行 ./nginx -t 检查配置文件是否成功 usr/local/nginx/sbin/ 执行./nginx启动nginx 执行 ./nginx -s reload 重启
4简单配置
#vim /usr/local/nginx/conf/nginx.conf^配置文件位置^#开启进程数 <=CPU数 worker_processes 1; #错误日志保存位置 #err_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #进程号保存文件 #pid logs/nginx.pid; #每个进程最大连接数(最大连接=连接数x进程数)每个worker允许同时产生多少个链接,默认1024 events {
ro } 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压缩 #gzip on; server { #监听端口,默认是80端口 listen 80; #监听域名 server_name localhost; #charset koi8-r; #nginx访问日志放在logs/host.access.log下,并且使用main格式(还可以自定义格式) #access_log logs/host.access.log main; #如果没有location更明确的匹配访问路径的话,访问请求都会被该location处理。 location / { #root指定nginx的根目录为/usr/local/nginx/html root html; #默认访问文件,欢迎页先去html目录下找index.html,如果找不到再去找index.htm index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # #错误页面及其返回地址,错误码为500、502、503、504都会返回50.html错误页面。 error_page 500 502 503 504 /50x.html; #location后面是"="的话,说明是精确匹配 location = /50x.html { root html; }
5结果