Nginx 实战闲谈第2讲:Centos7下Nginx各种方式安装

2023-12-19 21:39:34

前言

继续上一篇的协议介绍,今天来点实操的,毕竟技术性的玩意,不实操的话,很快就忘记,实操一点就知道大概是什么玩意了。

环境

阿里云的CentOs7。

先说点实在话

作为开发的铁子们,如果你还是一个在校的学生,一定要拿点钱整一台自己的服务器。只有让自己的程序跑在云上面,并且尝试去接触C端的用户,你才知道你要做什么,在这个过程中你会收获很多,至于具体是什么,就看你坚持多久,起初往往都是玩一玩,因为不知道自己要做什么。就跟我们之前做"韭盾"公众号一样,根本不知道要做什么,坚持了两年,现在系统也在逐渐完善中,预计2024年年初上线开放使用。最重要的是,他让我们知道我们要干什么,有目标地打工才有动力。这是唠嗑两句心里话,好了,下面上干货。

epol源安装

安装命令

 yum install -y nginx

官方源安装

1.配置官方源

[root@web02 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

2.安装依赖

[root@web02 ~]# yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree

3.安装nginx

[root@web02 ~]# yum install -y nginx

4.启动服务

[root@web02 ~]# systemctl start nginx
#或者
[root@web02 ~]# nginx

5.检验是否启动

#方式一:
	[root@web02 ~]# ps -ef | grep nginx
#方式二:
	[root@web02 ~]# netstat -lntp | grep 80
#方式三:
	访问页面 10.0.0.8:80
#方式四:
1.查看版本
[root@web02 ~]# nginx -v
2.查看安装模块
[root@web02 ~]# nginx -V

源码包安装

1.安装依赖

[root@web03 ~]# yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree

2.下载或上传源码包

[root@web03 ~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
#或者
[root@web03 ~]# rz nginx-1.18.0.tar.gz

3.解压

[root@web03 ~]# tar xf nginx-1.18.0.tar.gz

4.创建用户

[root@web03 ~]# groupadd www -g 666
[root@web03 ~]# useradd www -u 666 -g 666

5.生成编译文件

[root@web03 ~]# cd nginx-1.18.0/
[root@web03 ~/nginx-1.18.0]# ./configure --prefix=/usr/local/nginx-1.18.0 --user=www --group=www --with-http_addition_module --with-http_auth_request_module --without-http_gzip_module

6.编译安装

[root@web03 ~/nginx-1.18.0]# make && make install

7.配置system管理

[root@web03 ~]# vim /etc/systemd/system/nginx.service 
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target

8.做软连接

[root@web03 ~]# ln -s /usr/local/nginx-1.18.0 /usr/local/nginx

#软连接的作用:
1)配置环境变量可以不加版本号
2)配置system启动可以不加版本号
3)升级直接切换软连接的链接文件即可

9.配置环境变量

[root@web03 ~]# cat /etc/profile.d/nginx.sh 
export PATH=/usr/local/nginx/sbin/:$PATH

10.启动

[root@web03 ~]# systemctl daemon-reload
[root@web03 ~]# systemctl start nginx
#配置开机自启
[root@web03 ~]# systemctl enable nginx

拓展:Nginx常用命令

1.启动命令

[root@web02 ~]# systemctl start nginx
#或者
[root@web02 ~]# nginx

ps:使用哪种方式启动就用哪种方式关闭

2.关闭命令

[root@web02 ~]# systemctl stop nginx
#或者
[root@web02 ~]# nginx -s stop

3.重启命令

[root@web02 ~]# systemctl restart nginx

4.nginx重载配置文件

[root@web02 ~]# systemctl reload nginx
#或者
[root@web02 ~]# nginx -s reload

5.检查nginx配置

[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

6.加入开机自启

[root@web01 ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

今天的内容偏向实操,有需要用到nginx的铁子可以跟着选择一种方式安装一下。觉得有点作用的铁子们帮忙点赞 + 再看。更多干货持续输出中…
在这里插入图片描述

文章来源:https://blog.csdn.net/weixin_42329623/article/details/135091161
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。