yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
解释:
官网下载地址:https://nginx.org
根据自己的需求可以选择最新版、稳定版和历史版,这里选择稳定版进行安装。
wget https://nginx.org/download/nginx-1.24.0.tar.gz #下载命令
tar -zxvf nginx-1.24.0.tar.gz #解压命令
1、进入解压好的nginx文件夹
cd /nginx-1.24.0
2、编译nginx
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_sub_module --with-http_gzip_static_module --with-pcre
3、出现下面代码,表示配置成功
Configuration summary
+ using system PCRE2 library
+ using system OpenSSL library
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
4、编译安装
make && make install
出现下图,说明nginx编译安装成功
cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
test -d '/usr/local/nginx/logs' \
|| mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/logs' \
|| mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/html' \
|| cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs' \
|| mkdir -p '/usr/local/nginx/logs'
make[1]: 离开目录“/root/serv/nginx-1.24.0”
5、验证nginx版本
[root@localhost sbin]# cd /usr/local/nginx/sbin #进入nginx安装目录
[root@localhost sbin]# ls
nginx
[root@localhost sbin]# ./nginx -v #查看nginx版本命令
nginx version: nginx/1.24.0
1、进入nginx安装目录
root@localhost sbin]# ./nginx #启动命令
2、验证网页是否能打开
命令验证,出现以下命令说明安装成功
[root@localhost sbin]# curl http://localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@localhost sbin]#
解释:http://localhost为本机地址
浏览器验证,在本机浏览器上输入http://localhost出现下面页面说明安装成功
3、nginx常用命令
./nginx -s quit #(温和)此方式停止步骤是待nginx进程处理任务完毕进行停止。
./nginx -s stop # 停止进程。
./nginx -s reload # 重新载入nginx
./nginx # 启动
1、找到nginx的sbin文件路径,可以通过find命令找到Nginx的sbin文件路径 ,查询命令:find / -name sbin
[root@localhost ~]# find / -name sbin
/usr/sbin
/usr/lib/debug/usr/sbin
/usr/lib/debug/sbin
/usr/local/sbin
/usr/local/nginx/sbin
/sbin
[root@localhost ~]#
2、编辑环境变量文件:打开/etc/profile文件,添加Nginx的sbin路径到PATH环境变量中: vi /etc/profile 在文件末尾添加以下内容:export PATH=$PATH:/usr/local/nginx/sbin , 按esc输入:wq!保存并退出
[root@localhost ~]# vi /etc/profile
umask 002
else
umask 022
fi
for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do
if [ -r "$i" ]; then
if [ "${-#*i}" != "$-" ]; then
. "$i"
else
. "$i" >/dev/null
fi
fi
done
unset i
unset -f pathmunge
if [ -n "${BASH_VERSION-}" ] ; then
if [ -f /etc/bashrc ] ; then
# Bash login shells run only /etc/profile
# Bash non-login shells run only /etc/bashrc
# Check for double sourcing is done in /etc/bashrc.
. /etc/bashrc
fi
fi
# set minio environment
export PATH=$PATH:/usr/local/nginx/sbin
~
3、使环境变量生效:保存并退出编辑器后,执行以下命令使环境变量生效: source /etc/profile
4、验证环境变量:通过以下命令查看环境变量是否配置成功: echo $PATH 确认输出中包含Nginx的sbin路径。
设置 Nginx 开机启动可以通过 systemd 服务来实现。以下是具体步骤:
1、在/usr/lib/systemd/system/目录下创建nginx.service文件
vi /usr/lib/systemd/system/nginx.service
在文件中添加以下内容:
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
2、设置开机自启动
保存并退出编辑器后,运行以下命令以设置 nginx 开机自启动:
systemctl enable nginx.service
3、启动和管理 nginx 服务
你可以使用以下命令来启动、停止、重启和查看 Nginx 服务的状态:
systemctl start nginx.service # 启动服务
systemctl stop nginx.service # 停止服务
systemctl restart nginx.service # 重启服务
systemctl status nginx.service # 查看服务状态
至此nginx安装完成
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。