Centos7服务器下Nginx安装教程,centos7nginx


1、下载安装 gcc

由于 Nginx 使用 C 语言开发,所以官网下载的源码需要进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装。

[root@CentOS ~]# yum -y install gcc
2、下载安装其他第三方库

由于Nginx 的一些模块需要依赖其他第三方库,通常有 pcre 库(支持 rewrite 模块)、zlib 库(支持 gzip 模块)、和openssl(支持 ssl 模块)等。

[root@CentOS ~]# yum -y install pcre pcre-devel zlib zlib-devel openssl openssl-devel
3、下载安装包
[root@CentOS local]# wget http://nginx.org/download/nginx-1.12.2.tar.gz
4、解压安装包
[root@CentOS local]# tar -zxvf nginx-1.12.2.tar.gz 
nginx-1.12.2/
nginx-1.12.2/auto/
nginx-1.12.2/conf/
nginx-1.12.2/contrib/
nginx-1.12.2/src/
nginx-1.12.2/configure
nginx-1.12.2/LICENSE
nginx-1.12.2/README
nginx-1.12.2/html/
nginx-1.12.2/man/
......(略去内容)......
5、创建名为 nginx 的目录,用于保存编译安装后的文件
[root@CentOS local]# mkdir nginx
[root@CentOS local]# ll
总用量 960
drwxr-xr-x.  2 root  root       6 2月  24 16:18 bin
drwxr-xr-x.  2 root  root       6 11月  5 2016 etc
drwxr-xr-x.  2 root  root       6 11月  5 2016 games
drwxr-xr-x.  2 root  root       6 11月  5 2016 include
drwxr-xr-x.  2 root  root       6 11月  5 2016 lib
drwxr-xr-x.  2 root  root       6 11月  5 2016 lib64
drwxr-xr-x.  2 root  root       6 11月  5 2016 libexec
drwxr-xr-x. 10 root  root     141 2月  24 19:03 mysql
drwxr-xr-x.  6 root  root      54 3月  10 00:54 nginx????????????# 这里是刚创建的 nginx 目录
drwxr-xr-x.  9 mysql mysql    186 3月  10 00:53 nginx-1.12.2???? # 这里是解压后的目录
-rw-r--r--.  1 root  root  981687 10月 17 21:20 nginx-1.12.2.tar.gz
drwxr-xr-x.  2 root  root       6 11月  5 2016 sbin
drwxr-xr-x.  5 root  root      49 1月  30 14:30 share
drwxr-xr-x.  2 root  root       6 11月  5 2016 src
6、生成Makefile
[root@CentOS local]# cd nginx-1.12.2/
[root@CentOS nginx-1.12.2]# ./configure --prefix=/usr/local/nginx

--prefix 用于指定编译安装的目录,即 /usr/local/nginx。另外,我们可以看到在 /usr/local/nginx-1.12.2 目录下新生成的 Makefile 文件。

[root@CentOS nginx-1.12.2]# ll
总用量 708
drwxr-xr-x. 6 mysql mysql   4096 3月  10 00:52 auto
-rw-r--r--. 1 mysql mysql 278202 10月 17 21:16 CHANGES
-rw-r--r--. 1 mysql mysql 423948 10月 17 21:16 CHANGES.ru
drwxr-xr-x. 2 mysql mysql    168 3月  10 00:52 conf
-rwxr-xr-x. 1 mysql mysql   2481 10月 17 21:16 configure
drwxr-xr-x. 4 mysql mysql     72 3月  10 00:52 contrib
drwxr-xr-x. 2 mysql mysql     40 3月  10 00:52 html
-rw-r--r--. 1 mysql mysql   1397 10月 17 21:16 LICENSE
-rw-r--r--. 1 root  root     376 3月  10 00:54 Makefile????# 这里是刚生成的 Makefile 文件
drwxr-xr-x. 2 mysql mysql     21 3月  10 00:52 man
drwxr-xr-x. 3 root  root     174 3月  10 00:54 objs
-rw-r--r--. 1 mysql mysql     49 10月 17 21:16 README
drwxr-xr-x. 9 mysql mysql     91 3月  10 00:52 src
[root@CentOS nginx-1.12.2]# 
7、编译并安装
[root@CentOS nginx-1.12.2]# make && make install
8、查看安装后的文件
[root@CentOS local]# cd nginx
[root@CentOS nginx]# ll
总用量 4
drwxr-xr-x. 2 root root 4096 3月  10 00:54 conf
drwxr-xr-x. 2 root root   40 3月  10 00:54 html
drwxr-xr-x. 2 root root    6 3月  10 00:54 logs
drwxr-xr-x. 2 root root   19 3月  10 00:54 sbin
[root@CentOS nginx]# 

如上所示,Nginx 安装目录中主要包括了conf、html、logs和 sbin 等 4 个目录。

9、启动Nginx
[root@CentOS nginx]# ll
总用量 4
drwxr-xr-x. 2 root root 4096 3月  10 00:54 conf
drwxr-xr-x. 2 root root   40 3月  10 00:54 html
drwxr-xr-x. 2 root root    6 3月  10 00:54 logs
drwxr-xr-x. 2 root root   19 3月  10 00:54 sbin
[root@CentOS nginx]# ./sbin/nginx ????????# 启动 Nginx
[root@CentOS nginx]# ps -ef | grep nginx????# 确认 Nginx 进程是否已存在
root      13511      1  0 01:13 ?        00:00:00 nginx: master process ./sbin/nginx
nobody    13513  13511  0 01:13 ?        00:00:00 nginx: worker process
root      13517   3494  0 01:13 pts/0    00:00:00 grep --color=auto nginx
[root@CentOS nginx]# 
10、在 /etc/init.d 目录下创建 nginx 脚本
[root@CentOS init.d]# touch nginx
[root@CentOS init.d]# chmod u+x nginx????# 给脚本添加用户执行权限
[root@CentOS init.d]# ll
总用量 48
-rw-r--r--. 1 root root 15131 9月  12 2016 functions
-rwxr-xr-x. 1 root root 10576 2月  24 18:57 mysql
-rwxr-xr-x. 1 root root  2989 9月  12 2016 netconsole
-rwxr-xr-x. 1 root root  6643 9月  12 2016 network
-rwxr--r--. 1 root root     0 3月  10 01:55 nginx????# 可以看到刚创建的脚本 nginx
-rw-r--r--. 1 root root  1160 11月  7 2016 README
-rwxr--r--. 1 root root   450 3月   2 22:50 svnd
[root@CentOS init.d]# 
11、编辑后的 nginx 脚本内容如下
#!/bin/bash
# chkconfig: - 85 15
PATH=/usr/local/nginx
DESC="nginx daemon"
NAME=nginx
DAEMON=$PATH/sbin/$NAME
CONFIGFILE=$PATH/conf/$NAME.conf
PIDFILE=$PATH/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start() {
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}
do_stop() {
$DAEMON -s stop || echo -n "nginx not running"
}
do_reload() {
$DAEMON -s reload || echo -n "nginx can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0

需要注意的是,以上的相关配置信息可根据自己安装Nginx的位置作相应修改。

12、启动或停止 nginx 服务
[root@CentOS init.d]# service nginx start
Starting nginx daemon: nginx.
[root@CentOS init.d]# ps -ef | grep nginx????# 确认 nginx 服务已启动
root      14696      1  0 02:01 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody    14699  14696  0 02:01 ?        00:00:00 nginx: worker process
root      14702   3494  0 02:02 pts/0    00:00:00 grep --color=auto nginx
[root@CentOS init.d]# 
[root@CentOS init.d]# service nginx stop
Stopping nginx daemon: nginx.
[root@CentOS init.d]# ps -ef | grep nginx????# 确认 nginx 服务已停止
root      14734   3494  0 02:02 pts/0    00:00:00 grep --color=auto nginx
[root@CentOS init.d]# 
13、将 nginx 服务添加到系统服务,并设置为开机启动
[root@CentOS init.d]# chkconfig --add nginx
[root@CentOS init.d]# chkconfig nginx on
注意:正在将请求转发到“systemctl enable nginx.service”。
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@CentOS init.d]# 

相关内容