Linux安装Nginx

article/2025/8/22 6:40:08

目录

安装包下载

1.安装Nginx

2.配置环境变量

3.开机启动配置

4.配置HTTPS


安装包下载

nginx: download

1.安装Nginx

1.1 安装依赖:压缩包一般放在/opt/software目录下,没有则创建

[root@localhost software]# yum -y install gcc zlib zlib-devel pcre pcre-devel openssl openssl-devel make

1.2 解压安装包并切换到文件夹中:

[root@localhost software]# tar -zxvf /opt/software/nginx-1.22.0.tar.gz
[root@localhost spftware]# cd nginx-1.22.0

1.3 开始执行编译、安装(一并安装SSL模块):

[root@localhost nginx-1.22.0]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
[root@localhost nginx-1.22.0]# make && make install

1.4 查看是否安装成功

[root@localhost nginx-1.22.0]# /usr/local/nginx/sbin
[root@localhost nginx-1.22.0]# ./nginx -V

显示如下:

nginx version: nginx/1.22.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

安装结束。

2.配置环境变量

2.1 查看 nginx 版本,此时会发现报 bash:nginx: 未找到命令,需要我们手动配置打开环境变量的文件

[root@localhost nginx-1.22.0]# nginx -v

显示如下:

bash: nginx: command not found...

2.2 编辑环境变量文件:

[root@localhost nginx-1.22.0]# vim /etc/profile

编辑文件:行末加上自己的nginx安装目录下sbin目录的地址:

.... #前面省略
export PATH=$PATH:/usr/local/nginx/sbin

2.3 重新加载环境:

[root@localhost nginx-1.22.0]# source /etc/profile

2.4 再次查看 nginx 版本

[root@localhost nginx-1.22.0]# nginx -v

显示如下:

nginx version: nginx/nginx-1.22.0

3.开机启动配置

3.1 先创建开机自启脚本:

[root@localhost nginx-1.22.0]# cd /etc/systemd/system
[root@localhost system]# vim nginx.service

3.2 内容复制到 vim 中:

[Unit]
Description=nginx service
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

3.3 服务命令:

第一种方式启动
#启动nginx服务
cd /usr/local/nginx/sbin/
./nginx
#查看运行状态
ps aux | grep nginx
#停止nginx服务
./nginx –s stop
#重启nginx服务
./nginx –s reload
#检查配置文件是否正确
./nginx –t
#查看nginx版本
./nginx –v

第二种基于配置开机启动模式启动
#设置开机自启动
systemctl enable nginx
#启动nginx服务
systemctl start nginx.service
#重新启动服务
systemctl restart nginx.service
#查看服务当前状态
systemctl status nginx.service
#停止开机自启动
systemctl disable nginx.service

3.4 访问服务器IP

显示welcom to nginx 说明启动安装成功了,接下来可以通过编辑nginx.conf文件来进行项目的部署了。建议备份一下配置文件。

4.配置HTTPS

证书可以自行到阿里云或者腾讯云申请免费的证书,并下载对应Nginx的证书
服务器 /usr/local/nginx/conf 下创建 cert 目录,并将证书放入该文件夹中

4.1 编辑Nginx配置文件:

[root@localhost system]#  cd /usr/local/nginx/conf
[root@localhost conf]#  vim nginx.conf

配置如下配置:

# HTTPS server
server {listen 80;server_name pmis.shxrtech.com;rewrite ^(.*)$ https://$host$1 permanent;
}server {listen 443 ssl;server_name cc.shxrtech.com;ssl_certificate cert/pmis.shxrtech.com.pem;ssl_certificate_key cert/pmis.shxrtech.com.key;ssl_session_cache shared:SSL:1m;ssl_session_timeout 5m;ssl_ciphers HIGH:!aNULL:!MD5;ssl_prefer_server_ciphers on;location / {root html/pms;index index.html index.htm;try_files $uri $uri/ /index.html;}location /api {proxy_pass http://localhost:8093/api;proxy_set_header Host $host;proxy_pass_header User-Agent;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Real-Port $remote_port;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_request_buffering off;client_max_body_size 1024m;server_tokens off;}
}

http://chatgpt.dhexx.cn/article/yVvC0nyj.shtml

相关文章

CentOS如何安装nginx

默认情况Centos7中无Nginx的源,最近发现Nginx官网提供了Centos的源地址。因此可以如下执行命令添加源: sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm 2、安装Nginx 通过yum search ng…

ubuntu系统安装nginx

目录 一、安装nginx之前,安装一下工具 二、安装anginx 1.下载anginx 2.安装步骤 3.开放访问端口 4.测试 三、nginx重启错误解决: nginx: [alert] kill(8478, 1) failed (3: No such process) 1.查看nginx相关进程 2. 杀死nginx相关进程 3.重新指定配置环境 4.重启 n…

CentOS7安装Nginx详细步骤

1.首先,先去nginx官网下载压缩包:官网地址 2.然后把压缩包上传到我们CentOS7上。 上传完后接下来我们要安装一些依赖环境: 3.安装gcc环境(因为我们nginx底层其实是C语言开发的):yum install gcc-c 如果有弹出Is this ok这种提示…

Windows下安装Nginx

前言 Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由伊戈尔赛索耶夫为俄罗斯访问量第二的Rambler.ru 站 点(俄文:Рамблер)开发的    它也是一种轻量级的Web服务器&…

Linux安装nginx完整步骤

一、安装依赖(安装过的跳过) yum -y install gcc gcc-c automake pcre pcre-devel zlib zlib-devel openssl-devel二、下载nginx稳定版 方法一:在目标linux服务上执行 wget http://nginx.org/download/nginx-1.16.1.tar.gz 方法二&#xf…

centOS7安装nginx及nginx配置

安装所需插件 1、安装gcc gcc是linux下的编译器在此不多做解释,感兴趣的小伙伴可以去查一下相关资料,它可以编译 C,C,Ada,Object C和Java等语言 命令:查看gcc版本 gcc -v 一般阿里云的centOS7里面是都有的,没有安装的话会提示…

Docker安装Nginx

1.下载镜像 https://hub.docker.com/_/nginx?tabtags 这里选择官方镜像1.22.0版本 docker pull nginx:1.22.0 下载完成后检查镜像 2.创建挂载目录 用于存放默认页面、日志、配置文件,把这些文件挂载到宿主机上好处是:在需要频繁修改Nginx配置的场景…

ubuntu安装nginx

目录 ubuntu安装nginx一、apt-get安装nginx二、下载nginx包安装 在操作过程中有任何疑问,请留言,或者加群:高级java学习交流群(821605718)。 ubuntu安装nginx 目前支持两种安装方式,一种是apt-get的方式&a…

CentOs7安装nginx【详细】

CentOs7安装nginx 卸载nginx 先查看是否启动了 nginx 服务 ps -ef|grep nginx出现这个则 nginx 没启动服务 出现这个则 nginx 启动了服务 如果 nginx 启动了服务,则需要先关闭 nginx 服务 【没启动就略过这一步】 kill 进程id查看所有与 nginx 有关的文件夹 fin…

yum方式安装nginx

1、添加CentOS 7 Nginx yum资源库 [rootlocalhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm 2、安装nginx [rootlocalhost ~]# yum -y install nginx //安装nginx 3、启动nginx [rootlocalhost ~]# …

CentOS安装Nginx

一:安装依赖 yum -y install gcc gcc-c make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel 二:下载Nginx版本 1.官网直接下载.tar.gz安装包,地址:nginx: download 2.使用wget命令下载 wget -c https://ng…

Linux离线安装nginx详细教程

文章目录 一、什么是离线安装?二、安装步骤1.安装nginx所需依赖1.1 安装gcc和gcc-c1.1.1 下载依赖包1.1.2 上传依赖包1.1.3 安装依赖1.1.4 验证安装 1.2 安装pcre1.2.1 下载pcre1.2.2 上传解压安装包1.2.3 编译安装 1.3 下载安装zlib1. 3.1 下载zlib1.3.2 上传解压安…

Linux安装nginx详细步骤

Linux安装nginx详细步骤 一、安装nginx依赖二、下载并解压nginx安装包1.下载nginx安装包2.解压nginx安装包 三、安装nginx四、启动nginx五、停止重启nginx六、设置开机自启动nginx七、补充1.配置nginx.conf2.防火墙设置 一、安装nginx依赖 #安装gcc yum install gcc-c#安装PCR…

Windows下安装Nginx(详解)

文章目录 一、背景二、安装Nginx1、去到Nginx官网:[http://nginx.org/](http://nginx.org/) ,然后点击“[download](http://nginx.org/en/download.html)”在这里插入图片描述 三、Nginx的使用1、解压Nginx压缩包。2、在nginx的配置文件是conf目录下的ng…

Nginx安装教程(亲测)

1.nginx安装教程(亲测) 1.安装环境 yum install gcc openssl openssl-devel pcre pcre-devel zlib zlib-devel -y 2.上传nginx安装包 在home下创建一个包去放下载的压缩包 mkdir soft 3.解压 tar -zxvf nginx-1.16.1.tar.gz 4.进入目录 cd nginx-1.16.1 5.在nginx主目录ng…

nginx安装的详细教程(包括命令行安装和编译安装)

Nginx 是一个很强大的高性能Web和反向代理服务。是一款自由的、开源的、高性能的HTTP服务器和反向代理服务器;同时也是一个IMAP、POP3、SMTP代理服务器;Nginx可以作为一个HTTP服务器进行网站的发布处理,另外Nginx可以作为反向代理进行负载均衡…

Nginx篇-Nginx详细安装教程

Nginx安装教程-基于Linux系统 一、Nginx是什么?1.1正向代理及反向代理 二、Nginx下载及安装2.1官网下载2.2Nginx安装 三、Nginx的启动及测试3.1启动Nginx3.2测试连接 四、设置Nginx开机自启4.1编写开机启动文件4.2设置开机启动 总结 一、Nginx是什么? N…

Linux安装Nginx(超详细步骤)

1,进入官网下载Nginx资源: 官网下载:http://nginx.org/en/download.html 2,资源下载(因为是在Linux安装,所以我下载Linux安装的资源包,版本你们随意) 3,资源下载好之后&#xff0…

nginx 安装教程(详解)

环境准备 虚拟机一台: centos8 IP:172.16.183.18 关闭防火墙(为了测试) systemctl status firewalld.service(查看防火墙状态) systemctl stop firewalld.service (关闭防火墙) systemctl…