3、Nginx 安装和命令、配置文件
3.0、安装之前
先要安装 pcre
官网下载地址:地址
在 CentOS 中使用命令
先切换 root 用户
su root
cd /etc/local/src
wget https://sourceforge.net/projects/pcre/files/pcre/8.45/pcre-8.45.tar.gz
如果用的是普通用户,遇见了问题
错误: 无法验证 downloads.sourceforge.net 的由 “/C=US/O=Let's Encrypt/CN=R3” 颁发的证书:
颁发的证书已经过期。
要以不安全的方式连接至 downloads.sourceforge.net,使用“--no-check-certificate”。
解决方式
sudo yum install -y ca-certificates
这时需要保证普通用户有root权限
就先要切换root用户,chmod u+w /etc/sudoers 开放 sudoers 文件的权限,然后 vim /etc/sudoers ,加入
root ALL = (ALL) ALL
普通用户名 ALL = (ALL) ALL
保存退出,回复文件 sudoers 权限,chmod u-w /etc/sudoers ,切换用户,继续 sudo yum install -y ca-certificates ,然后接着执行下载命令
或者先这个下载的慢可以自己先去官网下载好文件,然后使用xftp直接拖过去,然后解压文件也是一样的
下载好了后,解压文件
tar -xvf pcre-8.45.tar.gz
中间出问题
configure: error: in `/usr/local/src/pcre-8.45':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
跟你说没有 C 编译环境
所以要 先 yum update -y 再 yum install -y gcc 执行命令后,再执行 ./configure
又出问题
configure: error: Invalid C++ compiler or C++ compiler flags
跟你说没有 C++ 环境
所以要 yum install -y gcc-c++ 执行命令后,再执行 ./configure
这次就好了
检查好了解压的文件后,然后执行 make && make install
命令【编译并安装,这个过程都在pcre解压后的目录里进行】。然后执行 pcre-config --version
出现了版本号就是好了。
然后安装 openssl 、zlib 、 gcc 依赖
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
执行命令,这次倒是很顺利,他会帮你自动检查哪些没安装,以及安装的内容里面需要的其他依赖版本它也会给你控制好,一次性安装好
3.1、在 Linux 中安装 Nginx
做好准备工作后,就能安装 nginx 了
来到 usr/local/src
wget https://nginx.org/download/nginx-1.20.2.tar.gz
或者自己去这个地址下好了用 xftp 传到 usr/local/src 目录下
ls
tar -xvf nginx-1.20.2.tar.gz
ls
cd nginx-1.20.2
./configure
make && make install
回到 local 目录,就能看见一个 nginx 文件夹
cd nginx/sbin
./nginx 此时nginx应该就启动了。
ps -ef | grep nginx 查看相关进程,会出来几条进程信息
此时需要关闭防火墙或者开放nginx的端口允许被访问,因为默认不准访问端口,并且防火墙是开启的。【CentOS6和7操作防火墙命令不一样】
开放访问端口
firewall-cmd --add-port=nginx 在centos上占用的端口/tcp --permanent
firewall-cmd --add-port=nginx 在centos上占用的端口/udp --permanent
firewall-cmd --add-service=http –permanent
firewall-cmd --add-port=80/tcp --permanent 端口默认是80 ,可以自己去nginx/conf/nginx.conf 文件中查看,它里面写的就是80
firewall-cmd --reload
关闭防火墙
systemctl stop firewalld.service 关闭防火墙服务
systemctl disable firewalld.service,开机禁止防火墙服务
systemctl enable firewalld.service,开机启动防火墙服务
systemctl status firewalld.service 查看防火墙状态
上面那两种方式二选一
然后刷新防火墙服务
firewall-cmd --reload
如果你选的是开放端口,重启防火墙后,使用命令 firewall-cmd --list-all 就能查看有哪些端口暴露了
如果你选的是关闭防火墙,那请再加一个永久的期限,你再使用命令 firewall-cmd --list-all 查看端口就提示你 防火墙没在运行
然后就能在浏览器里面通过 ip 地址端口号访问nginx
http://你centos的ip地址/ 回车就能看见欢迎页面。【ip 地址命令 ifconfig
】
至此就算是安装好了
3.2、Nginx 常用命令
使用 nginx 命令要进入到nginx安装目录下【cd /usr/local/nginx/sbin
】,在这里执行一些 nginx 命令
1、查看 nginx 版本号
./nginx -v
2、启动 nginx
./nginx
3、关闭 nginx
./nginx -s stop
ps -ef | grep nginx 查看nginx相关进程,就能看见只剩一个了
4、重新加载 nginx
./nginx -s reload
这个命令用来重新加载 nginx/conf 目录下的 nginx 配置文件
3.3、Nginx 配置
1、配置文件的位置
/etc/local/nginx/conf
root 用户
cd 空格
cd /etc/local/nginx/conf
ls
里面的 nginx.conf 文件就是 nginx 配置文件
vim nginx.conf
2、配置的组成
配置文件中包含三部分组成:
-
全局块:配置服务器整体运行的配置指令
从配置文件开始到 events 块之间的内容,主要会设置一些影响 nginx 服务器整体运行的配置指令,主要包括配 置运行 Nginx 服务器的用户(组)、允许生成的 worker process 数,进程 PID 存放路径、日志存放路径和类型以 及配置文件的引入等。
-
events 块:影响 nginx 服务器与用户的网络连接
events 块涉及的指令主要影响 Nginx 服务器与用户的网络连接,常用的设置包括
是否开启对多 work process 下的网络连接进行序列化,
是否允许同时接收多个网络连接,
选取哪种事件驱动模型来处理连接请求,
每个 word process 可以同时支持的最大连接数等。
这部分的配置对 Nginx 的性能影响较大,在实际中应该灵活配置
-
http 块
这算是 Nginx 服务器配置中最频繁的部分,代理、缓存和日志定义等绝大多数功能和第三方模块的配置都在这里。 需要注意的是:http 块也可以包括 http 全局块、server 块。
-
http 全局块
http 全局块配置的指令包括文件引入、MIME-TYPE 定义、日志自定义、连接超时时间、单链接请求数上限等
-
server 块 后面主要配置这一块
这块和虚拟主机有密切关系,虚拟主机从用户角度看,和一台独立的硬件主机是完全一样的,该技术的产生是为了 节省互联网服务器硬件成本。 每个 http 块可以包括多个 server 块,而每个 server 块就相当于一个虚拟主机。 而每个 server 块也分为全局 server 块,以及可以同时包含多个 locaton 块。
-
全局 server 块
最常见的配置是本虚拟机主机的监听配置和本虚拟主机的名称或 IP 配置
-
location 块
一个 server 块可以配置多个 location 块。 这块的主要作用是基于 Nginx 服务器接收到的请求字符串(例如 server_name/uri-string),对虚拟主机名称 (也可以是 IP 别名)之外的字符串(例如 前面的 /uri-string)进行匹配,对特定的请求进行处理。地址定向、数据缓存和应答控制等功能,还有许多第三方模块的配置也在这里进行。
-
-
#user nobody;
worker_processes 1;
# 处理并发数的配置,当前最大并发数是1.这是 Nginx 服务器并发处理服务的关键配置,worker_processes 值越大,可以支持的并发处理量也越多,但是会受到硬件、软件等设备的制约
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
# 上面那一堆就是 全局块 。
# events 后面大括号里的就是 events 块
events {
# 工作最大连接数,下面就表示每个 work_process 支持的最大连接数为 1024.
worker_connections 1024;
}
# http 打括号里面的内容就是 http 块配置
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 on;
# 这里server后面打括号里面的内容就是 server 块
server {
# 监听端口
listen 80;
# 主机名
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# location 块
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}