目录
前言
源码安装nginx的使用 虚拟主机的应用 基于nginx源码包编写的Unit文文件
一、源码安装nginx
1)安装流程
安装编译工具 gcc make=》解包=》配置=》编译=》安装
2)操作流程
[root@proxy ~]# yum -y install gcc make pcre-devel openssl-devel
#安装编译工具,正则表达式依赖包,SSL加密依赖包
[root@proxy ~]# tar -xf /root/lnmp_soft.tar.gz
[root@proxy ~]# cd lnmp_soft/
[root@proxy ~]# tar -xf nginx-1.22.1.tar.gz
[root@proxy lnmp_soft]# cd nginx-1.22.1/
[root@proxy nginx-1.22.1]# ./configure
--prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module
#指定安装路径,指定用户,指定组,开启SSL加密功能
[root@proxy nginx-1.22.1]# make #编译
[root@proxy nginx-1.22.1]# make install #安装
[root@proxy nginx-1.22.1]# cd /usr/local/nginx/
[root@proxy nginx]# ls
conf html logs sbin
启动nginx服务器之前 需要建立一个同nginx的用户来单独承载这个服务器来运行
如果没有这个用户 它会找不到 无法运行
[root@proxy nginx]# useradd nginx -s /sbin/nologin
[root@proxy nginx]# /usr/local/nginx/sbin/nginx #启动服务
nginx服务默认通过80端口监听客户端请求
[root@proxy nginx]# ss -antlp | grep 80
tcp LISTEN 0 128 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=7681,fd=6),("nginx",pid=7680,fd=6))
nginx命令其他命令:
[root@proxy nginx]# /usr/local/nginx/sbin/nginx -V #查看软件信息
[root@proxy nginx]# /usr/local/nginx/sbin/nginx -s reload #重新加载配置文件
[root@proxy nginx]# /usr/local/nginx/sbin/nginx -s stop #关闭服务
客户端测试
Nginx服务默认首页文档存储目录为/usr/local/nginx/html/,在此目录下默认有一个index.html的文件
命令行访问:
[root@proxy nginx]# /usr/local/nginx/sbin/nginx
[root@client ~]# curl http://192.168.88.5
<html>
<head>
<title>Welcome to nginx!</title>
</head>
...
浏览器访问:
真机浏览器访问192.168.88.5
3)目录说明
conf 配置文件目录
sbin 主程序目录
html 网站页面目录
logs 日志目录
4)沿2实现的继续 用户认证
访问nginx的Web页面需要进行用户认证
用户名为:tom,密码为:123456
修改Nginx配置文件
1)开启密码认证功能
[root@proxy nginx]# vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
listen 80;
server_name localhost;
auth_basic "Input Password:"; #新添加,认证提示符信息
auth_basic_user_file "/usr/local/nginx/pass"; #新添加,认证的密码文件
...
location / { #网页访问根目录
root html; #网页访问根文件
index index.html index.htm; #网页访问指定文件
}
}
2)生成密码文件在Nginx文件下
使用htpasswd命令创建账户文件,需要确保系统中已经安装了httpd-tools
htpasswd -c /usr/local/nginx/pass tom 创建密码文件,-c新创建
[root@proxy nginx]# yum -y install httpd-tools #如果已经安装,则不需要执行安装命令
[root@proxy nginx]# htpasswd -c /usr/local/nginx/pass tom #创建密码文件,-c新创建
New password: #密码123456
Re-type new password: #密码123456
Adding password for user tom
3)重新加载配置
[root@proxy nginx]# /usr/local/nginx/sbin/nginx -s reload #重新加载配置文件
4)浏览器测试
访问192.168.88.5,会出现一个登录的界面,输入用户名/密码:tom/123456登录即可
5)追加账户
追加就不用新建了-c 只有第一次需要
[root@proxy nginx]# htpasswd /usr/local/nginx/pass alice #追加用户,不使用-c选项
New password: #密码123456
Re-type new password: #密码123456
Adding password for user alice
[root@proxy ~]# cat /usr/local/nginx/pass
二、虚拟主机
1)基于域名的虚拟主机
创建多个server来承载多个页面
[root@proxy nginx]# cp conf/nginx.conf.default conf/nginx.conf #还原配置文件
#conf/nginx.conf.default 自带的模板配置文件 进行覆盖
[root@proxy nginx]# vim conf/nginx.conf
...
http {
.. .
server {
listen 80; #端口
server_name www.b.com; #定义虚拟主机域名
location / { #可以根据不同文件夹的页面 进行指定设置和限制
root html_b; #指定网站根路径页面
index index.html index.htm; #默认页面
}
}
server {
listen 80; #端口
server_name www.a.com; #定义虚拟主机域名
...
location / {
root html; #指定网站根路径页面
index index.html index.htm;
}
}
...
[root@proxy nginx]# /usr/local/nginx/sbin/nginx -s reload #重新加载配置文件
创建网站文件 进行访问
[root@proxy nginx]# echo "nginx-A~~~" > html/index.html #创建a网站测试页
[root@proxy nginx]# mkdir html_b #创建b网站的目录
[root@proxy nginx]# echo "nginx-B~~~" > html_b/index.html #创建b网站测试页
client客户端测试
[root@client ~]# vim /etc/hosts #修改hosts文件添加ip和域名的映射关系
192.168.88.5 www.a.com www.b.com
[root@client ~]# curl www.a.com
nginx-A~~~
[root@client ~]# curl www.b.com
nginx-B~~~
2)基于端口的虚拟机访问
域名一样 端口不一样
[root@proxy nginx]# vim conf/nginx.conf
...
server {
listen 8080; #端口
server_name www.a.com; #域名
......
}
server {
listen 8000; #端口
server_name www.a.com; #域名
.......
}
...
[root@proxy nginx]# sbin/nginx -s reload #重新加载配置文件
client客户端测试
[root@client ~]# curl www.a.com:8080
nginx-B~~~
[root@client ~]# curl www.a.com:8000
nginx-A~~~
3)基于IP的虚拟主机
[root@proxy nginx]# vim conf/nginx.conf
...
server {
listen 192.168.88.5:80; #IP地址与端口
server_name www.a.com; #域名
... ...
}
server {
listen 192.168.99.5:80; #IP地址与端口
server_name www.a.com;
... ...
}
...
[root@proxy nginx]# /usr/local/nginx/sbin/nginx -s reload #重新加载配置文件
三、加密虚拟主机 https加密访问
源码安装Nginx时必须使用--with-http_ssl_module参数,启用加密模块,对于需要进行SSL加密处理的站点添加ssl相关指令(设置网站需要的私钥和证书)
1)加密命令的示例
root@proxy nginx]# echo 123 > /root/test
[root@proxy nginx]# md5sum /root/test
[root@proxy nginx]# echo 1234 > /root/test #更改文件内容
[root@proxy nginx]# md5sum /root/test #md5值已经发生变化
[root@proxy nginx]# sha256sum /root/test
a883dafc480d466ee04e0d6da986bd78eb1fdd2178d04693723da3a8f95d42f4 /root/test
[root@proxy nginx]# sha512sum /root/test
7985558370f0de86a864e0050afdf45d7029b8798bcd72cddbf781329f99380e3f3b1afdca6765d89fc388b213df8f6a193cfc56d4ff2ef6e0a99bd883a6d98c /root/test
2)配置SSL虚拟主机
1)修改Nginx配置
[root@proxy nginx]# cp conf/nginx.conf.default conf/nginx.conf #还原配置文件
[root@proxy nginx]# vim /usr/local/nginx/conf/nginx.conf
...
server {
listen 443 ssl; #监听端口443,ssl使用安全加密技术 ssl简写s 所以是https
server_name localhost;
ssl_certificate cert.pem; #这里是公钥证书文件
ssl_certificate_key cert.key; #这里是私钥文件
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root https; #加密网站根目录,更改,也可以自行定义 这里进行了自定义
index index.html index.htm;
}
}
...
2)生成私钥与证书
[root@proxy nginx]# openssl genrsa > conf/cert.key #生成私钥,放到cert.key文件
[root@proxy nginx]# openssl req -x509 -key conf/cert.key > conf/cert.pem 将私钥文件生成证书
#-x509格式,生成证书,生成过程会询问诸如你在哪个国家之类的问题,可以随意回答
Country Name (2 letter code) [XX]:dc #国家名
State or Province Name (full name) []:dc #省份
Locality Name (eg, city) [Default City]:dc #城市
Organization Name (eg, company) [Default Company Ltd]:dc #公司
Organizational Unit Name (eg, section) []:dc #部门
Common Name (eg, your name or your server's hostname) []:dc #服务器名称
Email Address []:dc@dc.com #电子邮件
[root@proxy nginx]# /usr/local/nginx/sbin/nginx -s reload #重新加载配置文件
[root@proxy nginx]# mkdir https #创建安全网站的目录
[root@proxy nginx]# echo "nginx-https~" > https/index.html #创建安全网站的页面
3)客户端验证
命令行测试
[root@client ~]# curl -k https://192.168.88.5 #检验,-k是忽略安全风险 不加访问失败
nginx-https~ #看到这个内容就说明实验成功
真机浏览器访问:https://192.168.88.5
四、编写Unit文件
1)Unit文件语法格式参考表
语句 | 描述 |
Description | 描述信息 |
After | 在哪个服务之后启动 |
Before | 在哪个服务之前启动 |
type | 服务类型,默认为simple |
EnvironmentFile | 定义变量文件 |
ExecStart | 执行systemctl start需要启动的进程名称 |
ExecStop | 执行systemctl stop需要停止的进程名称 |
ExecReload | 执行systemctl reload需要执行的命令 |
2)使用systemd管理Nginx
编写Unit文件
[root@web1 ~]# cd /usr/lib/systemd/system
[root@system ~]# cp httpd.service nginx.service
[root@system ~]# vim nginx.service
[Unit]
Description=The Nginx HTTP Server #描述信息
After=network.target remote-fs.target nss-lookup.target #在网络程序,网络文件系统,域名解析等服务启动之后,再启动nginx
[Service]
Type=forking #forking多进程类型服务
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID #kill给程序发送QUIT退出信号,关闭nginx
[Install]
WantedBy=multi-user.target
[root@web1 ~]#systemctl start nginx #可以控制nginx开启了,这里如果无效可以尝试重启服务器
五、ss命令
ss命令 查看系统中启动的端口信息
选项
-a显示所有端口的信息
-n以数字格式显示端口号
-t显示TCP连接的端口
-u显示UDP连接的端口
-l显示服务正在监听的端口信息,如httpd启动后,会一直监听80端口
-p显示监听端口的服务名称是什么(也就是程序名称)
ss -antlp 查看tcp的所有运行的服务端口等信息
[root@proxy nginx]# ss -antlp
[root@proxy nginx]# ss -antlp | grep 80 #只查看80的