admin管理员组

文章数量:1446753

centos 安装nginx

一、首先安装必要组件

代码语言:bash复制
yum  -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel

解释:

  • gcc 可以编译c,c++,Ada,Object C和Java的语言 pcre pcre-devel pcre是一个perl库,包括perl兼容的正则表达式库,nginx的http模块使用pcre来解析正则表达式,所以需要安装pcre库 zlib zlib-devel zlib库提供了很多种压缩和解压缩方式nginx使用zlib对http包的内容进行gzip,所以需要安装 *openssl openssl-devel openssl是web安全通信的基石,没有openssl,可以说我们的信息基本上是不安全的

二、下载nginx

官网下载地址:

根据自己的需求可以选择最新版、稳定版和历史版,这里选择稳定版进行安装。

代码语言:bash复制
wget .24.0.tar.gz  #下载命令
tar -zxvf nginx-1.24.0.tar.gz  #解压命令

三、编译与安装nginx

1、进入解压好的nginx文件夹

代码语言:bash复制
cd /nginx-1.24.0  

2、编译nginx

代码语言:bash复制
./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、出现下面代码,表示配置成功

代码语言:bash复制
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、编译安装

代码语言:bash复制
make && make install

出现下图,说明nginx编译安装成功

代码语言:bash复制
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版本

代码语言:javascript代码运行次数:0运行复制
[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

四、启动nginx

1、进入nginx安装目录

代码语言:javascript代码运行次数:0运行复制
root@localhost sbin]# ./nginx #启动命令

2、验证网页是否能打开

命令验证,出现以下命令说明安装成功

代码语言:javascript代码运行次数:0运行复制
[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="/">nginx</a>.<br/>
Commercial support is available at
<a href="/">nginx</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@localhost sbin]#

解释:http://localhost为本机地址

浏览器验证,在本机浏览器上输入http://localhost出现下面页面说明安装成功

3、nginx常用命令

代码语言:bash复制
    ./nginx -s quit   #(温和)此方式停止步骤是待nginx进程处理任务完毕进行停止。
    ./nginx -s stop   # 停止进程。
    ./nginx -s reload  # 重新载入nginx
    ./nginx            # 启动

五、配置环境变量

1、找到nginx的sbin文件路径,可以通过find命令找到Nginx的sbin文件路径 ,查询命令:find / -name sbin

代码语言:javascript代码运行次数:0运行复制
[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!保存并退出

代码语言:javascript代码运行次数:0运行复制
 [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文件

代码语言:bash复制
vi /usr/lib/systemd/system/nginx.service

在文件中添加以下内容:

代码语言:bash复制
[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 开机自启动:

代码语言:bash复制
systemctl enable nginx.service

3、启动和管理 nginx 服务

你可以使用以下命令来启动、停止、重启和查看 Nginx 服务的状态:

代码语言:bash复制
 systemctl start nginx.service # 启动服务
 systemctl stop nginx.service # 停止服务
 systemctl restart nginx.service # 重启服务
 systemctl status nginx.service # 查看服务状态

至此nginx安装完成

本文标签: centos 安装nginx