这里有最新的使用文档和教程
CentOS8编译安装Nginx1.16.1,Centos8编译安装nginx ,编译安装nginx
安装前准备:
一、防火墙配置
CentOS 从7.x开始默认使用的是firewall作为防火墙,这里改为iptables防火墙。
1、关闭firewall:
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
systemctl mask firewalld
systemctl stop firewalld
yum -y remove firewalld
2、安装iptables防火墙
yum install iptables-services #安装
vi /etc/sysconfig/iptables #编辑防火墙配置文件,开发mysql默认端口3306
# 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 -s 192.168.0.100 -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT #只允许此ip访问
-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 3306 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
:wq! #保存退出
systemctl restart iptables.service #最后重启防火墙使配置生效
systemctl enable iptables.service #设置防火墙开机启动
/usr/libexec/iptables/iptables.init restart #重启防火墙
二、关闭SELINUX
vi /etc/selinux/config
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq! #保存退出
setenforce 0 #使配置立即生效
三 、系统约定
软件源代码包存放位置:/usr/local/src
源码包编译安装位置:/usr/local/软件名字
四、下载软件包
1、下载nginx
http://nginx.org/download/nginx-1.16.1.tar.gz
2、mysql
https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.18.tar.gz
3、下载php
https://www.php.net/distributions/php-7.3.10.tar.gz
4、下载cmake(MySQL编译工具)
https://github.com/Kitware/CMake/releases/download/v3.15.4/cmake-3.15.4.tar.gz
5、下载pcre (支持nginx伪静态)
ftp://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz
6、下载openssl(nginx扩展)
https://www.openssl.org/source/openssl-1.1.0l.tar.gz
7、下载zlib(nginx扩展)
http://www.zlib.net/zlib-1.2.11.tar.gz
8、下载libmcrypt(php扩展)
https://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
9、下载yasm(php扩展)
http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
10、t1lib(php扩展)
http://download.freenas.org/distfiles/t1lib-5.1.2.tar.gz
11、下载gd库安装包
https://github.com/libgd/libgd/releases/download/gd-2.1.1/libgd-2.1.1.tar.gz
12、libvpx(gd库需要)
https://codeload.github.com/webmproject/libvpx/tar.gz/v1.8.1
13、tiff(gd库需要)
http://download.osgeo.org/libtiff/tiff-4.0.7.tar.gz
14、libpng(gd库需要)
ftp://ftp.simplesystems.org/pub/libpng/png/src/libpng16/libpng-1.6.37.tar.gz
15、freetype(gd库需要)
http://download.savannah.gnu.org/releases/freetype/freetype-2.9.tar.gz
16、jpegsrc(gd库需要)
http://www.ijg.org/files/jpegsrc.v9c.tar.gz
17、Boost(编译mysql需要)
https://dl.bintray.com/boostorg/release/1.70.0/source/boost_1_70_0.tar.gz
18、libzip(编译php需要)
https://libzip.org/download/libzip-1.2.0.tar.gz
19、rpcsvc-proto(编译MySQL需要)
https://github.com/thkukuk/rpcsvc-proto/releases/download/v1.4/rpcsvc-proto-1.4.tar.gz
以上软件包上传到/usr/local/src目录
五、安装编译工具及库文件(使用yum命令安装)
yum install apr* autoconf automake bison bzip2 bzip2* cpp curl curl-devel fontconfig fontconfig-devel freetype-devel gcc gcc-c++ gd gd-devel gettext gettext-devel glibc kernel kernel-headers keyutils keyutils-libs-devel krb5-devel libcom_err-devel libpng libpng-devel libjpeg* libsepol-devel libselinux-devel libstdc++-devel libtool* libgomp libxml2 libxml2-devel libXpm* libxml* libXaw-devel libXmu-devel libtiff libtiff* make openssl openssl-devel patch pcre-devel perl php-common php-gd policycoreutils telnet wget zlib-devel ncurses-devel libtirpc-devel gtk* ntpstat bison* sqlite-devel libsqlite3x-devel oniguruma libzip-devel
开始安装Nginx
1、安装pcre
cd /usr/local/src
mkdir /usr/local/pcre
tar zxvf pcre-8.43.tar.gz
cd pcre-8.43
./configure --prefix=/usr/local/pcre
make
make install
2、安装openssl
cd /usr/local/src
mkdir /usr/local/openssl
tar zxvf openssl-1.1.0l.tar.gz
cd openssl-1.1.0l
./config --prefix=/usr/local/openssl
make
make install
vi /etc/profile
export PATH=$PATH:/usr/local/openssl/bin
export LD_LIBRARY_PATH=/usr/local/openssl/lib
export LC_ALL="en_US.UTF-8"
export LDFLAGS="-L/usr/local/openssl/lib -Wl,-rpath,/usr/local/openssl/lib"
:wq!
source /etc/profile
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.16.1.tar.gz
cd nginx-1.16.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.0l --with-zlib=/usr/local/src/zlib-1.2.11 --with-pcre=/usr/local/src/pcre-8.43
注意:--with-openssl=/usr/local/src/openssl-1.1.0l --with-zlib=/usr/local/src/zlib-1.2.11 --with-pcre=/usr/local/src/pcre-8.43指向的是源码包解压的路径,而不是安装的路径,否则会报错
make
make install
/usr/local/nginx/sbin/nginx #启动Nginx
设置nginx开机启动
vi /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
PrivateTmp=true
[Install]
WantedBy=multi-user.target
:wq! #保存退出
/usr/local/nginx/sbin/nginx -s stop #停止
systemctl enable nginx.service #设置开机自启动
systemctl start nginx.service #启动
systemctl stop nginx.service #关闭
systemctl restart nginx.service #重启
systemctl reload nginx.service #重新加载配置文件
在浏览器中打开服务器IP地址,会看到下面的界面,说明Nginx安装成功。
扩展阅读:
Nginx配置txt、pdf、doc、xls等文件直接下载的方法
在nginx配置文件中添加以下代码
location / {
if ($request_filename ~* ^.*?\.(txt|pdf|doc|xls)$){
add_header Content-Disposition: 'attachment;';
}
}