Linux安装NGINX

#1.1 下载nginx安装包
#进入nginx官网下载自己对应版本的nginx
#下载地址:http://nginx.org/download/
#我这里下载nginx-1.17.4.tar.gz版本(下载地址:http://nginx.org/download/nginx-1.17.4.tar.gz)


#SFTP上传NGINX安装包

cd /backup
ls -lhrt


[root@ZabbixServer backup]# ls -lhrt|grep nginx
-rw------- 1 root root 1011K Sep 29 13:54 nginx-1.17.4.tar.gz
[root@ZabbixServer backup]#  

#1.2 配置nginx需要的环境
#安装gcc用于编译nginx源码包

yum install -y gcc-c++

#安装pcre pcre-devel包
#Nginx的Rewrite模块和HTTP核心模块会使用到PCRE正则表达式语法。这里需要安装两个安装包pcre和pcre-devel。第一个安装包提供编译版本的库,而第二个提供开发阶段的头文件和编译项目的源代码。

yum install -y pcre pcre-devel

#安装zlib
#zlib库提供了开发人员的压缩算法,在Nginx的各种模块中需要使用gzip压缩。

yum install -y zlib zlib-devel

#安装Open SSL
#Nginx不仅支持 http协议,还支持 https(即在 ssl 协议上传输 http),如果使用了 https,需要安装 OpenSSL 库。

yum install -y openssl openssl-devel

#1.3 解压Nginx包

tar -zxf nginx-1.17.4.tar.gz -C /usr/local/src/
cd /usr/local/src/nginx-1.17.4/


[root@ZabbixServer nginx-1.17.4]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@ZabbixServer nginx-1.17.4]# 

#1.4 编译安装

./configure --prefix=/usr/local/nginx


......
creating objs/Makefile

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

[root@ZabbixServer nginx-1.17.4]#

make
make install

#1.5 创建nginx用户、组

cat /etc/group | grep nginx
cat /etc/passwd | grep nginx


#若无记录,则需要创建

groupadd nginx
useradd -r -g nginx nginx
cat /etc/passwd | grep nginx


[root@ZabbixServer logs]# cat /etc/passwd | grep nginx
nginx:x:986:2015::/home/nginx:/bin/bash
[root@ZabbixServer logs]# 

#1.6 修改nginx属组

chown -R nginx:nginx /usr/local/nginx/

#1.7 修改nginx配置

cd /usr/local/nginx/conf
vi nginx.conf


#修改用户及错误日志模式

user  nginx;
worker_processes  1;

error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        logs/nginx.pid;
......

#1.8 启停Nginx
#启动

/usr/local/nginx/sbin/nginx 

#关闭

/usr/local/nginx/sbin/nginx -s quit


#或者

/usr/local/nginx/sbin/nginx -s stop

#重启

/usr/local/nginx/sbin/nginx -s reload

#查看nginx进程

ps aux|grep nginx


[root@ZabbixServer sbin]# ps aux|grep nginx
root     10345  0.0  0.0  20632  1224 ?        Ss   16:24   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx    10348  0.0  0.0  23148  1492 ?        S    16:24   0:00 nginx: worker process
root     10358  0.0  0.0 112648   960 pts/0    S+   16:24   0:00 grep --color=auto nginx
[root@ZabbixServer sbin]# 

#1.9 浏览器验证
#URL中直接输入IP地址,回车。看到Welcome页面则表示nginx初步配置成功


#1.10 设置服务&开机自启动

vi /etc/init.d/nginx


#!/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

nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/usr/local/nginx/logs/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

#注意
#nginxd=/usr/local/nginx/sbin/nginx  #修改成nginx执行程序的路径。
#nginx_config=/usr/local/nginx/conf/nginx.conf #修改成nginx.conf文件的路径。
#nginx_pid=/usr/local/nginx/logs/nginx.pid  #修改成nginx.pid文件的路径
# chkconfig: 345 85 15 (345代表在设置在那个level中是on的,如果一个都不想on,那就写一个横线"-",比如:chkconfig: - 85 15。后面两个数字代表S和K的默认排序号,如在/etc/rc(0~6).d 中的S85tomcat K15tomcat)

#修改文件权限

chmod a+x /etc/init.d/nginx 

#通过systecmctl来启动服务
#创建启动文件

vim /usr/lib/systemd/system/nginx.service


[Unit]
Description=nginx.service
After=network.target remote-fs.target nss-lookup.target  
[Service]
Type=forking
ExecStart=/etc/init.d/nginx start
ExecReload=/etc/init.d/nginx restart 
ExecStop=/etc/init.d/nginx stop/usr/local/nginx/sbin/nginx -s stop 
PrivateTmp=true
[Install]
WantedBy=multi-user.target

#说明
#Description=nginx.service     //描述信息
#Type=forking    //后台运行
#ExecStart=/etc/init.d/nginx start    //启动路径
#PrivateTmp=true    //是否给程序留下独立的空间

#配置为开机启动

systemctl enable nginx.service


 
#至此就可以通过下面指令控制启动停止 

systemctl start nginx
systemctl stop nginx
systemctl restart nginx

#重启机器验证

reboot
ps aux|grep nginx


[root@ZabbixServer ~]# ps aux|grep nginx
root      1060  0.0  0.0  20500   616 ?        Ss   17:51   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx     1069  0.0  0.0  23036  1384 ?        S    17:51   0:00 nginx: worker process
root      3000  0.0  0.0 112652   960 pts/0    S+   17:52   0:00 grep --color=auto nginx
[root@ZabbixServer ~]#

参考文章:

            https://www.cnblogs.com/herui1991/p/8996917.html
            https://blog.csdn.net/qq_42815754/article/details/82980326
            https://blog.csdn.net/chuozongxiao7597/article/details/101055212

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值