技術(shù)文檔
Debian 10.9.x編譯安裝Nginx1.20.x
準(zhǔn)備篇:
一、配置防火墻,開啟80端口、3306端口
Debian默認(rèn)沒有安裝任何防火墻的,我們這里推薦使用iptables防火墻。
1.1安裝iptables防火墻
whereis iptables #查看系統(tǒng)是否安裝防火墻
apt-get install iptables #運(yùn)行此命令安裝防火墻
mkdir /etc/sysconfig #創(chuàng)建防火墻配置文件存放目錄
touch /etc/sysconfig/iptables #創(chuàng)建防火墻配置文件
nano /etc/sysconfig/iptables #編輯添加防火墻規(guī)則
# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
ctrl+o #保存
ctrl+x #退出
/sbin/iptables-restore /etc/sysconfig/iptables #使防火墻規(guī)則生效
特別注意:
1、修改完防火墻規(guī)則文件/etc/sysconfig/iptables后,需要再次執(zhí)行
/sbin/iptables-restore /etc/sysconfig/iptables命令,防火墻規(guī)則才能生效。
2、系統(tǒng)重啟后,防火墻默認(rèn)不會(huì)開機(jī)啟動(dòng),需要再次執(zhí)行/sbin/iptables-restore /etc/sysconfig/iptables命令,防火墻規(guī)則才能生效。
3、如果要臨時(shí)關(guān)閉防火墻,需要清空/etc/sysconfig/iptables配置文件,再次執(zhí)行/sbin/iptables-restore /etc/sysconfig/iptables命令。
4、如果要再次開啟防火墻,需要恢復(fù)/etc/sysconfig/iptables配置文件,再次執(zhí)行/sbin/iptables-restore /etc/sysconfig/iptables命令。
1.2添加防火墻管理腳本
nano /etc/init.d/iptables #編輯添加腳本
#腳本中的IPTABLES_CONFIG=/etc/sysconfig/iptables是防火墻配置規(guī)則文件的路徑。
#!/bin/sh -e
### BEGIN INIT INFO
# Provides: iptables
# Required-Start: mountvirtfs ifupdown $local_fs
# Default-Start: S
# Default-Stop: 0 6
### END INIT INFO
# July 9, 2007
# James B. Crocker ubuntu@james.crocker.name
# Creative Commons Attribution - Share Alike 3.0 License (BY,SA)
# Script to load/unload/save iptables firewall settings.
PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"
IPTABLES=/sbin/iptables
IPTABLES_SAVE=/sbin/iptables-save
IPTABLES_RESTORE=/sbin/iptables-restore
IPTABLES_CONFIG=/etc/sysconfig/iptables
[ -x $IPTABLES ] || exit 0
. /lib/lsb/init-functions
case "$1" in
start)
log_action_begin_msg "Starting firewall"
type usplash_write /dev/null 2/dev/null usplash_write "TIMEOUT 120" || true
if $IPTABLES_RESTORE $IPTABLES_CONFIG ; then
log_action_end_msg $?
else
log_action_end_msg $?
fi
type usplash_write /dev/null 2/dev/null usplash_write "TIMEOUT 15" || true
;;
stop)
log_action_begin_msg "Saving current firewall configuration"
if $IPTABLES_SAVE $IPTABLES_CONFIG ; then
log_action_end_msg $?
else
log_action_end_msg $?
fi
log_action_begin_msg "Flushing ALL firewall rules from chains!"
if $IPTABLES -F ; then
log_action_end_msg $?
else
log_action_end_msg $?
fi
log_action_begin_msg "Deleting ALL firewall chains [Warning: ACCEPTING ALL PORT SERVICES!]"
if $IPTABLES -X ; then
$IPTABLES -P INPUT ACCEPT
$IPTABLES -P FORWARD ACCEPT
$IPTABLES -P OUTPUT ACCEPT
log_action_end_msg $?
else
log_action_end_msg $?
fi
;;
save)
log_action_begin_msg "Saving current firewall configuration"
if $IPTABLES_SAVE $IPTABLES_CONFIG ; then
log_action_end_msg $?
else
log_action_end_msg $?
fi
;;
force-reload|restart)
log_action_begin_msg "Reloading firewall configuration [Warning: POTENTIAL NETWORK INSECURITY DURING RELOAD]"
$IPTABLES -F
$IPTABLES -X
if $IPTABLES_RESTORE $IPTABLES_CONFIG ; then
log_action_end_msg $?
else
log_action_end_msg $?
fi
;;
*)
echo "Usage: /etc/init.d/iptables {start|stop|save|restart|force-reload}"
exit 1
;;
esac
exit 0
ctrl+o #保存
ctrl+x #退出
chmod +x /etc/init.d/iptables #添加執(zhí)行權(quán)限
update-rc.d iptables defaults 99 #添加服務(wù)
systemctl start iptables.service #啟動(dòng)
service iptables stop #停止
#現(xiàn)在就可以使用上面的命令管理防火墻了,啟動(dòng)、停止
#如果修改了防火墻配置規(guī)則,還是需要執(zhí)行/sbin/iptables-restore /etc/sysconfig/iptables命令使其生效,然后再使用防火墻管理腳本進(jìn)行管理
1.3設(shè)置防火墻開機(jī)啟動(dòng)
1.3.1使用系統(tǒng)啟動(dòng)腳本進(jìn)行設(shè)置
cp /lib/systemd/system/rc-local.service /lib/systemd/system/rc-local.service-bak #備份
ln -s /lib/systemd/system/rc-local.service /etc/systemd/system/ #創(chuàng)建軟連接文件
nano /lib/systemd/system/rc-local.service #添加[Install]段到最后
# SPDX-License-Identifier: LGPL-2.1+
#
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.local is executable.
[Unit]
Description=/etc/rc.local Compatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.local
After=network.target
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no
[Install]
WantedBy=multi-user.target
Alias=rc-local.service
ctrl+o #保存
ctrl+x #退出
nano /etc/rc.local #創(chuàng)建文件,添加防火墻啟動(dòng)命令
#!/bin/bash
/sbin/iptables-restore /etc/sysconfig/iptables
ctrl+o #保存
ctrl+x #退出
chmod +x /etc/rc.local #添加執(zhí)行權(quán)限
#重新啟動(dòng)系統(tǒng)進(jìn)行測(cè)試,現(xiàn)在防火墻已經(jīng)開機(jī)自啟動(dòng)了
1.3.2使用sysv-rc-conf服務(wù)設(shè)置開機(jī)啟動(dòng)
apt-get install sysv-rc-conf #安裝
cp /usr/sbin/sysv-rc-conf /usr/sbin/chkconfig #拷貝
sysv-rc-conf iptables on #設(shè)置開機(jī)啟動(dòng)
chkconfig iptables on
sysv-rc-conf #查看啟動(dòng)服務(wù)
#如果使用apt-get無法直接安裝sysv-rc-conf,則修改apt-get源
cp /etc/apt/sources.list /etc/apt/sources.list-bak #備份
nano /etc/apt/sources.list #編輯添加下面一行代碼
deb http://ftp.de.debian.org/debian sid main
ctrl+o #保存
ctrl+x #退出
apt-get update #更新軟件源索引
#重新啟動(dòng)系統(tǒng)進(jìn)行測(cè)試,現(xiàn)在防火墻已經(jīng)開機(jī)自啟動(dòng)了
Debian 10.9.x系統(tǒng)中默認(rèn)是沒有開啟SELINUX的,無需關(guān)閉。
二、系統(tǒng)約定
軟件源代碼包存放位置:/usr/local/src
源碼包編譯安裝位置:/usr/local/軟件名字
三、下載軟件包
1、下載nginx
http://nginx.org/download/nginx-1.20.1.tar.gz
2、下載MySQL
https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-boost-8.0.25.tar.gz #下載帶boost的安裝包
http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
#下載解壓版的mysql,編譯php5.2.x需要用到此版本的mysql驅(qū)動(dòng)
#由于mysql-8.0系列已經(jīng)去掉了對(duì)php5.2.x的支持,所以在安裝php5.2的時(shí)候我們要用到mysql-5.7的驅(qū)動(dòng)文件
3、下載php
http://mirrors.sohu.com/php/php-8.0.7.tar.gz
http://mirrors.sohu.com/php/php-7.4.20.tar.gz
http://mirrors.sohu.com/php/php-7.3.28.tar.gz
http://mirrors.sohu.com/php/php-7.2.34.tar.gz
http://mirrors.sohu.com/php/php-7.1.33.tar.gz
http://mirrors.sohu.com/php/php-7.0.33.tar.gz
http://mirrors.sohu.com/php/php-5.6.40.tar.gz
http://mirrors.sohu.com/php/php-5.5.38.tar.gz
http://mirrors.sohu.com/php/php-5.4.45.tar.gz
http://mirrors.sohu.com/php/php-5.3.29.tar.gz
http://museum.php.net/php5/php-5.2.17.tar.gz
https://php-fpm.org/downloads/php-5.2.17-fpm-0.5.14.diff.gz
4、下載cmake(MySQL編譯工具)
https://cmake.org/files/v3.20/cmake-3.20.2.tar.gz
5、rpcsvc-proto(編譯MySQL需要)
https://github.com/thkukuk/rpcsvc-proto/releases/download/v1.4.2/rpcsvc-proto-1.4.2.tar.xz
6、下載pcre (支持nginx偽靜態(tài))
http://ftp.pcre.org/pub/pcre/pcre-8.44.tar.gz
7、下載openssl(nginx擴(kuò)展)
7.1下載最新穩(wěn)定版本,適用于nginx擴(kuò)展https
https://www.openssl.org/source/openssl-1.1.1k.tar.gz
7.2下載舊版本,適用于php5.6.x及其以下版本編譯安裝openssl擴(kuò)展
https://www.openssl.org/source/old/1.0.2/openssl-1.0.2u.tar.gz
8、下載zlib(nginx擴(kuò)展)
http://www.zlib.net/zlib-1.2.11.tar.gz
9、下載libmcrypt(php擴(kuò)展)
https://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
10、下載yasm(php擴(kuò)展)
http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
11、t1lib(php擴(kuò)展)
http://download.freenas.org/distfiles/t1lib-5.1.2.tar.gz
12、下載gd庫安裝包
12.1適用于php 5.5.x及其以上版本
https://github.com/libgd/libgd/releases/download/gd-2.3.1/libgd-2.3.1.tar.gz
12.2適用于 php 5.4.x 5.3.x 5.2.x版本
https://jaist.dl.sourceforge.net/project/gd2/gd-2.0.35.tar.gz
13、libvpx(gd庫需要)
https://github.com/webmproject/libvpx/archive/v1.10.0/libvpx-1.10.0.tar.gz
14、tiff(gd庫需要)
http://download.osgeo.org/libtiff/tiff-4.0.7.tar.gz
15、libpng(gd庫需要)
ftp://ftp.simplesystems.org/pub/libpng/png/src/libpng16/libpng-1.6.37.tar.gz
16、freetype(gd庫需要)
https://download.savannah.gnu.org/releases/freetype/freetype-2.10.4.tar.gz
17、jpegsrc(gd庫需要)
http://distfiles.macports.org/jpeg/jpegsrc.v9d.tar.gz
18、Boost(編譯mysql需要,要與mysql版本相匹配)
https://dl.bintray.com/boostorg/release/1.73.0/source/boost_1_73_0.tar.gz
19、libzip(編譯php需要)
https://libzip.org/download/libzip-1.7.3.tar.gz
20、oniguruma(編譯安裝php7.4.x及其以上版本需要)
https://github.com/kkos/oniguruma/archive/refs/tags/v6.9.7.1.tar.gz -O oniguruma-6.9.7.1.tar.gz
21、curl庫(編譯php需要)
https://curl.se/download/curl-7.77.0.tar.gz
四、安裝編譯工具及庫文件(使用apt-get命令安裝)
apt-get install debian-keyring debian-archive-keyring build-essential gcc g++ make libtool automake autoconf libmcrypt-dev libxml2-dev re2c wget cron bzip2 libzip-dev libc6-dev bison file flex m4 gawk less cpp binutils diffutils unzip tar libbz2-dev libncurses5 libncurses5-dev libevent-dev openssl libssl-dev zlibc libsasl2-dev libltdl3-dev libltdl-dev zlib1g zlib1g-dev libbz2-1.0 libglib2.0-0 libglib2.0-dev libjpeg-dev libpng-dev libkrb5-dev curl libcurl3-gnutls libpcre3-dev libpq-dev libpq5 gettext libcap-dev ca-certificates libc-client2007e-dev psmisc patch git libc-ares-dev libicu-dev e2fsprogs libxslt1.1 libxslt1-dev libc-client-dev xz-utils libexpat1-dev libaio-dev libtirpc-dev python-dev libsqlite3-dev libonig-dev lsof libxpm-dev libfreetype6-dev checkinstall zip libfcgi-dev libfcgi0ldbl libmhash-dev freetds-dev libmariadbclient-dev-compat unixodbc-dev pkg-config libcurl4-openssl-dev
開始安裝Nginx
1、安裝pcre
cd /usr/local/src
mkdir /usr/local/pcre
tar zxvf pcre-8.44.tar.gz
cd pcre-8.44
./configure --prefix=/usr/local/pcre
make
make install
2、安裝openssl
cd /usr/local/src
mkdir /usr/local/openssl
tar zxvf openssl-1.1.1k.tar.gz
cd openssl-1.1.1k
./config -fPIC shared zlib --prefix=/usr/local/openssl/ enable-ec_nistp_64_gcc_128
./config -t
make
make install
ln -s /usr/local/openssl/lib /usr/local/openssl/lib/x86_64-linux-gnu #添加軟連接
3、安裝zlib
cd /usr/local/src
mkdir /usr/local/zlib
tar zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure --prefix=/usr/local/zlib
make
make install
4、安裝Nginx
groupadd www
useradd -g www www -s /bin/false
cd /usr/local/src
tar zxvf nginx-1.20.1.tar.gz
cd nginx-1.20.1
./configure --prefix=/usr/local/nginx --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-openssl=/usr/local/src/openssl-1.1.1k --with-zlib=/usr/local/src/zlib-1.2.11 --with-pcre=/usr/local/src/pcre-8.44
注意:--with-openssl=/usr/local/src/openssl-1.1.1k --with-zlib=/usr/local/src/zlib-1.2.11 --with-pcre=/usr/local/src/pcre-8.44指向的是源碼包解壓的路徑,而不是安裝的路徑,否則會(huì)報(bào)錯(cuò)
make #編譯
make install #安裝
/usr/local/nginx/sbin/nginx #啟動(dòng)Nginx
設(shè)置nginx開機(jī)啟動(dòng)
nano /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
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
ExecStartPost=/bin/sleep 0.1
PrivateTmp=true
[Install]
WantedBy=multi-user.target
ctrl+o #保存配置
ctrl+x #退出
/usr/local/nginx/sbin/nginx -s stop #停止
systemctl enable nginx.service #設(shè)置開機(jī)自啟動(dòng)
systemctl start nginx.service #啟動(dòng)
systemctl stop nginx.service #關(guān)閉
systemctl restart nginx.service #重啟
systemctl reload nginx.service #重新加載配置文件
打開瀏覽器,輸入服務(wù)器ip地址,看到如下界面,表示Nginx安裝成功