Linux CentOS 7.6安装nginx详细保姆级教程

一、通过wget下载nginx压缩包

1、进入home文件并创建nginx文件夹用来存放nginx压缩包

cd /home  //进入home文件夹
mkdir nginx //创建nginx文件夹
cd nginx  //进入nginx文件夹

Linux CentOS 7.6安装nginx详细保姆级教程_第1张图片
2、下载nginx,我这里下载的是Nginx 1.24.0版本,如果要下载新版本可以去官网进行下载:https://nginx.org/en/download.html
Linux CentOS 7.6安装nginx详细保姆级教程_第2张图片
wget下载命令:

wget https://nginx.org/download/nginx-1.24.0.tar.gz

Linux CentOS 7.6安装nginx详细保姆级教程_第3张图片
3、解压文件

tar -zxvf nginx-1.24.0.tar.gz

在这里插入图片描述
4、编译和安装

//进入到 Nginx 解压目录
cd nginx-1.24.0
//编译前的配置和依赖检查
./configure

Linux CentOS 7.6安装nginx详细保姆级教程_第4张图片

//编译
make
//安装
make install

Linux CentOS 7.6安装nginx详细保姆级教程_第5张图片
Linux CentOS 7.6安装nginx详细保姆级教程_第6张图片
Nginx安装完成后,默认自动创建 /usr/local/nginx 目录

三、启动nginx

1、防火墙开启80端口并重启防火墙

//打开80端口
firewall-cmd --zone=public --add-port=80/tcp --permanent 
//重启防火墙
firewalld-cmd --reload

在这里插入图片描述
或者直接关闭防火墙

//查看防火墙状态
systemctl status firewalld
//关闭防火墙
systemctl stop firewalld
//开启防火墙
systemctl start firewalld
//开机禁用防火墙
systemctl disable firewalld

Linux CentOS 7.6安装nginx详细保姆级教程_第7张图片

2、进入Nginx的安装目录

cd /usr/local/nginx/sbin

在这里插入图片描述
3、启动Nginx

./nginx

在这里插入图片描述
4、在浏览器输入服务器ip查看是否安装成功
Linux CentOS 7.6安装nginx详细保姆级教程_第8张图片

4、关闭nginx服务

//检查nginx启动状态
ps -ef|grep nginx
//停止nginx服务
./nginx -s stop
//重启nginx
./nginx -s reload

Linux CentOS 7.6安装nginx详细保姆级教程_第9张图片

5、设置开机自动启动

1、在linux系统的/etc/init.d/目录下创建nginx文件

vim /etc/init.d/nginx

Linux CentOS 7.6安装nginx详细保姆级教程_第10张图片

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
#              It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
#以下路径为你nginx安装目录
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
   echo "nginx already running...."
   exit 1
fi
   echo -n $"Starting $prog: "
   daemon $nginxd -c ${nginx_config}
   RETVAL=$?
   echo
   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
   return $RETVAL
}
# Stop nginx daemons functions.
stop() {
        echo -n $"Stopping $prog: "
        killproc $nginxd
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
    echo -n $"Reloading $prog: "
    #kill -HUP `cat ${nginx_pid}`
    killproc $nginxd -HUP
    RETVAL=$?
    echo
}
# See how we were called.
case "$1" in
start)
        start
        ;;
stop)
        stop
        ;;
reload)
        reload
        ;;
restart)
        stop
        start
        ;;
status)
        status $prog
        RETVAL=$?
        ;;
*)
        echo $"Usage: $prog {start|stop|restart|reload|status|help}"
        exit 1
esac
exit $RETVAL

输入i进行粘贴后,再点击键盘esc按钮退出后,再输入:wq!强制保存并退出
Linux CentOS 7.6安装nginx详细保姆级教程_第11张图片
2、设置文件的访问权限:

//(a+x参数表示 ==> all user can execute  所有用户可执行)
chmod a+x /etc/init.d/nginx 

在这里插入图片描述
3、将ngix加入到rc.local文件中,这样开机的时候nginx就默认启动了

vi /etc/rc.local
/etc/init.d/nginx start

Linux CentOS 7.6安装nginx详细保姆级教程_第12张图片

下次重启就会生效,实现nginx的自启动。

你可能感兴趣的:(linux,centos,nginx)