下载:http://nginx.org/
安装指导文档:http://nginx.org/en/docs/install.html
编译安装
[root@linux2019 src]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
[root@linux2019 src]# tar zxvf nginx-1.14.2.tar.gz
[root@linux2019 src]# cd nginx-1.14.2
[root@linux2019 nginx-1.14.2]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module
[root@linux2019 nginx-1.14.2]# echo $?
0
[root@linux2019 nginx-1.14.2]# make && make install
[root@linux2019 nginx-1.14.2]# echo $?
[root@linux2019 nginx-1.14.2]# /usr/local/nginx/sbin/nginx
#报错:缺失软件包
yum install -y openssl-devel
yum install -y pcre-devel
YUM安装nginx
官方安装指导文档说明:
To set up the yum repository for RHEL/CentOS, create the file named /etc/yum.repos.d/nginx.repo with the following contents:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/
gpgcheck=0
enabled=1
Replace “OS” with “rhel” or “centos”, depending on the distribution used, and “OSRELEASE” with “6” or “7”, for 6.x or 7.x versions, respectively.
[root@linux2019 nginx]# vi /etc/yum.repos.d/nginx.repo #添加nginx的yum仓库源
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
[root@linux2019 nginx]# yum list |grep nginx
nginx.x86_64 1:1.14.2-1.el7_4.ngx @nginx
nginx-debug.x86_64 1:1.8.0-1.el7.ngx nginx
nginx-debuginfo.x86_64 1:1.14.2-1.el7_4.ngx nginx
nginx-module-geoip.x86_64 1:1.14.2-1.el7_4.ngx nginx
nginx-module-geoip-debuginfo.x86_64 1:1.14.2-1.el7_4.ngx nginx
nginx-module-image-filter.x86_64 1:1.14.2-1.el7_4.ngx nginx
nginx-module-image-filter-debuginfo.x86_64 1:1.14.2-1.el7_4.ngx nginx
nginx-module-njs.x86_64 1:1.14.2.0.2.7-1.el7_4.ngx nginx
nginx-module-njs-debuginfo.x86_64 1:1.14.2.0.2.7-1.el7_4.ngx nginx
nginx-module-perl.x86_64 1:1.14.2-1.el7_4.ngx nginx
nginx-module-perl-debuginfo.x86_64 1:1.14.2-1.el7_4.ngx nginx
nginx-module-xslt.x86_64 1:1.14.2-1.el7_4.ngx nginx
nginx-module-xslt-debuginfo.x86_64 1:1.14.2-1.el7_4.ngx nginx
nginx-nr-agent.noarch 2.0.0-12.el7.ngx nginx
pcp-pmda-nginx.x86_64 4.1.0-5.el7_6 updates
[root@linux2019 nginx]# yum install -y nginx.x86_64
[root@linux2019 nginx]# systemctl start nginx
yum安装的软件包的配置文件在/etc/下相应软件名的路径下
[root@linux2019 ~]# cd /etc/nginx/
[root@linux2019 nginx]# ll
总用量 36
drwxr-xr-x 2 root root 26 1月 24 10:52 conf.d
-rw-r--r-- 1 root root 1007 12月 4 23:03 fastcgi_params
-rw-r--r-- 1 root root 2837 12月 4 23:03 koi-utf
-rw-r--r-- 1 root root 2223 12月 4 23:03 koi-win
-rw-r--r-- 1 root root 5170 12月 4 23:03 mime.types
lrwxrwxrwx 1 root root 29 1月 24 10:52 modules -> ../../usr/lib64/nginx/modules
-rw-r--r-- 1 root root 643 12月 4 23:01 nginx.conf
-rw-r--r-- 1 root root 636 12月 4 23:03 scgi_params
-rw-r--r-- 1 root root 664 12月 4 23:03 uwsgi_params
-rw-r--r-- 1 root root 3610 12月 4 23:03 win-utf
[root@linux2019 ~]# nginx -v #查看nginx版本
nginx version: nginx/1.14.2
[root@linux2019 ~]# nginx -V #查看nginx编译参数
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
[root@linux2019 ~]# nginx -t #查看nginx配置文件有没有语法错误
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@linux2019 ~]# nginx -s reload
PS
- pkill nginx #杀死nginx进程
- kill -9 pid #根据nginx进程的pid杀死一个进程
- yum仓库配置参数说明:
[base] #源名称,必要
name=CentOS-$releasever - Base - mirrors.aliyun.com
failovermethod=priority #源描述,非必要
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/ #源地址,必要
enabled=1 #是否启用该仓库,默认开启
gpgcheck=1 #是否校验包来源
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7 #校验url
nginx虚拟主机
虚拟主机,就是把一台物理服务器划分成多个“虚拟”的服务器,每一个虚拟主机都可以有独立的域名和独立的目录,可以实现在一台服务器上虚拟出多个网站。nginx的虚拟主机就是通过nginx.conf中server的配置,想要设置多个虚拟主机,配置多个server节点即可。
server {
listen 80; #监听端口
server_name localhost; #域名或者IP,可是同时指定多个
#charset koi8-r; #网页字符集
#access_log /var/log/nginx/host.access.log main; #登陆日志
location / {
root /usr/share/nginx/html; #网页文件位置
index index.html index.htm; #索引文件
}
一个网站配置文件最主要的两个参数:servier_name和root。
建议以域名来定义虚拟主机配置文件,方便区分,在配置不同的nginx虚拟主机时不建议把所有的server配置在同一个配置文件中。
配置虚拟主机的三种方法:
- 基于IP:虚拟出一个网卡,配置一个新IP,修改nginx.conf配置文件中server_name
- 基于端口
- 基于域名
配置完nginx虚拟主机配置文件后
- nginx -t #查看配置文件是否有错误
- nginx -s reload #重载配置文件
或者重启systemctl restart nginx #不建议重启 - 将域名解析添加到/etc/hosts文件中,(windows:C:\Windows\System32\drivers\etc\hosts)
默认虚拟主机
- /etc/nginx/conf.d/目录下排在第一个的就是默认虚拟主机
- 手动指定:或者在主机配置文件中监听端口添加配置(default_server)
- 禁掉默认虚拟主机,加一行 deny all;
PS1
- 关闭firewalld服务: systemctl stop firewalld, 不建议直接关闭防火墙,建议采取添加防火墙规则的方式放行端口访问,
- firewall-cmd --add-port=80/tcp --permanent(永久生效)
- firewall-cmd --reload