CentOS 下安装并且启动 nginx

安装所需环境

Nginx 是 C语言 开发,建议在 Linux 上运行,当然,也可以安装 Windows 版本,本篇则使用 CentOS 7 作为安装环境。

1.gcc 安装
安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:

yum install gcc-c++

2. PCRE pcre-devel 安装
PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:

yum install -y pcre pcre-devel

3.zlib 安装
zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。

yum install -y zlib zlib-devel

4.OpenSSL 安装
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

yum install -y openssl openssl-devel

官网下载

官网地址: https://nginx.org/en/download.html

1.使用wget命令下载安装包(推荐)

wget -c https://nginx.org/download/nginx-1.13.3.tar.gz

我下载的是1.13.3版本。

2.解压命令

tar -zxvf nginx-1.13.3.tar.gz
cd nginx-1.13.3

3.配置

其实在 nginx-1.13.3 版本中你就不需要去配置相关东西,默认就可以了。当然,如果你要自己配置目录也是可以的。
3.1. 使用默认配置

./configure

3.2. 自定义配置(不推荐)

./configure \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/conf/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

注:将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录

4.编译安装

[root@localhost nginx-1.13.3]# make
[root@localhost nginx-1.13.3]# make install

// 查找安装路径
[root@localhost nginx-1.13.3]# whereis nginx

nginx: /usr/local/nginx

5.启动、停止nginx

[root@localhost nginx-1.13.3]# cd /usr/local/nginx/sbin/

// 启动 nginx
[root@localhost sbin]# ./nginx

// 停止 nginx(此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程)
[root@localhost sbin]# ./nginx -s stop

// 停止 nginx(此方式停止步骤是待nginx进程处理任务完毕进行停止)
[root@localhost sbin]# ./nginx -s quit

// 从新加载配置文件(修改配置文件 nginx.conf 可以使用该命令使配置生效)
[root@localhost sbin]# ./nginx -s reload

// 查询 nginx 进程
[root@localhost sbin]# ps aux | grep nginx

当安装好之后可通过浏览器中输入 IP, 访问欢迎页, 如果其他设备需要访问, 需要配置防火墙信息。

6.开机自启动
在 rc.local 增加启动代码即可。

vi /etc/rc.local

增加一行 /usr/local/nginx/sbin/nginx

设置执行权限 :

chmod 755 rc.local
[root@localhost sbin]# vi /etc/rc.local

#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local
/usr/local/nginx/sbin/nginx # 增加这一行

到这里, nginx 就安装完毕了, 当然也可以添加系统服务。

参考地址: CentOS 7 下安装 Nginx

----------------------------分割线 -----------------------------
Nginx负载均衡配置实战
CentOS 6.2实战部署Nginx+MySQL+PHP
使用Nginx搭建WEB服务器
搭建基于Linux6.3+Nginx1.2+PHP5+MySQL5.5的Web服务器全过程
CentOS 6.3下Nginx性能调优
CentOS 6.3下配置Nginx加载ngx_pagespeed模块
CentOS 6.4安装配置Nginx+Pcre+php-fpm
Nginx安装配置使用详细笔记
Nginx日志过滤 使用ngx_log_if不记录特定日志
----------------------------分割线 -----------------------------

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是在CentOS 7下源码安装Nginx的步骤: 1. 安装必要的依赖包: ``` sudo yum install -y gcc gcc-c++ make automake autoconf libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel ``` 2. 下载并解压Nginx源码包: ``` wget http://nginx.org/download/nginx-1.18.0.tar.gz tar -zxvf nginx-1.18.0.tar.gz cd nginx-1.18.0 ``` 3. 配置编译参数: ``` ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module ``` 以上参数的含义: - `--prefix=/usr/local/nginx`:指定Nginx安装目录为`/usr/local/nginx` - `--with-http_ssl_module`:启用SSL功能 - `--with-http_v2_module`:启用HTTP/2功能 4. 编译安装: ``` make && make install ``` 5. 启动Nginx: ``` /usr/local/nginx/sbin/nginx ``` 6. 验证Nginx是否已经成功启动: 在浏览器中访问服务器IP地址或域名,如果能够看到"Welcome to nginx!"的页面,说明Nginx已经成功安装启动了。 7. 将Nginx设置为系统服务(可选): ``` sudo vi /etc/systemd/system/nginx.service ``` 将以下内容复制到文件中: ``` [Unit] Description=The NGINX HTTP and reverse proxy server After=syslog.target 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 ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target ``` 保存并退出。 使用以下命令重新加载systemd配置: ``` sudo systemctl daemon-reload ``` 使用以下命令启动Nginx服务: ``` sudo systemctl start nginx ``` 使用以下命令设置开机启动: ``` sudo systemctl enable nginx ``` 至此,在CentOS 7下源码安装Nginx完成。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值