准备篇
一、关闭防火墙firewall:
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
systemctl mask firewalld
systemctl stop firewalld
二、关闭SELINUX
vi /etc/selinux/config
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq! #保存退出
setenforce 0 #使配置立即生效
三 、路径约定
软件源代码包存放位置:/usr/local/src
源码包编译安装位置:/usr/local/软件名字,例如/usr/local/nginx
四、下载软件包
wget http://nginx.org/download/nginx-1.16.1.tar.gz
wget wget https://www.openssl.org/source/openssl-1.1.1e.tar.gz
wget http://ftp.pcre.org/pub/pcre/pcre-8.44.tar.gz
wget http://www.zlib.net/zlib-1.2.11.tar.gz
五、安装编译工具及库文件(使用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 oniguruma libzip-devel
安装篇
六、安装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.1e.tar.gz
cd openssl-1.1.1e
./config shared zlib --prefix=/usr/local/openssl
make
make install
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.1e --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.1e --with-zlib=/usr/local/src/zlib-1.2.11 --with-pcre=/usr/local/src/pcre-8.44指向的是源码包解压的路径,而不是安装的路径,否则会报错
make
make install
手动启动和关闭nginx
/usr/local/nginx/sbin/nginx #启动nginx
/usr/local/nginx/sbin/nginx -s stop #停止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
ExecStartPost=/bin/sleep 0.1
PrivateTmp=true
[Install]
WantedBy=multi-user.target
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27[Unit]
Description=TheNGINXHTTPandreverseproxyserver
After=syslog.targetnetwork.targetremote-fs.targetnss-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-sreload
ExecStop=/bin/kill-sQUIT$MAINPID
ExecStartPost=/bin/sleep0.1
PrivateTmp=true
[Install]
WantedBy=multi-user.target
:wq! #保存退出
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;';
}
}
1
2
3
4
5location/{
if($request_filename~*^.*?\.(txt|pdf|doc|xls)$){
add_headerContent-Disposition:'attachment;';
}
}