Nginx安装-编译安装
一、安装Nginx所需的pcre库
#直接yum安装了 yum install pcre pcre-devel -y #检查是否已安装 rpm -qa pcre pcre-devel #如果显示有这两个名就说明已经安装成功
二、安装Nginx
安装nginx前,先安装openssl-server 和 gcc-c++
#安装openssl-devel 和 gcc-c++ #不安装gcc-c++是无法编译的 yum install -y openssl openssl-devel gcc-c++ ####### #下载需要安装wget yum install -y wget
nginx官网下载页:http://nginx.org/en/download.html
!!下载稳定版本
本文环境:centos7.3 + Nginx1.14.0
#创建存放安装包的目录 mkdir -p /mnt/app/ # mkdir 是创建目录; #-p表示不提示目录是否存在循环常见所有层级目录,如果存在则忽略 ####################################################### #切换到app目录 cd /mnt/app #用wget下载 wget -q http://nginx.org/download/nginx-1.14.0.tar.gz #tar解压安装包 tar xf nginx-1.14.0.tar.gz #创建nginx用户并加入名为nginx的组 useradd nginx -s /sbin/nologin -M #切换到nginx目录下执行安装 cd nginx-1.14.0 ./configure --user=nginx --group=nginx --prefix=/application/nginx-1.14.0/ --with-http_stub_status_module --with-http_ssl_module #编译安装报错./configure: error: the HTTP rewrite module requires the PCRE library. yum install -y pcre-devel pcre #编译 make && make install 创建nginx软连接 ln -s /application/nginx-1.14.0 /application/nginx
nginx安装参数说明 --prefix=PATH #指定安装路径 --user=USER #进程用户权限 --group=GROUP #进程用户组权限 --with-http_stub_status_module #激活状态信息 --with-http_ssl_module #激活ssl功能
三、启动Nginx
启动前需要检查配置文件
#检查配置文件 /application/nginx/sbin/nginx -t #如果显示下面两行说明安装没有问题 nginx: the configuration file /application/nginx-1.14.0//conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.14.0//conf/nginx.conf test is successful
启动nginx
#启动 /application/nginx/sbin/nginx #启动后检查,会显示端口已启动 netstat -lnt|grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN #关闭防火墙 systemctl stop firewalld #关闭selinux setenforce 0 #临时关闭 vi /etc/selinux/config,将SELINUX = enforcing改为SELINUX = disabled #永久关闭 ###永久关闭selinux设置后,启用临时关闭,等下次重启时配置文件就会生效 #通过客户端浏览器访问服务器地址,会显示nginx页面 如: http://192.168.8.49 #查看nginx进程是否存在 ps -ef|grep nginx root 6665 1 0 11:06 ? 00:00:00 nginx: master process /application/nginx/sbin/nginx nginx 6666 6665 0 11:06 ? 00:00:00 nginx: worker process root 6718 2610 0 11:19 pts/0 00:00:00 grep --color=auto nginx
运行报错处理(看日志)
#查看nginx错误日志 cat /appliction/nginx/logs/error.log
四、编辑nginx配置文件
#编辑配置文件 vi /appliction/nginx/conf/nginx.conf #去掉注释行的配置文件内容 worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; #默认站点目录html,路径/appliction/nginx/html index index.html index.htm; #站点首页文件 } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } #如果需要部署网站,只需要把开发好的网页放到/appliction/nginx/html目录下,删掉默认的index.html #操作过程 cd /appliction/nginx/html/ rm -f index.html vi index.html #index.html内写如网页代码 <h1>hello world</h1> #然后保存退出 #查看index.html文件内容 cat index.html #然后再次访问服务器IP地址
查看Nginx的目录结构
#查看nginx的目录结构 tree /appliction/nginx #如果无法查看请yum安装 tree yum install -y tree ├── client_body_temp ├── conf │ ├── fastcgi.conf │ ├── fastcgi.conf.default │ ├── fastcgi_params │ ├── fastcgi_params.default │ ├── koi-utf │ ├── koi-win │ ├── mime.types │ ├── mime.types.default │ ├── nginx.conf │ ├── nginx.conf.default │ ├── scgi_params │ ├── scgi_params.default │ ├── uwsgi_params │ ├── uwsgi_params.default │ └── win-utf ├── fastcgi_temp ├── html │ ├── 50x.html │ ├── index.html │ └── index.html.bak ├── logs │ ├── access.log │ ├── error.log │ └── nginx.pid ├── proxy_temp ├── sbin │ └── nginx ├── scgi_temp └── uwsgi_temp
#conf目录内.default都是同文件名的备份文件
Nginx主配置文件nginx.conf
#Nginx主配置文件 cat /application/nginx/conf/nginx.conf #主配置文件内容(去掉没用的注释行)
#去掉注释行方法(egrep -v "#|^$" nginx.conf.default > nginx.conf) worker_processes 1; #worker进程数量 events { #事件区块开始 worker_connections 1024; #每个worker进程最大连接数 } #事件区块结束
http { #http区块开始 include mime.types; #nginx支持的媒体类型看文件 default_type application/octet-stream; #默认媒体类型 sendfile on; #开启高效传输 keepalive_timeout 65; #连接超时 server { #第一个server区块(一个server百事一个独立虚拟主机) listen 80; #提供服务端口 server_name localhost; #提供服务的主机名域名
location / { #location区块 root html; #站点目录 index index.html index.htm; #首页文件,多个用空格分开 } error_page 500 502 503 504 /50x.html; #http状态码,使用50x.html回应
location = /50x.html { #访问50x.html root html; } } }