centos7 nginx 安装

本文详细介绍如何在服务器上安装Nginx,并提供了一系列必要的依赖安装命令。通过具体步骤指导如何下载、上传、解压及配置Nginx,确保用户能够顺利完成安装过程。

别管那么多 先装一堆基础依赖(本次用的着的,用不着的以装别的也可能用的着的)

yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel gcc gcc-c++  openssl-devel
yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
yum install  gcc perl-ExtUtils-MakeMaker
  1. 官网下载nginx(找当前的稳定版)
  2. 下载后上传到服务器(scp 就不用写了吧)
  3. 解压并进入目录:tar -xzf nginx-1.16.0.tar.gz cd nginx-1.16.0
  4. 编译安装:
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_gzip_static_module --with-http_stub_status_module --with-http_ssl_module --with-pcre --with-file-aio --with-http_realip_module --without-http_scgi_module --without-http_uwsgi_module --without-http_fastcgi_module`
make && make install
  1. nginx 安装到 /usr/local/nginx
  2. 安装好以后修改 nginx.conf 文件进行配置。配置修改后使用测试命令
nginx -t 

出现以下显示为配置文件修改没有错误,可以重新加载配置

nginx: the configuration file /usr/local/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/nginx.conf test is successful

重新加载配置

nginx -s reload 
### 如何在 CentOS 7安装 Nginx #### 方法一:通过官方 Yum 源安装 可以按照以下方法快速完成 Nginx安装: 1. 添加 Nginx 到 Yum 源 使用命令 `sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm` 将 Nginx 官方仓库添加到系统的 Yum 源列表中[^2]。 2. 安装 Nginx 执行命令 `sudo yum install -y nginx` 来安装 Nginx。Yum 工具会自动解析依赖关系并完成软件包的下载与安装。 3. 启动 Nginx 服务 使用命令 `sudo systemctl start nginx` 启动 Nginx 服务。 4. 设置开机自启 如果希望系统重启后仍然运行 Nginx,执行命令 `sudo systemctl enable nginx` 开启开机自启动功能。 5. 查看 Nginx 运行状态 可以通过命令 `sudo systemctl status nginx` 验证 Nginx 是否正常运行。 --- #### 方法二:手动编译安装 如果需要更灵活的配置选项或者特定版本支持,则可以选择手动编译的方式安装 Nginx。 1. 下载指定版本的 Nginx 压缩包 访问官网获取最新稳定版链接,例如 `wget -c https://nginx.org/download/nginx-1.21.5.tar.gz` 下载所需版本的源码文件[^3]。 2. 创建目标目录并将压缩包移入其中 创建 `/usr/local/nginx` 文件夹作为工作路径,并将下载好的 `.tar.gz` 文件复制至此处[^4]。 3. 解压源码包 使用命令 `tar -zxvf nginx-1.22.1.tar.gz` 对压缩包进行解压操作。 4. 编译与安装 转至解压后的目录,运行如下脚本完成环境检测、模块加载以及最终部署过程: ```bash ./configure --prefix=/usr/local/nginx && make && sudo make install ``` 5. 测试页面访问确认成功与否 --- #### 注意事项 无论采用哪种方式,在实际应用前都需要确保防火墙规则允许 HTTP 和 HTTPS 请求通行;另外还需调整 SELinux 状态以免其干扰程序行为。 ```python # 示例 Python 脚本用于验证网络连通性 (可选) import requests def check_nginx(): url = 'http://localhost/' try: response = requests.get(url, timeout=5) if response.status_code == 200 and '<title>Welcome to nginx!</title>' in response.text: print('Nginx is running correctly.') else: print('Unexpected content from server.') except Exception as e: print(f'Error occurred: {e}') check_nginx() ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值