本文已参与[新人创作礼]活动,一起开启掘金创作之路。
1.下载安装包
这里下载稳定版本:
2.将安装包上传至服务器
上传至服务器的opt目录
scp nginx-1.20.2.tar.gz root@192.168.74.131:/opt
查看是否上传成功。
上传成功:
3.解压
在安装包所在目录解压:
tar -zxvf nginx-1.20.2.tar.gz
解压成功:
可删除安装包,节约空间。
4.开始安装
在安装开始前需要安装一些依赖,进入解压后的文件:
1.执行可执行文件.configure
出现错误:
checking for OS
+ Linux 3.10.0-1160.el7.x86_64 x86_64
checking for C compiler ... not found
./configure: error: C compiler cc is not found
原因是缺少C语言的编译器。
GCC:
gcc:GCC(GNU Compiler Collection,GNU 编译器套件),是由 GNU 开发的编程语言编译器。GCC 原本作为 GNU 操作系统的官方编译器,现已被大多数类 Unix 操作系统(如 Linux、BSD、Mac OS X 等)采纳为标准的编译器。
安装依赖gcc:
yum install -y gcc
安装完成:
2.再执行可执行文件.configure
在编译nginx时,可指定把nginx安装到指定的目录下(/usr/local/nginx)
./configure --prefix=/usr/local/nginx
报错:
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
PCRE:
PCRE(Perl Compatible Regular Expressions)安装 ,它是一个 Perl 库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库。
安装依赖pcre 库:
yum install -y pcre pcre-devel
安装完成:
3.再执行可执行文件.configure
./configure --prefix=/usr/local/nginx
报错:
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
zlib:它提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 linux 上安装 zlib 库。
安装依赖zlib库:
yum install -y zlib zlib-devel
安装完成:
4.再执行可执行文件.configure
./configure --prefix=/usr/local/nginx
成功:
5.执行make 和 make install
make执行完成:
make install执行完成:
6.查看是否安装成功
cd /usr/local/nginx
可以看见nginx目录下有以下文件:
7.启动nginx
因为没有脚本等,所以现在是手动启动。
去到sbin目录,执行可执行文件nginx:
./nginx
8.检查是否启动成功
通过ip addr获取ip,浏览器访问该服务器ip。
如果访问不了,一直转圈,出现无法访问此网站,原因是防火墙拦截了nginx的端口,需要开放端口。
查看防火墙状态:
firewall-cmd --state
查看防火强墙开放的端口:
firewall-cmd --list-all
开放80端口:
firewall-cmd --zone=public --add-port=80/tcp --permanent
重启防火墙:
firewall-cmd --reload
再次访问ip地址,访问成功:
9.创建服务脚本
之前都是通过执行可执行文件.nginx启动的nginx,但是这样很不方便,所以需要配置脚本文件,配置脚本文件后可以使用systemctl操作。
输入命令:
vi /usr/lib/systemd/system/nginx.service
建立了nginx.service服务文件,将下面的内容拷贝进去(注意路径,这里是将nginx安装到/usr/local/nginx时的配置文件),在insert状态下可完整粘贴:
[Unit]
Description=nginx - web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
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
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
10.重新加载系统服务
systemctl daemon-reload
11.查看之前通过.nginx启动的nginx
查看之前通过.nginx启动的nginx是否还在,如果在的话关闭nginx。
ps -ef | grep nginx
12.关闭nginx
在安装的nginx的sbin目录下,关闭nginx:
./nginx -s reload
再查看:
ps -ef | grep nginx
关闭成功:
13.使用systemctl启动nginx
systemctl start nginx
查看状态:
systemctl status nginx
nginx设置开机自启:
systemctl enable nginx
使用.nginx对nginx相关操作
进入nginx安装目录的sbin目录:
启动nginx ./nginx 快速停止 ./nginx -s stop 在退出前完成已经接受的连接请求 ./nginx -s quit 重新加载配置 ./nginx -s reload