Nginx 下载地址 :https://pan.baidu.com/s/1Vo5mtRB4HE_EcsjcJCuLdwhtml
实验环境:linux
主机 | IP |
centos7.5 | 192.168.116.133 |
Nginx安装nginx
1.应具有的基础环境c++
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.步骤vim
# 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.测试,启动。centos
# systemctl disable firewalld.service # systemctl stop firewalld.service 关闭防火墙 # nginx 启动服务 # netstat -ntap | grep 80 查看端口状态 # elinks 使用工具查看网页是否存在,没有就使用工具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 1 esac 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; #错误日志保存位置 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #进程号保存文件 #pid logs/nginx.pid; #每一个进程最大链接数(最大链接=链接数x进程数)每一个worker容许同时产生多少个连接,默认1024 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压缩 #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、50二、50三、504都会返回50.html错误页面。 error_page 500 502 503 504 /50x.html; #location后面是"="的话,说明是精确匹配 location = /50x.html { root html; }
5结果bash
在浏览器中输入IP地址。app