安装nginx所需的pere库
pere,peri兼容正则表达式
yum install -y pcre-devel
安装nginx
安装openssl-devel
yum install -y openssl-devel
检查并安装nginx基础依赖包pcre-devel、openssl-devel
rpm -qa | egrep 'pcre-devel|openssl-devel'
开始安装nginx
创建组和用户
groupadd -r -g 995 nginx
useradd -r -u 995 -g 995 -s /sbin/nologin -M nginx
下载nginx
wget http://nginx.org/download/nginx-1.18.0.tar.gz
解压
tar xf nginx-1.18.0.tar.gz -C /usr/local/src/
下载编译器
yum install gcc gcc-c++ make -y
进入目录
cd /usr/local/src/nginx-1.18.0/
./configure --help
./configure --user=nginx --group=nginx \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_auth_request_module \
--with-http_gzip_static_module \
--with-http_gunzip_module \
--with-http_stub_status_module
安装
make install
修改配置文件
vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/After=network-online.target remote-fs.targe nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
测试:
systemctl enable --now nginx
查看端口
netstat -lnupt | grep 80
基于rpm下载的nginx
参考http://nginx.org/en/linux_packages.html
新增IP地址
[root@localhost ~]# nmcli connection modify ens33 +ipv4.addresses 172.25.10.110/24
[root@localhost ~]# nmcli connection up ens33
[root@localhost ~]# nmcli connection reload
创建站点目录
创建主页文件
[root@localhost ~]# cd /usr/share/nginx/html/
[root@localhost html]# for name in bbs blog; do mkdir $name; done
[root@localhost html]# for name in bbs blog; do echo "$name test page." > $name/index.html;done
[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# vim vhost.conf
server{
listen 172.25.10.100:80;
server_name localhost;
location / {
root /usr/share/nginx/html/bbs/;
index index.html index.htm;
}
}
server{
listen 172.25.10.110:80;
server_name localhost;
location / {
root /usr/share/nginx/html/blog/;
index index.html index.htm;
}
}
[root@localhost conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
重启服务
systemctl restart nginx
测试
[root@localhost conf.d]# vim vhost.conf
server{
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html/bbs/;
index index.html index.htm;
}
}
server{
listen 81;
server_name localhost;
location / {
root /usr/share/nginx/html/blog/;
index index.html index.htm;
}
}
[root@localhost conf.d]# nginx -t
[root@localhost conf.d]# systemctl restart nginx
[root@localhost conf.d]# vim vhost.conf
server{
listen 80;
server_name bbs.jzz.org;
location / {
root /usr/share/nginx/html/bbs/;
index index.html index.htm;
}
}
server{
listen 80;
server_name blog.jzz.org;
location / {
root /usr/share/nginx/html/blog/;
index index.html index.htm;
}
}
[root@localhost conf.d]# nginx -t
[root@localhost conf.d]# vim /etc/hosts
172.25.10.100 bbs.jzz.org blog.jzz.org
[root@localhost conf.d]# systemctl restart nginx
①定义错误页面内容:
echo "the require failed" > /usr/share/nginx/html/test1/err.html
②编辑配置文件:
[root@localhost ~]# vim /etc/nginx/conf.d/vhost.conf
修改内容如下:
server {
listen 80;
server_name www.test1.com;
location / {
root /usr/share/nginx/html/test1/;
index index.html index.htm;
if (!-f $request_filename) {
rewrite /.* err.html permanent;}
}
}
③重启服务
systemctl restart nginx