CentOS-8x+Linux-8中源码编译安装Nginx-1.20*详解版
=目录索引=
01)系统 &环境
运行环境:实验室虚拟环境 系统型号:CtenOS-8.4-x86_64 Linux版本:linux-8-x86_64 CPU&内存:4H + 4GB 系统内核:4.18.0-305.3.1.el8.x86_64
=主题内容=
02)安装前准备
- 1 更新
CentOS-8 软件包 :
dnf update
- 2 关闭系统防火墙:
systemctl stop firewalld
- 3 关闭防火墙开机自启动:
systemctl disable firewalld
- 4 关闭
SELINUX :
sed -ri '/^SELINUX=/c\SELINUX=disabled' /etc/selinux/config
setenforce 0
03)编译器 &【dev】库安装
- 1 安装
make & wget :
yum -y install gcc automake autoconf libtool make wget
- 2 安装
gcc 编译器:
gcc --version
gcc (GCC) 10.2.1 20201112 (Red Hat 10.2.1-8)
yum -y install gcc gcc-c++
- 2 安装
OpenSSL 开发库:
ssl 模块,服务器使用 SSL 安全证书需要,【–http_ssl_module】用于开启 https 安全加密功能,依赖 OpenSSL 开发库 :
yum -y install openssl openssl-devel
- 3 安装
PCRE 正则式表达库:
nginx 必装模块【–http_rewrite_module】,基于正则匹配来实现重定向,依赖PCRE 库 :
yum -y install pcre pcre-devel
- 4 安装
zlib 压缩库:
nginx 必装模块【–http_gzip_module】,用于针对 HTTP 包中内容进行 gzip 格式的压缩,依赖 zlib 压缩库:
yum -y install zlib zlib-devel
04)编译安装【Nginx】
- 1 创建
nginxs 用户 & 用户属组:
useradd -s /sbin/nologin -M nginx
- 2 官网下载
nginx 最新稳定版:
mkdir lanmpp
cd /root/lanmpp
wget http://nginx.org/download/nginx-1.20.1.tar.gz
- 3 解压 & 删除
nginx 压缩包:
tar -xvf nginx-1.20.1.tar.gz
rm -rf nginx-1.20.1.tar.gz
- 4 预编译 & 配置
nginx 源码,使之生成可执行的二进制文件:
cd nginx-1.20.1/
./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--error-log-path=/usr/local/nginx/logs/error.log \
--http-log-path=/usr/local/nginx/logs/access.log \
--pid-path=/usr/local/nginx/logs/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--user=nginx \
--group=nginx \
--with-pcre \
--with-file-aio \
--with-threads \
--with-mail \
--with-http_ssl_module \
--with-mail_ssl_module \
--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-http_stub_status_module \
--with-http_sub_module \
--with-http_v2_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--without-mail_pop3_module \
--without-mail_imap_module \
--without-mail_smtp_module
Configuration summary
+ using threads
+ using system PCRE library
+ using system OpenSSL library
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx"
nginx configuration file: "/usr/local/nginx/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "/var/cache/nginx/client_temp"
nginx http proxy temporary files: "/var/cache/nginx/proxy_temp"
nginx http fastcgi temporary files: "/var/cache/nginx/fastcgi_temp"
nginx http uwsgi temporary files: "/var/cache/nginx/uwsgi_temp"
nginx http scgi temporary files: "/var/cache/nginx/scgi_temp"
- 5 开始编译 & 安装
nginx :
make && make install
05)配置【Nginx】
- 1 将
nginx 添加至系统环境变量中:
echo export PATH=$PATH:/usr/local/nginx/sbin >>/etc/profile
source /etc/profile
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
- 2 查看
nginx 安装结果 & 版本号:
nginx -v
nginx version: nginx/1.20.1
nginx -V
nginx version: nginx/1.20.1
built by gcc 10.2.1 20201112 (Red Hat 10.2.1-8) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments:
- 3 修改
nginx 运行端口:
vim /usr/local/nginx/nginx.conf
listen 80;
server_name localhost;
listen 8088;
server_name mhy.com;
- 4 安装
iptables 防火墙:
yum install iptables-services
vim /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8088 -j ACCEPT
systemctl restart iptables.service
systemctl enable iptables.service
/usr/libexec/iptables/iptables.init restart
- 5 查看 & 确认是否开启【8088】监听端口:
netstat -tnl
tcp 0 0 127.0.0.1:4330 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:8088 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:44321 0.0.0.0:* LISTEN
tcp6 0 0 :::33060 :::* LISTEN
tcp6 0 0 :::3306 :::* LISTEN
tcp6 0 0 ::1:4330 :::* LISTEN
tcp6 0 0 :::111 :::* LISTEN
tcp6 0 0 :::80 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 ::1:631 :::* LISTEN
tcp6 0 0 ::1:44321 :::* LISTEN
06)启动【Nginx】
/usr/local/nginx/sbin/nginx
nginx: [emerg] mkdir() "/var/cache/nginx/client_temp" failed (2: No such file or directory)
mkdir -p /var/cache/nginx/client_temp
/usr/local/nginx/sbin/nginx
ps aux | grep nginx
========================================================================================================
root 9253 0.0 0.0 42604 860 ? Ss 02:14 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 9254 0.0 0.1 77336 5012 ? S 02:14 0:00 nginx: worker process
root 11840 0.0 0.0 12352 1132 pts/0 S+ 02:58 0:00 grep --color=auto nginx
07)【Nginx】启动 & 停止和重启服务
- 1 启动代码&路径格式:
nginx 安装目录路径+ -c nginx 配置文件 nginx.conf 的路径,示例如下:
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/sbin/nginx
- 2 停止
nginx 服务:
/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx -s quit
ps -ef|grep nginx
root 9253 1 0 02:14 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 12594 9253 0 03:19 ? 00:00:00 nginx: worker process
root 12761 8788 0 03:23 pts/0 00:00:00 grep --color=auto nginx
kill -QUIT 9235
kill -TERM 9235
kill -INT 9235
pkill -9 nginx
- 3 重启
Nginx 服务:
===方法-1===
nginx -t
===方法-2===
cd /usr/local/nginx/sbin
./nginx -t
===方法-3===
/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/nginx.conf
=================================打印信息==================================
nginx: the configuration file /usr/local/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/nginx.conf test is successful
=================================简明释文==================================
Nginx:配置文件/usr/local/nginx/nginx.conf
Nginx:配置文件/usr/local/nginx/nginx.conf test is successful
cd /usr/local/nginx/sbin
./nginx -s reload
/usr/local/nginx/sbin/nginx -s reload
kill -HUP 9235
08)设置【Nginx】开机自动启动
- 1 创建 & 配置
nginx 开机自启动脚本文件:
vim /etc/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: NGINX is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
chmod a+x /etc/init.d/nginx
- 2 将
nginx 服务加入chkconfig 系统管理序列中…
chkconfig --add /etc/init.d/nginx
chkconfig nginx on
chkconfig --list nginx
- 3 执行启动
nginx 服务脚本:
systemctl start nginx
- 4 终端命令行测试:
curl localhost:8088
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
09)【Nginx】 重定向常用启停命令
systemctl start nginx
systemctl status nginx
● nginx.service - SYSV: NGINX is an HTTP(S) server, HTTP(S) reverse proxy and IMAP>
Loaded: loaded (/etc/rc.d/init.d/nginx; generated)
Active: active (running) since Sun 2021-08-08 04:50:18 CST; 19min ago
Docs: man:systemd-sysv-generator(8)
Process: 17347 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=0/SUC>
Main PID: 9253 (nginx)
Tasks: 0 (limit: 23376)
Memory: 276.0K
CGroup: /system.slice/nginx.service
? 9253 nginx: master process /usr/local/nginx/sbin/nginx
systemctl stop nginx
nginx -s reload
nginx -t
10)【Nginx】 配置优化
vim /usr/local/nginx/nginx.conf
user nginx nginx;
worker_processes auto;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
multi_accept on;
}
include /etc/nginx
11)浏览器访问 & 测试【Nginx】安装结果
学如人生,砥砺前行… 互勉互励,志之所趋…
|