演示环境声明:CentOS 7.6
一、yum安装与卸载
- 安装
yum -y install nginx
systemctl start nginx
# 访问主机ip,无法访问请检查下列项目:
# 服务是否启动成功
systemctl status nginx
# 防火墙是否关闭
systemctl staus firewalld
# 防火墙端口是否放开
firewall-cmd --list-all
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload
# 使用云服务,检查安全组,云防火墙等
- 卸载
yum -y remove nginx
安装的时候安装了哪些依赖包:
centos-indexhtml.noarch 网站首页
centos-logos.noarch nginx日志切割
gperftools-libs.x86_64
nginx-filesystem.noarch
openssl11-libs.x86_64 openssl库
二、定制安装与卸载
知识储备:
- yum安装中重要的文件夹和文件
rpm -ql nginx
/etc/nginx # nginx默认配置文件目录
/usr/bin/nginx-upgrade #
/usr/lib/systemd/system/nginx.service # systemd服务管理文件
/usr/lib64/nginx/modules #依赖模块
/usr/share/doc/ # 说明文档
/usr/share/man/ # man帮助手册
/usr/share/nginx/html/ # 默认站点根目录
/usr/share/vim/ # vim 配置文件,用于vim编辑器的语法高亮等
/var/lib/nginx # 依赖库
/var/log/nginx # 默认日志目录
/var/log/nginx/access.log # 默认访问日志文件
/var/log/nginx/error.log # 默认错误日志文件
- 定制什么
常见的定制需求如下
- 生产环境中,所有安装的软件都不在系统磁盘
- 按照内部规范,配置文件、日志、依赖包等放在统一的指定目录
- 假设需求如下:
- 非系统磁盘挂载点为/data
- 配置文件存放在/data/config/nginx,日志存放在/data/logs/nginx/,二进制运行文件存在/data/software/nginx/,源码包存放在/data/setup目录
- 使用systemd进行服务管理
- 定制的三种常用方法:
- 在nginx启动时,使用选项指定,或者在修改配置文件,例如日志路径等
- 重新编译nginx,在编译时指定选项,可以修改默认配置文件路径、日志路径等
- 一劳永逸,打包rpm包
编译安装
- 下载源码包 http://nginx.org/en/download.html
cd /data/setup # 此处目录为上方假设需求
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar xvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
- 安装编译所需依赖包
yum -y install cmake # 编译工具
yum -y install gcc gcc-c++
yum -y install zlib zlib-devel openssl openssl-devel pcre-devel
- 编译:
./configure --prefix=/data/software/nginx \
--config-path=/data/config/nginx/nginx.conf \
--pid-path=/var/run/nginx.pid \
--user=www --group=www
# --pid-path指定的pid文件路径需要与systemd服务文件中定义的pid文件对应
# pid位置、用户、用户组选项均可以在配置文件中定义
# 上述编译选项不支持https,请添加--with-http_ssl_module选项
Tip:
1、上述安装的依赖为常用系统依赖,编译需要的依赖包可能因指定的编译选项不同而不同,在变异过程中,如果提示缺少某个模块,可以yum search 模块名,安装模块包后再尝试或者直接使用搜索引擎搜索报错语句以确认需要安装什么模块
2、假如编译后,需要额外加入其他模块重新编译,又忘记了原先的编译选项,可以使用/data/software/nginx/nginx -V查看(此处路径为nginx二进制文件路径)
3、常用编译选项见文章末尾
- 编写systemd 启动文件
[Unit]
Description=The Nginx HTTP and reverse proxy proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile= /var/run/nginx.pid
ExecStartPre=/usr/bin/rm -f /var/run/nginx.pid
ExecStart=/data/software/nginx/sbin/nginx
ExecReload=/data/software/nginx/sbin/nginx -s reload
ExecStop=/data/software/nginx/sbin/nginx -s stop
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
Restart=on-failure
[Install]
WantedBy=multi-user.target
打包rpm包
writing…
三、附录
- Nginx常用编译选项
# 下列选项中,--without开头的选项,若不指定,则默认添加对应模块,--with开头的选项,若不指定,则默认不添加
./configure --help
--help 打印此消息
--prefix=PATH 设置安装前缀,即主目录
--sbin-path=PATH 设置 nginx 二进制路径名
--modules-path=PATH 设置模块路径
--conf-path=PATH 设置 nginx.conf 路径名
--error-log-path=PATH 设置错误日志路径名
--pid-path=PATH 设置 nginx.pid 路径名
--lock-path=PATH 设置 nginx.lock 路径名
--user=USER 设置非特权用户工作进程
--group=GROUP 设置非特权组工作进程
--build=NAME 设置构建名称
--builddir=DIR 设置构建目录
--with-select_module 启用选择模块
--without-select_module 禁用选择模块
--with-poll_module 启用轮询模块
--without-poll_module 禁用轮询模块
--with-threads 启用线程池支持
--with-file-aio 启用文件 AIO 支持
--with-http_ssl_module 启用 ngx_http_ssl_module
--with-http_v2_module 启用 ngx_http_v2_module
--with-http_realip_module 启用 ngx_http_realip_module
--with-http_addition_module 启用 ngx_http_addition_module
--with-http_xslt_module 启用 ngx_http_xslt_module
--with-http_xslt_module=dynamic 启用动态 ngx_http_xslt_module
--with-http_image_filter_module 启用 ngx_http_image_filter_module
--with-http_image_filter_module=dynamic 启用动态 ngx_http_image_filter_module
--with-http_geoip_module 启用 ngx_http_geoip_module
--with-http_geoip_module=dynamic 启用动态 ngx_http_geoip_module
--with-http_sub_module 启用 ngx_http_sub_module
--with-http_dav_module 启用 ngx_http_dav_module
--with-http_flv_module 启用 ngx_http_flv_module
--with-http_mp4_module 启用 ngx_http_mp4_module
--with-http_gunzip_module 启用 ngx_http_gunzip_module
--with-http_gzip_static_module 启用 ngx_http_gzip_static_module
--with-http_auth_request_module 启用 ngx_http_auth_request_module
--with-http_random_index_module 启用 ngx_http_random_index_module
--with-http_secure_link_module 启用 ngx_http_secure_link_module
--with-http_degradation_module 启用 ngx_http_degradation_module
--with-http_slice_module 启用 ngx_http_slice_module
--with-http_stub_status_module 启用 ngx_http_stub_status_module
--without-http_charset_module 禁用 ngx_http_charset_module
--without-http_gzip_module 禁用 ngx_http_gzip_module
--without-http_ssi_module 禁用 ngx_http_ssi_module
--without-http_userid_module 禁用 ngx_http_userid_module
--without-http_access_module 禁用 ngx_http_access_module
--without-http_auth_basic_module 禁用 ngx_http_auth_basic_module
--without-http_mirror_module 禁用 ngx_http_mirror_module
--without-http_autoindex_module 禁用 ngx_http_autoindex_module
--without-http_geo_module 禁用 ngx_http_geo_module
--without-http_map_module 禁用 ngx_http_map_module
--without-http_split_clients_module 禁用 ngx_http_split_clients_module
--without-http_referer_module 禁用 ngx_http_referer_module
--without-http_rewrite_module 禁用 ngx_http_rewrite_module
--without-http_proxy_module 禁用 ngx_http_proxy_module
--without-http_fastcgi_module 禁用 ngx_http_fastcgi_module
--without-http_uwsgi_module 禁用 ngx_http_uwsgi_module
--without-http_scgi_module 禁用 ngx_http_scgi_module
--without-http_grpc_module 禁用 ngx_http_grpc_module
--without-http_memcached_module 禁用 ngx_http_memcached_module
--without-http_limit_conn_module 禁用 ngx_http_limit_conn_module
--without-http_limit_req_module 禁用 ngx_http_limit_req_module
--without-http_empty_gif_module 禁用 ngx_http_empty_gif_module
--without-http_browser_module 禁用 ngx_http_browser_module
--without-http_upstream_hash_module 禁用 ngx_http_upstream_hash_module
--without-http_upstream_ip_hash_module 禁用 ngx_http_upstream_ip_hash_module
--without-http_upstream_least_conn_module 禁用 ngx_http_upstream_least_conn_module
--without-http_upstream_random_module 禁用 ngx_http_upstream_random_module
--without-http_upstream_keepalive_module 禁用 ngx_http_upstream_keepalive_module
--without-http_upstream_zone_module 禁用 ngx_http_upstream_keepalive_module