nginx的三种安装方式

一、安装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 808080 访问宿主机的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的安装路径
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Kubernetes中,您可以使用以下三种方式来部署Nginx: 1. 使用Deployment和Service:这是最常见的部署方式,它使用Deployment控制器来管理Pod的创建和伸缩,并使用Service将流量路由到Pod。以下是一个示例配置: Deployment配置(nginx-deployment.yaml): ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:latest ports: - containerPort: 80 ``` Service配置(nginx-service.yaml): ```yaml apiVersion: v1 kind: Service metadata: name: nginx-service spec: selector: app: nginx ports: - protocol: TCP port: 80 targetPort: 80 type: LoadBalancer ``` 使用命令部署: ``` kubectl apply -f nginx-deployment.yaml kubectl apply -f nginx-service.yaml ``` 2. 使用Helm Chart:Helm是Kubernetes的包管理工具,可以简化应用程序的部署和管理。您可以使用Helm Chart来部署Nginx。以下是一个示例配置: 创建Helm Chart: ``` helm create nginx-chart ``` 编辑Chart配置文件(values.yaml): ```yaml replicaCount: 3 image: repository: nginx tag: latest pullPolicy: IfNotPresent service: name: nginx-service type: LoadBalancer port: 80 ``` 安装Helm Chart: ``` helm install nginx nginx-chart ``` 3. 使用Kubernetes Ingress:Ingress是Kubernetes集群中的一个API对象,它充当流量入口,并将流量路由到不同的服务。您可以使用Ingress来部署Nginx,并通过Ingress规则配置路由。以下是一个示例配置: 创建Ingress资源(nginx-ingress.yaml): ```yaml apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: nginx-ingress spec: rules: - host: example.com http: paths: - pathType: Prefix path: / backend: service: name: nginx-service port: number: 80 ``` 使用命令部署: ``` kubectl apply -f nginx-deployment.yaml kubectl apply -f nginx-ingress.yaml ``` 请注意,这些示例仅提供了基本的部署配置。根据您的需求,您可能需要进行其他配置,例如使用持久卷声明(Persistent Volume Claim)来存储Nginx日志文件或自定义Nginx配置等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值