centos下nginx-1.18.0安装

Posted AI程序

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了centos下nginx-1.18.0安装相关的知识,希望对你有一定的参考价值。

1、下载安装包

进入官网(http://nginx.org/en/download.html)下载指定版本的安装包,这里下载的是最新的nginx-1.18.0.tar.gz:

1 http://nginx.org/download/nginx-1.18.0.tar.gz

2、解压压缩文件

将下载下来的压缩文件进行解压安装:

1 [develop@fxb-ah softs]$ tar zxf nginx-1.18.0.tar.gz
2 [develop@fxb-ah softs]$ cd nginx-1.18.0

3、编译安装nginx

3.1 环境的安装

安装nginx之前,需要安装相应的环境,让nginx可以正常的允许:

  • gcc环境:基本运行环境
  • pcre:用于nginx的http模块解析正则表达式
  • zlib:用户进行gzip压缩
  • openssl:用于nginx https协议的传输

直接通过yum进行安装即可:

1 [develp@fxb-ah nginx-1.18.0]$ sudo yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

 3.2 编译安装nginx

 1 [develop@fxb-ah nginx-1.18.0]$ sudo useradd -M -s /sbin/nologin nginx
 2 [develop@fxb-ah nginx-1.18.0]$ sudo ./configure --prefix=/usr/local/nginx --user=nginx
 3 [develop@fxb-ah nginx-1.18.0]$ sudo make && sudo make install
 4 [develop@fxb-ah nginx-1.18.0]$ cd /usr/local/nginx/
 5 [develop@fxb-ah nginx]$ ll
 6 total 16
 7 drwxr-xr-x 2 root root 4096 Jun 30 14:21 conf
 8 drwxr-xr-x 2 root root 4096 Jun 30 14:21 html
 9 drwxr-xr-x 2 root root 4096 Jun 30 14:21 logs
10 drwxr-xr-x 2 root root 4096 Jun 30 14:21 sbin

安装完成!

 4、设置服务

将nginx制作成服务,并设置开机启动(centos7):

 1 [develop@fxb-ah nginx]$ sudo vim /usr/lib/systemd/system/nginx.service
 2 [Unit]
 3 Description=nginx
 4 After=network.target
 5 
 6 [Service]
 7 Type=forking
 8 ExecStart=/usr/local/nginx/sbin/nginx
 9 ExecReload=/usr/local/nginx/sbin/nginx -s reload
10 ExecStop=/usr/local/nginx/sbin/nginx -s quit
11 PrivateTmp=true
12 
13 [Install]
14 WantedBy=multi-user.target
15 [developer@iZuf6ai62xce7yu53epv2jZ redis-6.0.5]$ sudo systemctl daemon-reload
16 [developer@iZuf6ai62xce7yu53epv2jZ redis-6.0.5]$ sudo systemctl start nginx.service
17 [developer@iZuf6ai62xce7yu53epv2jZ redis-6.0.5]$ sudo  systemctl enable nginx.service

 

 低版本centos下服务制作:

 1 [develop@fxb-ah nginx]$ sudo vi /etc/init.d/nginx
 2 #!/bin/bash
 3 # nginx Startup script for the Nginx HTTP Server
 4 # it is v.0.0.2 version.
 5 # chkconfig: - 85 15
 6 # description: Nginx is a high-performance web and proxy server.
 7 # It has a lot of features, but its not for everyone.
 8 # processname: nginx
 9 # pidfile: /var/run/nginx.pid
10 # config: /usr/local/nginx/conf/nginx.conf
11 nginxd=/usr/local/nginx/sbin/nginx
12 nginx_config=/usr/local/nginx/conf/nginx.conf
13 nginx_pid=/var/run/nginx.pid
14 RETVAL=0
15 prog="nginx"
16 # Source function library.
17 . /etc/rc.d/init.d/functions
18 # Source networking configuration.
19 . /etc/sysconfig/network
20 # Check that networking is up.
21 [ ${NETWORKING} = "no" ] && exit 0
22 [ -x $nginxd ] || exit 0
23 # Start nginx daemons functions.
24 start() {
25 if [ -e $nginx_pid ];then
26     echo "nginx already running...."
27     exit 1
28 fi
29     echo -n $"Starting $prog: "
30     daemon $nginxd -c ${nginx_config}
31     RETVAL=$?
32     echo
33     [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
34     return $RETVAL
35 }
36 # Stop nginx daemons functions.
37 stop() {
38     echo -n $"Stopping $prog: "
39     killproc $nginxd
40     RETVAL=$?
41     echo
42     [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
43 }
44 # reload nginx service functions.
45 reload() {
46     echo -n $"Reloading $prog: "
47     #kill -HUP `cat ${nginx_pid}`
48     killproc $nginxd -HUP
49     RETVAL=$?
50     echo
51 }
52 # See how we were called.
53 case "$1" in
54 start)
55         start
56         ;;
57 stop)
58         stop
59         ;;
60 reload)
61         reload
62         ;;
63 restart)
64         stop
65         start
66         ;;
67 status)
68         status $prog
69         RETVAL=$?
70         ;;
71 *)
72         echo $"Usage: $prog {start|stop|restart|reload|status|help}"
73         exit 1
74 esac
75 exit $RETVAL
76 [develop@fxb-ah nginx]$ sudo chmod a+x /etc/init.d/nginx
77 [develop@fxb-ah nginx]$ sudo chkconfig --add /etc/init.d/nginx
78 [develop@fxb-ah nginx]$ service nginx status
79 nginx is stopped
80 [develop@fxb-ah nginx]$ sudo chkconfig nginx on

 

 

 

 

 

------------恢复内容结束------------

以上是关于centos下nginx-1.18.0安装的主要内容,如果未能解决你的问题,请参考以下文章

Linux:CentOS7下安装Nginx的详细步骤

[乐意黎]Centos安装nginx1.18后, 将nginx制作成服务设置为自启动

[乐意黎]Centos安装nginx1.18后, 将nginx制作成服务设置为自启动

RHEL 7.4 下yum安装配置nginx与php

RHEL 7.4 下yum安装配置nginx与php

初识nginx