Linux 下安装 Nginx

文章中是以Centos7 64 为例

目录

一. 使用yum 安装最新版本

二.   在安装Nginx之前需要查看是否安装某软件

三.   Nginx安装

四.   设置防火墙

五.   配置开启自动启动Nginx



一. 使用yum 安装最新版本

官网介绍的安装步骤URl: http://nginx.org/en/linux_packages.html#stable

1.  在安装之前要设置 yum 存储库,请创建名为 /etc/yum.repos.d/nginx.repo 以下内​​容的文件:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

2.  根据使用的发行版,将“OS”替换为“rhel”或“centos”,将“OSRELEASE”替换为“6”或“7”,表示6.x或7.x,x代表不同版本。

3.  执行如下命令进行yum安装nginx

yum install nginx

sudo yum install nginx

 4.  查看版本

# 查看nginx版本
nginx -v

# 查看编译参数
nginx -V

5. 查看安装目录

rpm -ql nginx

6.  查看配置文件

# 用于日志切割
/etc/logrotate.d/nginx

二.   在安装Nginx之前需要查看是否安装某软件

1、使用rpm包安装的,可以用 rpm -qa 看到,如果要查找某软件包是否安装,用 rpm -qa | grep "软件或者包的名字"。

2、使用deb包安装的,可以用 dpkg -l 看到。如果是查找指定软件包,用 dpkg -l | grep "软件或者包的名字"。

3、使用yum方法安装的,可以用 yum list installed 查找,如果是查找指定包,用 yum list installed | grep "软件名或者包名"。

示例 :查看是否安装了gcc

yum list installed | grep "gcc"

示例:未安装gcc软件

yum -y install gcc

示例:全部安装

yum -y install gcc pcre-devel zlib-devel openssl openssl-devel

三.   Nginx安装

        1. Nginx下载地址:https://nginx.org/download/

        2. 把文件解压到自己安装软件的路径

                /usr/mylocal/myprogram

        3. 安装Nginx    

## 解压
tar -zxvf nginx-1.19.9.tar.gz

##进入nginx目录
cd nginx-1.19.9
## 配置
./configure --prefix=/usr/mylocal/nginx-1.19.9

# make
make
make install

 在执行完make、make install命令,通过下命令检测是否安装成功

# cd到刚才配置的安装路径/usr/mylocal/nginx-1.19.9
./sbin/nginx -t

 错误信息

nginx: [alert] could not open error log file: open() "/usr/mylocal/nginx-1.19.9/logs/error.log" failed (2: No such file or directory)
2016/09/13 19:08:56 [emerg] 6996#0: open() "/usr/mylocal/nginx-1.19.9/logs/access.log" failed (2: No such file or directory)

原因分析:nginx/目录下没有logs文件夹

需要创建日志文件

mkdir logs
chmod 700 logs 

正常情况的信息输出:

nginx: the configuration file /usr/mylocal/nginx-1.19.9/conf/nginx.conf syntax is ok
nginx: configuration file /usr/mylocal/nginx-1.19.9/conf/nginx.conf test is successful 

启动nginx (cd 到自己的Nginx路径下:/usr/mylocal/nginx-1.19.9/sbin)

./nginx //启动nginx 

四.   设置防火墙

        查询端口是否打开

firewall-cmd --query-port=80/tcp 

        开启端口

firewall-cmd --add-port=80/tcp --permanent
#重启防火墙
systemctl restart firewalld 

--permanent  #永久生效,没有此参数重启后失效

五.   配置开启自动启动Nginx

vim /etc/rc.d/rc.local 

在文件中添加 /usr/mylocal/nginx-1.19.9/sbin/nginx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值