安装部署nginx+配置基于端口、域名的虚拟主机

目录

一、 源码安装nginx,并提供服务脚本

1、下载nginx源码包

2、添加nginx用户

3、编译安装

二、配置基于ip的虚拟主机

1、新增ip地址

2、创建站点目录 创建主页文件

3、配置文件添加虚拟主机部分

4、重启服务

5、测试

三、配置基于端口的虚拟主机

四、配置基于域名的虚拟主机

五、配置nginx基于用户和地址的访问控制

六、配置nginx rewrite,要求如果访问不存在的任意网页都重定向到错误页面,错误页面内容自行定义


一、 源码安装nginx,并提供服务脚本

1、下载nginx源码包

curl -o  nginx-1.20.1.tar.gz http://nginx.org/download/nginx-1.20.1.tar.gz

2、添加nginx用户

useradd nginx -c "Nginx web server" -d /var/lib/nginx -s /sbin/nologin -r
 
id nginx

3、编译安装

将安装包解压到安装目录

tar xf nginx-1.20.1.tar.gz -C /usr/local/src
cd /usr/local/src/nginx-1.20.1/
添加需要编译的参数和模块
./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 
 
#
./configure: error: C compiler cc is not found
没有C语言的编译环境
#
yum install -y gcc gcc-c++
继续
#
./configure: error: the HTTP rewrite module requires the PCRE library.
报错缺失一个pcre库
#
yum install pcre-devel -y
继续
#
./configure: error: SSL modules require the OpenSSL library.
缺失一个openssl库
#
yum install openssl-devel -y
继续

出现汇总信息

 

编译

make

安装

make install

 

编译配置文件

vim /usr/lib/systemd/system/nginx.service 
 
 
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
 
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target
 
 
# ExecStartPre  ExecStart
后面的第一个路径改为汇总信息的里nginx binary file: 的路径
第二个路径改为你指定的目录(--prefix=)里面的conf/nginx.conf路径

重新加载daemon-reload守护进程

systemctl daemon-reload

设置开机自启

systeml enable --now nginx

启动nginx

systemctl start nginx

查看端口

netstat -lntup | grep 80

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值