Nginx安装方法
法一、Nginx部署-yum安装
访问nginx 的官方网站:http://www.nginx.org/
配置安装环境:创建Nginx储存库
#vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
安装Nginx数据包
#yum -y install nginx
#nginx -v //查看下载版本号
关闭防火墙和selinux
[root@nginx-server ~]# getenforce
Enforcing
[root@nginx-server ~]# setenforce 0
[root@nginx-server ~]# systemctl stop firewalld
[root@nginx-server ~]# systemctl disable firewalld
启动并设置开机自启
#systemctl start nginx
#systemctl enable nginc
在浏览器输入ip访问测试
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4xUqtyKJ-1620187002164)(file://D:\学习文件\Nginx Web服务器\assets\1561536791743.png?lastModify=1620178424)]
法二、Nginx编译安装与配置使用
1、安装编译环境
yum -y install gcc gcc-c++
2、安装pcre软件包(使nginx支持http rewrite模块)
yum -y install pcre pcre-devel
3、安装openssl-devel(使nginx支持ssl)
yum -y openssl openssl-devel
4、安装zlib
yum -y install zlib zlib-devel
5、创建用户nginx
useradd nginx
passwd nginx
6、安装nginx
# wget http://nginx.org/download/nginx-1.16.0.tar.gz
# tar xzfv nginx-1.16.0.tar.gz -C /usr/local/
# cd /usr/local/nginx-1.16.0/
[root@localhost nginx-1.16.0]# make && make install
7、Nginx编译参数
# /usr/local/nginx/sbin/nginx -v //查看nginx安装的模块
8、修改配置文件/etc/nginx/nginx.conf
#全局参数值
user nginx; //指定用户
worker_processes 4; //设置nginx启动进程数量,一般设置成与逻辑CPU数量相同
error_log logs/error.log; //指定错误日志
worker_rlimit_nofile 10240; //设置一个nginx进程能打开的最大数量
pid /var/run/nginx.pid;
events {
worker_connections 10240; //设置一个进程的最大并发连接数
}
#httpp 服务相关设置
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 /var/log/nginx/access.log main; #设置访问日志的位置和格式
sendfile on; #是否调用sendfile函数输出文件,一般设置为on,若nginx是用来进行磁盘IO负载应用时,可以设置为off,降低系统负载
gzip on; #是否开启gzip压缩,将注释去掉开启
keepalive_timeout 65; #设置长连接的超时时间
# 虚拟服务器的相关设置
server {
listen 80; #设置监听的端口
server_name localhost; #设置绑定的主机名、域名或ip地址
# charset koi8-r; # 设置编码字符
charset utf-8;
location / {
root /var/www/nginx; #设置服务器默认网站的根目录位置,需要手动创建
index index.html index.htm; #设置默认打开的文档
}
error_page 500 502 503 504 /50x.html; #设置错误信息返回页面
location = /50x.html {
root html; #这里的绝对位置是/usr/local/nginx/html
}
}
}
nginx.conf的组成:nginx.conf一共由三部分组成,分别为:全局块、events块、http块。在http块中又包含http全局块、多个server块。每个server块中又包含server全局块以及多个location块。在统一配置块中嵌套的配置快,各个之间不存在次序关系。
9、检测Nginx配置文件是否正确
# /usr/local/nginx/sbin/nginx -t
# /mkdir -p /tmp/nginx
# /mkdir /usr/local/nginx/logs
10、启动Nginx服务
# /usr/local/nginx/sbin/nginx
11、通过nginx命令控制Nginx服务
常用命令
nginx -c /path/nginx.conf # 以特定目录下的配置文件启动nginx:
nginx -s reload # 修改配置后重新加载生效
nginx -s stop # 快速停止nginx
nginx # 快速启动nginx
nginx -t # 测试当前配置文件是否正确
nginx -t -c /path/to/nginx.conf # 测试特定的nginx配置文件是否正确
添加权限
# chmod +x /etc/init.d/nginx
重新加载系统启动文件
# systemctl daemon-relocad
启动并设置开机自启
# systemctl start nginx