nginx安装部署详细手册

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


一、nginx安装

1.下载安装包

官网下载地址:http://nginx.org/download/,选择好版本后可以直接通过命令下载

cd /opt/nginx
wget http://nginx.org/download/nginx-1.21.6.tar.gz
tar -zxvf nginx-1.21.6.tar.gz

2.安装编译工具和库文件

yum -y install gcc‐c++ pcre pcre‐devel zlib zlib‐devel openssl openssl‐devel

3.编译安装

cd /opt/nginx/nginx-1.21.6
#构建并添加常用模块,可根据个人需要取舍
./configure --prefix=/opt/nginx/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-openssl=/opt/openssl-1.1.1t --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'
##如需集成lua,需要单独添加 --add-module=/opt/nginx/lua-nginx-module-0.10.22  --add-module=/opt/nginx/ngx_devel_kit-0.3.2
make -j2 && make install

4.启动nginx

cd /opt/nginx/nginx/sbin
./nginx

5.访问首页

访问主机ip:例如192.168.19.101
在这里插入图片描述

6.开机自启

vi /lib/systemd/system/nginx.service

编辑nginx.service文件,加入如下内容

[Unit]
Description=nginx service
After=network.target
 
[Service]
Type=forking
ExecStart=/opt/nginx/nginx/sbin/nginx
ExecReload=/opt/nginx/nginx/sbin/nginx -s reload
ExecStop=/opt/nginx/nginx/sbin/nginx -s stop
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target
#加入开机自启动
systemctl enable nginx.service
#取消开机自启动
systemctl disable nginx.service

二、nginx常用配置

1.日志格式化

修改nginx.conf,在http模块内添加

log_format main '客户端地址: $remote_addr 客户端用户名称: $remote_user 访问时间和时区: $time_local 请求的URI和HTTP协议: $request 请求地址: $http_host  '  
					'HTTP请求状态: $status upstream状态: $upstream_status  发送给客户端文件内容大小: $body_bytes_sent  url跳转来源: $http_referer'  
					'用户终端浏览器等信息: $http_user_agent SSL协议版本: $ssl_protocol  交换数据中的算法: $ssl_cipher 后台upstream的地址,即真正提供服务的主机地址: $upstream_addr'    
					'整个请求的总时间: $request_time  请求过程中,upstream响应时间: $upstream_response_time'

在这里插入图片描述
在server模块内定义日志路径

access_log  logs/access.log  main;
		#error_log  logs/error.log  debug;
		error_log  logs/error_log error;

在这里插入图片描述

通过ngx.log打印的日志都在error_log定义的文件中打印,可根据日志级别来控制是否显示打印内容,一般来说,调试代码使用debug级别,生产环境使用error级别


结尾

  • 感谢大家的耐心阅读,如有建议请私信或评论留言。
  • 如有收获,劳烦支持,关注、点赞、评论、收藏均可,博主会经常更新,与大家共同进步

猜你喜欢

转载自blog.csdn.net/qq359605040/article/details/129219672