写在前面:本次测试环境是linux 7.4 (redhat,centos,oracle linux).linux6也行,只是服务管理方式不同而已.
本次教程的版本: https://nginx.org/download/nginx-1.9.9.tar.gz
生产环境不建议用这么新的,其实也不新了,都2015年的了.....
note: name-devel的多数都是编译所需要的依赖,做成rpm包之后,可以不依赖了.
yum install -y make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre
ls nginx-1.9.9.tar.gz
tar -xvf nginx-1.9.9.tar.gz
cd nginx-1.9.9
./configure --prefix=/u01/nginx --with-pcre --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module
make && make install
部分选项解释:
--prefix=/u01/nginx 表示安装路径为/u01/nginx 这是个人习惯,也可以换成其它路径,比如/usr/local/nginx
--with-http_ssl_module 表示要用ssl模块,现在很多环境都要求是https的,所以建议带上ssl模块
具体的选项可以看看官网的 http://nginx.org/en/docs/configure.html
也有中文的:https://www.nginx.cn/doc/
/u01/nginx/sbin/nginx -v # 查看版本
/u01/nginx/sbin/nginx # 启动nginx
/u01/nginx/sbin/nginx -s reload # 重新载入配置文件
/u01/nginx/sbin/nginx -s reopen # 重启 Nginx
/u01/nginx/sbin/nginx -s stop # 停止 Nginx
这是linux7的,linux6的有点区别,区别在于linux6是用service来管理,是这样的文件/etc/rc.d/init.d/nginx ,你可以参考4.3nginx.spec配置文件里面怎么写的.
cat << EOF > /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx by ddcw
After=network.target
[Service]
Type=forking
PIDFile=/u01/nginx/logs/nginx.pid
ExecStart=/u01/nginx/sbin/nginx
ExecReload=/u01/nginx/sbin/nginx -s reload
ExecStop=/u01/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.targe
EOF
测试:
systemctl start nginx #启动
systemctl stop nginx #停止
systemctl status nginx #查看状态
systemctl restart nginx #重启
这样下次安装的时候方便一点,这个也是个人习惯的,你也可以写成shell脚本,这样还能适合其它nginx版本和操作系统版本.制作成rpm之后,受限与nginx版本和操作系统版本(其实也就是一些库)
yum install -y rpm-build
rpmbuild .
结构如下
14:55:46 [root@ddcw ~]#tree rpmbuild/
rpmbuild/
|-- BUILD
|-- BUILDROOT
|-- RPMS
|-- SOURCES
|-- SPECS
`-- SRPMS
这个我不多讲,后面有空了再说.
rpmbuild/SPECS 是放name.spec文件的,你可以把这个文件理解为配置文件.
rpmbuild/SOURCES/ 是放源码包的
rpmbuild/BUILD 是配置编译等的目录,有个对应的变量的
rpmbuild/RPMS 是生成的RPM包的路径
你可以看看IBM或者红帽官网的具体教程,https://www.ibm.com/developerworks/cn/linux/management/package/rpm/part3/
nginx.spec内容可以参考如下(linux 6或者7通用的):安装路径是/u01/nginx,你可以换成其它的,nginx服务名是nginxDDCW,你也可以换成其它的.默认端口设置的是8888端口,你可以换成其它的端口.
Name: nginx
Version: 1.9.9
Release: 1%{?dist}
Summary: nginx1.9.9 by ddcw
Group: Applications/System
License: ddcw
URL: https://github.com/ddcw/nginx
Source0: %{name}-%{version}.tar.gz
#BuildRequires: make
#Requires: make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre
Requires: zlib zlib-devel openssl openssl-devel pcre
%define _prefix /u01/nginx
%description
this is for install nginx in oel74
%prep
%setup -q
%build
./configure --prefix=%{_prefix} --with-pcre --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module
make
%install
make install DESTDIR=%{buildroot}
#make install
%post
export os_v="6"
systemctl -h >/dev/null 2>&1 && export os_v="7"
sed -i 's/ 80\;/ 8888\;/' %{_prefix}/conf/nginx.conf
cat << EOF > %{_prefix}/html/index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx by DDCW!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<h1>Successful</h1>
<p>
you can visit https://github.com/ddcw/nginx to known more
<a href="https://github.com/ddcw/nginx">https://github.com/ddcw/nginx</a>.</p>
</body>
</html>
EOF
if [ "${os_v}" -eq "7" ]
then
cat << EOF > /usr/lib/systemd/system/nginxDDCW.service
[Unit]
Description=nginx by ddcw
After=network.target
[Service]
Type=forking
PIDFile=%{_prefix}/logs/nginx.pid
ExecStart=%{_prefix}/sbin/nginx
ExecReload=%{_prefix}/sbin/nginx -s reload
ExecStop=%{_prefix}/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
chmod 754 /usr/lib/systemd/system/nginxDDCW.service
systemctl daemon-reload
systemctl enable nginxDDCW
systemctl restart nginxDDCW
echo -e "[\033[32;40mINFO\033[0m `date +%Y%m%d-%H:%M:%S`] you can run \033[32;40m systemctl start nginxDDCW \033[0m to start nginx"
echo -e "[\033[32;40mINFO\033[0m `date +%Y%m%d-%H:%M:%S`] you can run \033[32;40m systemctl stop nginxDDCW \033[0m to stop nginx"
echo -e "[\033[32;40mINFO\033[0m `date +%Y%m%d-%H:%M:%S`] you can run \033[32;40m systemctl restart nginxDDCW \033[0m to restart nginx status"
echo -e "[\033[32;40mINFO\033[0m `date +%Y%m%d-%H:%M:%S`] you can run \033[32;40m systemctl reload nginxDDCW \033[0m to reload nginx"
echo -e "[\033[32;40mINFO\033[0m `date +%Y%m%d-%H:%M:%S`] you can run \033[32;40m systemctl status nginxDDCW \033[0m to known nginx status"
fi
if [ "${os_v}" -eq "6" ]
then
cat << EOF > /etc/rc.d/init.d/nginxDDCW
#chkconfig: 2345 80 05
#description: nginx 1.9.9 by ddcw
case \$1 in
start)
%{_prefix}/sbin/nginx
;;
stop)
%{_prefix}/sbin/nginx -s stop
;;
restart)
service nginxDDCW stop
service nginxDDCW start
;;
reload)
%{_prefix}/sbin/nginx -s reload
;;
status)
ps -ef |grep nginx | grep -v grep | grep -v status | grep --color nginx
;;
*)
;;
esac
EOF
chmod +x /etc/rc.d/init.d/nginxDDCW
service nginxDDCW restart 2>/dev/null
echo -e "[\033[32;40mINFO\033[0m `date +%Y%m%d-%H:%M:%S`] you can run \033[32;40m service nginxDDCW start \033[0m to start nginx"
echo -e "[\033[32;40mINFO\033[0m `date +%Y%m%d-%H:%M:%S`] you can run \033[32;40m service nginxDDCW stop \033[0m to stop nginx"
echo -e "[\033[32;40mINFO\033[0m `date +%Y%m%d-%H:%M:%S`] you can run \033[32;40m service nginxDDCW restart\033[0m to restart nginx"
echo -e "[\033[32;40mINFO\033[0m `date +%Y%m%d-%H:%M:%S`] you can run \033[32;40m service nginxDDCW reolad \033[0m to reload nginx"
echo -e "[\033[32;40mINFO\033[0m `date +%Y%m%d-%H:%M:%S`] you can run \033[32;40m service nginxDDCW status \033[0m to known nginx status"
fi
%files
%doc
%{_prefix}
%postun
#systemctl -h >/dev/null 2>&1 && cat %{_prefix}/logs/nginx.pid | xargs -t -i kill -9 \{\} >/dev/null 2>&1 || ps -ef | grep nginx | grep -v grep| awk '{print $2}' | xargs -t -i kill -9 {}
cat %{_prefix}/logs/nginx.pid | xargs -t -i kill -9 \{\} >/dev/null 2>&1
#systemctl -h >/dev/null 2>&1 || ps -ef | grep nginx | grep -v grep| awk '{print $2}' | xargs -t -i kill -9 {}
rm -rf %{_prefix} 2>/dev/null
%changelog
#write at 20200427 by ddcw first
rpmbuild -bb rpmbuild/SPECS/nginxddcw.spec
查看自己打包的nginx rpm包
ll rpmbuild/RPMS/x86_64/nginx-1.9.9-1.el7.x86_64.rpm
yum install -y rpmbuild/RPMS/x86_64/nginx-1.9.9-1.el7.x86_64.rpm #个人习惯使用yum
访问测试呢:
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。