友情参考:
编译安装LNMP之PHP
https://blog.csdn.net/liuyuhui_gdtyj/article/details/92012312
编译安装LNMP之MySQL
https://blog.csdn.net/liuyuhui_gdtyj/article/details/92011128
系统环境:
CentOS7.6最小安装
Nginx需要依赖下面3个包
- gzip 模块需要 zlib 库 ( 下载: http://www.zlib.net/ ) zlib-1.2.11.tar.gz
- rewrite 模块需要 pcre 库 ( 下载: http://www.pcre.org/ ) pcre-8.43.tar.gz
- ssl 功能需要 openssl 库 ( 下载: http://www.openssl.org/ ) openssl-1.1.1c
- zlib包和pcre包可直接yum install zlib-devel pcre-devel,openssl建议编译安装新版的,安全太重要了。
$ rpm -ivh http://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-7-11.noarch.rpm
$ yum -y install gcc-c++ gd-devel
$ cd ~ && wget http://www.zlib.net/zlib-1.2.11.tar.gz
$ tar zxf zlib-1.2.11.tar.gz -C /usr/local/src/
$ cd /usr/local/src/zlib-1.2.11
$ ./configure && make -j 2 && make install
$ cd ~ && wget https://jaist.dl.sourceforge.net/project/pcre/pcre/8.43/pcre-8.43.tar.gz
$ tar zxf pcre-8.43.tar.gz -C /usr/local/src/
$ cd /usr/local/src/pcre-8.43
$ ./configure && make -j 2 && make install
编译openssl要求perl5,官网上有安装步骤:
$ cd ~ && wget https://www.cpan.org/src/5.0/perl-5.30.0.tar.gz
$ tar -xzf perl-5.30.0.tar.gz -C /usr/local/src/
$ cd /usr/local/src/perl-5.30.0
$ ./Configure -des -Dprefix=/usr/local
$ make -j 2 && make install
$ perl -v
This is perl 5, version 30, subversion 0 (v5.30.0) built for x86_64-linux
$ cd ~ && wget https://www.openssl.org/source/openssl-1.1.1c.tar.gz
$ tar zxf openssl-1.1.1c.tar.gz -C /usr/local/src/
$ cd /usr/local/src/openssl-1.1.1c
$ ./config && make -j 2 && make install
$ echo ‘/usr/local/lib64’ >> /etc/ld.so.conf.d/local.conf
$ ldconfig -v | grep libssl
libssl.so.1.1 -> libssl.so.1.1
libssl3.so -> libssl3.so
libssl.so.10 -> libssl.so.1.0.2k
检查版本信息
$ openssl version -a
OpenSSL 1.1.1c 28 May 2019
…
OPENSSLDIR: “/usr/local/ssl”
ENGINESDIR: “/usr/local/lib64/engines-1.1”
Seeding source: os-specific
$ useradd -r -s /sbin/nologin nginx
$ wget http://nginx.org/download/nginx-1.16.0.tar.gz
$ tar zxf nginx-1.16.0.tar.gz -C /usr/local/src/
$ cd /usr/local/src/nginx-1.16.0
$ ./configure --prefix=/usr/local/nginx
–user=nginx --group=nginx
–with-openssl=/usr/local/src/openssl-1.1.1c
–with-pcre=/usr/local/src/pcre-8.43
–with-zlib=/usr/local/src/zlib-1.2.11
–with-http_stub_status_module
–with-http_ssl_module
–with-http_v2_module
–with-http_realip_module
–with-http_addition_module
–with-http_sub_module
–with-http_dav_module
–with-http_flv_module
–with-http_mp4_module
–with-http_gunzip_module
–with-http_gzip_static_module
–with-http_random_index_module
–with-http_secure_link_module
–with-http_auth_request_module
–with-http_image_filter_module
–with-http_slice_module
–with-mail
–with-threads
–with-file-aio
–with-stream
–with-mail_ssl_module
–with-stream_ssl_module
$ make -j 2 && make install
$ echo ‘PATH=$PATH:/usr/local/nginx/sbin’ >> /etc/profile
$ source /etc/profile
检查安装参数:
$ nginx -V
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.1.1c 28 May 2019
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx …
可以看到openssl版本正确。
配置开机启动,有两种方式
方式一:
$ vi /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 85 15
# description: nginx is a World Wide Web server. It is used to serve
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
[[ "$NETWORKING" = "no" ]] && exit 0
nginx=/usr/local/nginx/sbin/nginx
prog=$(basename $nginx)
NGINX_CONFIG_NAME="/usr/local/nginx/conf/nginx.conf"
LOCKFILE="/var/lock/nginx/nginx.lock"
configtest() {
$nginx -t
}
start() {
configtest
test -x $nginx || exit 5
test -f $NGINX_CONFIG_NAME || exit 6
mkdir -p /var/run/nginx
mkdir -p /var/lock/nginx
echo -n $"Starting $prog :"
daemon $nginx -c $NGINX_CONFIG_NAME
retval=$?
echo
test $retval -eq 0 && touch $LOCKFILE
return $retval
}
stop() {
echo "Stoping $prog :"
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $LOCKFILE
return $retval
}
restart() {
configtest || return $?
stop
sleep 3
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog :"
killproc $nginx -HUP
retval=$?
echo
}
force_reload() {
restart
}
rt_status() {
status $prog
[ $? -eq 0 ] && echo -n `configtest`
}
case $1 in
status)
rt_status
;;
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload)
reload
;;
force_reload)
force_reload
;;
*)
echo "Usage:$prog {start|stop|status|reload|force_reload|restart}"
exit 1
;;
esac
// 赋予脚本执行权限
$ chmod +x /etc/init.d/nginx
// 设置开机自启
$ chkconfig --add nginx
$ chkconfig nginx on
// 启动nginx
$ systemctl start nginx
$ systemctl status nginx
方式二:
$ vi /usr/lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
开机启动服务
$ systemctl enable nginx.service
$ systemctl start nginx.service
参考:
https://blog.csdn.net/niuxitong/article/details/89610004