一、安装nginx
1、yum安装
1.安装
[root@lhj-docker lianxi]# yum install nginx -y
2.启动nginx服务
[root@lhj-docker lianxi]# systemctl start nginx
3.查看nginx是否启动成功
[root@lhj-docker lianxi]# ps aux|grep nginx
root 15523 0.0 0.0 119180 2172 ? Ss 16:48 0:00 nginx: master process /usr/sbin/nginx
nginx 15524 0.0 0.2 151836 7916 ? S 16:48 0:00 nginx: worker process
nginx 15525 0.0 0.2 151836 7916 ? S 16:48 0:00 nginx: worker process
root 15528 0.0 0.0 12348 1148 pts/0 S+ 16:48 0:00 grep --color=auto nginx
查看
[root@lhj-docker lianxi]# ss -anplut|grep nginx
tcp LISTEN 0 128 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=15525,fd=8),("nginx",pid=15524,fd=8),("nginx",pid=15523,fd=8))
tcp LISTEN 0 128 [::]:80 [::]:* users:(("nginx",pid=15525,fd=9),("nginx",pid=15524,fd=9),("nginx",pid=15523,fd=9))
4.查看ip地址
[root@lhj-docker lianxi]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:9f:e4:3b brd ff:ff:ff:ff:ff:ff
inet 192.168.243.135/24 brd 192.168.243.255 scope global dynamic noprefixroute ens33
valid_lft 1665sec preferred_lft 1665sec
inet6 fe80::20c:29ff:fe9f:e43b/64 scope link noprefixroute
valid_lft forever preferred_lft forever
5.关闭防火墙和selinux,并且设置开机不启动
[root@lhj-docker lianxi]# service firewalld stop 立马关闭防火墙
Redirecting to /bin/systemctl stop firewalld.service
[root@lhj-docker lianxi]# getenforce
Enforcing
[root@lhj-docker lianxi]# setenforce 0 关闭selinux
[root@lhj-docker lianxi]# systemctl disable firewalld 设置防火墙开机不启动
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
查看防火墙状态:
[root@xiaoliu ~]# firewall-cmd --state
not running
6.访问网站
打开浏览器在地址栏里输入ip地址
7.修改网站的首页
[root@mysql-proxy-1 lianxi]# cd /usr/share/nginx/html/ 进入存放网页的目录
[root@mysql-proxy-1 html]# ls
404.html 50x.html en-US icons img index.html nginx-logo.png poweredby.png
[root@mysql-proxy-1 html]#
index.html 打开网站看到的第1个页面--》首页
[root@mysql-proxy-1 html]# rm -rf index.html 删除首页
[root@mysql-proxy-1 html]# vim index.html 新建了一个首页
<html>
<head>
<title> welcome to changsha </title>
</head>
<body>
<h1><p>name:liuhongjie</h1>
<p>phone:17670404872
<p>address:hunan.huaihua
<p>sex:male
<p><img src=kobe.jpg width=500> #kobe.jpg需要自己上传到/usr/share目录里去
</body>
</html>
2、使用docker容器,在容器里安装nginx
1、service docker restart
重启docker服务,会重新添加docker自定义链在iptables里
Redirecting to /bin/systemctl restart docker.service
2、docker run –name lhj-nginx -v /usr/share/nginx/html:/usr/share/nginx/html:ro -d -p 8080:80 daocloud.io/nginx
docker run 启动容器
--name lhj-nginx 指定容器的名字
-v /usr/share/nginx/html:/usr/share/nginx/html:ro 数据卷:可以实现宿主机和容器直接的数据共享
第一个/usr/share/nginx/html 是宿主机里的目录
第二个/usr/share/nginx/html 是容器里的系统的目录
ro 只读
-d 在后台启动一个容器进程 deamon
-p 端口的映射:iptables的DNAT 8080:80 访问宿主机的8080端口,转发到容器里的80端口
daocloud.io/nginx 到daocloud.io网站去下载nginx的镜像
3、一个容器就是一个进程在支撑,多启动几个容器
docker run –name lhj-nginx-2 -v /usr/share/nginx/html:/usr/share/nginx/html:ro -d -p 8081:80 daocloud.io/nginx
docker run –name lhj-nginx-3 -v /usr/share/nginx/html:/usr/share/nginx/html:ro -d -p 8082:80 daocloud.io/nginx
4、docker ps :查看启动了几个容器
5、在浏览器用宿主机ip加端口来访问容器里的nginx web服务,看是否启动成功
3、编译安装nginx,也是最常用的安装方式
1、为什么需要编译安装
1.能够实现定制功能,需要什么功能就可以使用参数加上
2.可以指定安装的路径
2、一键编译安装的脚本
#!/bin/bash
#解决软件的依赖关系,需要安装的软件包
yum install epel-release -y
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gcc-c++ autoconf automake make psmisc net-tools lsof vim geoip geoip-devel wget -y
#新建luogan用户和组
id lilin || useradd lilin -s /sbin/nologin
#下载nginx软件
mkdir /lilin99 -p
cd /lilin99
wget https://nginx.org/download/nginx-1.21.4.tar.gz
#解压软件
tar xf nginx-1.21.4.tar.gz
#进入解压后的文件夹
cd nginx-1.21.4
#编译前的配置
./configure --prefix=/usr/local/sclilin99 --user=lilin --group=lilin --with-http_ssl_module --with-threads --with-http_v2_module --with-http_stub_status_module --with-stream --with-http_geoip_module --with-http_gunzip_module
#如果上面的编译前的配置失败,直接退出脚本
if (( $? != 0));then
exit
fi
#编译,启动2个进程去编译,这样速度快
make -j 2
#编译安装
make install
#修改PATH变量
echo "PATH=$PATH:/usr/local/sclilin99/sbin" >>/root/.bashrc
#执行修改了环境变量的脚本
source /root/.bashrc
#firewalld and selinux
#stop firewall和设置下次开机不启动firewalld
service firewalld stop
systemctl disable firewalld
#临时停止selinux和永久停止selinux
setenforce 0
sed -i '/^SELINUX=/ s/enforcing/disabled/' /etc/selinux/config
#开机启动
chmod +x /etc/rc.d/rc.local
echo "/usr/local/sclilin99/sbin/nginx" >>/etc/rc.local
#修改nginx.conf的配置,例如:端口号,worker进程数,线程数,服务域名
sed -i '/worker_processes/ s/1/2/' /usr/local/sclilin99/conf/nginx.conf
sed -i '/worker_connections/ s/1024/2048/' /usr/local/sclilin99/conf/nginx.conf
sed -i -r '36c \\tlisten 80;' /usr/local/sclilin99/conf/nginx.conf
sed -i -r '37c \\tserver_name www.lilin.com;' /usr/local/sclilin99/conf/nginx.conf
#killall nginx进程
killall -9 nginx
#启动nginx
/usr/local/sclilin99/sbin/nginx
3、编译安装的参数
1.--with-http_stub_status_module
状态统计功能
2.--with-stream
4层负载均衡功能
3.--with-http_ssl_module
实现https功能
4.--with-http_realip_module
让后端的web服务器知道前面的代理的ip地址,获得真正的客户端的ip地址
5.--with-http_geo_module
根据客户端的ip地址进行限制的
4、如何卸载编译安装的nginx?
1.删除安装指定的目录
脚本中--prefix指定的路径就是安装nginx的目录
2.在PATH变量里删除nginx的安装路径
版权声明:本文为weixin_47661174原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。