Linux 安装Nginx详细教程(简洁适用)

Linux 安装Nginx详细教程(简洁适用)

1、常用命令

 启动Nginx服务:[root@localhost ~]# /usr/local/nginx/sbin/nginx
 重启Nginx服务:[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
 停止Nginx服务:[root@localhost ~]# /usr/local/nginx/sbin/nginx -s stop
 停止Nginx服务:[root@localhost ~]# pkill -9 nginx
 查看Nginx服务:[root@localhost ~]# ps -ef | grep nginx
 查看Nginx端口:[root@localhost ~]# netstat -anpl | grep 'nginx'

2、安装相关依赖

(1)安装openssl 、zlib 、gcc 、pcre依赖
[root@localhost ~]# yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel

3、安装nginx

(1)进入到cd/usr/local/ 目录下
[root@localhost ~]# cd /usr/local/
(2)下载nginx-1.18.0.tar.gz压缩包
[root@localhost local]# wget -c https://nginx.org/download/nginx-1.18.0.tar.gz
(3)解压
[root@localhost local]# tar -xvf nginx-1.18.0.tar.gz
(4)进入nginx-1.18.0目录
[root@localhost local]# cd nginx-1.18.0/
(5)./configure检测编译器、库文件和其他必要的组件是否存在。(默认回在/usr/local/下生产 nginx 文件夹)
[root@localhost nginx-1.18.0]# ./configure 
(6)编译&安装
[root@localhost nginx-1.18.0]# make && make install

此时在/usr/local/nginx下将会生成conf、sbin等文件夹。

4、查看nginx版本

[root@localhost nginx]# nginx -v
nginx version: nginx/1.20.1
(1)如下报错需要配置
[root@localhost nginx]# nginx -v

-bash: nginx: command not found

如果出现-bash: nginx: command not found需要我们手动配置环境变量,出现不需要配置

a. 添加export PATH=$PATH:/usr/local/nginx/sbin配置文件到/etc/profile

echo "export PATH=$PATH:/usr/local/nginx/sbin"  >>  /etc/profile

b. 刷新配置文件

[root@localhost local]# source /etc/profile

5、启动nginx及常用命令

(1)启动 nginx
[root@localhost nginx-1.18.0]# /usr/local/nginx/sbin/./nginx
(2)停止 nginx
[root@localhost nginx-1.18.0]# /usr/local/nginx/sbin/./nginx -s stop
(3)重启 nginx
[root@localhost nginx-1.18.0]# /usr/local/nginx/sbin/./nginx -s reload

6、查看nginx进程

[root@localhost sbin]# ps -ef|grep nginx
root     120657      1  0 13:54 ?        00:00:00 nginx: master process ./nginx
nobody   120659 120657  0 13:54 ?        00:00:00 nginx: worker process
root     120666   3367  0 13:54 pts/0    00:00:00 grep --color=auto nginx

7、设置开机自启动

(1)进入到/lib/systemd/system/目录
[root@localhost local]# cd /lib/systemd/system/
(2)创建nginx.service文件
[root@localhost system]# vim nginx.service
(3)添加到开机启动
[root@localhost system]# systemctl enable nginx

8、关闭虚拟机服务器防火墙并开放访问的端口号,80 端口

(1)查看开放的端口号:firewall-cmd --list-all 默认是没有端口开放的
[root@localhost local]# firewall-cmd --list-all
(2)设置开放的端口号:nginx默认端口是80
[root@localhost local]# firewall-cmd --add-port=80/tcp --permanent
(3)相关命令
firewall-cmd --remove-port=80/tcp --permanent:关闭端口
systemctl start firewalld.service :开启防火墙
systemctl status firewalld:查看FirewallD防火墙状态
systemctl stop firewalld :暂时关闭防火墙
systemctl disable firewalld :永久关闭防火墙
(4)执行完开放端口后,要重新加载防火墙
[root@localhost local]# firewall-cmd --reload

9、验证远程访问nginx是否成功

浏览器输入:http://47.115.20.67/(输入IP),进入nginx主页。
nginx主页,标识安装成功

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值