在cent下安装软件的方式大多用yum或者源码安装。yum安装属于在线安装的方式,安装简单不易出错;源码包安装是先将Nginx的源码下载下来,在自己的系统里编译生成可执行文件,然后执行,好处是:因为是在自己的系统上编译的,更符合自己系统的性能,也就是说在自己的系统上执行Nginx服务性能效率更好。
yum在线安装会将Nginx安装在不同的位置,可以通过命令:
rpm -ql nginx
来查看安装路径,会显示很多路径:
➜ ~ rpm -ql nginx
/etc/logrotate.d/nginx
/etc/nginx
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
/etc/nginx/conf.d/ssl.conf
/etc/nginx/conf.d/virtual.conf
/etc/nginx/fastcgi.conf
/etc/nginx/fastcgi.conf.default
/etc/nginx/fastcgi_params
/etc/nginx/fastcgi_params.default
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/mime.types.default
/etc/nginx/nginx.conf
/etc/nginx/nginx.conf.default
/etc/nginx/scgi_params
/etc/nginx/scgi_params.default
/etc/nginx/uwsgi_params
/etc/nginx/uwsgi_params.default
/etc/nginx/win-utf
/etc/rc.d/init.d/nginx
/etc/sysconfig/nginx
/usr/lib/perl5/vendor_perl/auto/nginx
/usr/lib/perl5/vendor_perl/auto/nginx/nginx.so
/usr/lib/perl5/vendor_perl/nginx.pm
/usr/sbin/nginx
/usr/share/doc/nginx-1.0.15
/usr/share/doc/nginx-1.0.15/CHANGES
/usr/share/doc/nginx-1.0.15/LICENSE
/usr/share/doc/nginx-1.0.15/README
/usr/share/man/man3/nginx.3pm.gz
/usr/share/man/man8/nginx.8.gz
/usr/share/nginx/html/404.html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/usr/share/nginx/html/nginx-logo.png
/usr/share/nginx/html/poweredby.png
/usr/share/vim/vimfiles/ftdetect/nginx.vim
/usr/share/vim/vimfiles/indent/nginx.vim
/usr/share/vim/vimfiles/syntax/nginx.vim
/var/lib/nginx
/var/lib/nginx/tmp
/var/log/nginx
卸载的时候只能通过命令来卸载,因为手动删除这些文件有点不太实际,所以要通过命令:
rpm -e nginx
来卸载,如果因为依赖包的关系报错,就尝试用:
rpm -e --nodeps nginx
来强制卸载,忽略依赖问题。
而源码包安装的文件都在同一个目录下,要卸载的时候将文件夹删除就可以了,通常在 /usr/local/包名 目录下。
yum安装的Nginx可以通过service系统服务命令来开启/关闭:
service nginx start #启动 nginx 服务
service nginx stop #停止 nginx 服务
service nginx restart #重启 nginx 服务
而源码包安装的Nginx启动的时候要在安装目录下的sbin目录中启动Nginx服务:
➜ ~ /usr/local/nginx/sbin/nginx #启动 nginx 服务
➜ ~ /usr/local/nginx/sbin/nginx -s stop #停止 nginx 服务
或者直接进入目录,执行 ./nginx 也可以打开Nginx服务:
➜ ~ cd /usr/local/nginx/sbin
➜ sbin ls
nginx
➜ sbin ./nginx #启动 nginx 服务
➜ sbin ./nginx -s stop #停止 nginx 服务
使用 yum -y install nginx 进行安装:
➜ ~ yum -y install nginx
已加载插件:fastestmirror, security
设置安装进程
Loading mirror speeds from cached hostfile
解决依赖关系
--> 执行事务检查
---> Package nginx.i686 0:1.0.15-12.el6 will be 安装
--> 完成依赖关系计算
依赖关系解决
========================================================================================================================================
软件包 架构 版本 仓库 大小
========================================================================================================================================
正在安装:
nginx i686 1.0.15-12.el6 epel 410 k
事务概要
========================================================================================================================================
Install 1 Package(s)
总下载量:410 k
Installed size: 1.1 M
下载软件包:
nginx-1.0.15-12.el6.i686.rpm | 410 kB 00:00
运行 rpm_check_debug
执行事务测试
事务测试成功
执行事务
Warning: RPMDB altered outside of yum.
正在安装 : nginx-1.0.15-12.el6.i686 1/1
Verifying : nginx-1.0.15-12.el6.i686 1/1
已安装:
nginx.i686 0:1.0.15-12.el6
完毕!
启动Nginx服务器:
➜ ~ service nginx start
Starting nginx: [ OK ]
停止Nginx服务器:
➜ ~ service nginx stop
Stopping nginx: [ OK ]
重启Nginx服务器:
➜ ~ service nginx restart
Stopping nginx: [ OK ]
Starting nginx: [ OK ]
启动后,在浏览器输入服务器的IP地址就能看到Nginx服务首页了!
首先要进行安装编译需要的依赖包(我用的CentOS 6.7):
yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel
下载源码包到 /root 目录下:
wget http://nginx.org/download/nginx-1.10.1.tar.gz
解压缩:
tar -zxvf nginx-1.10.1.tar.gz
解压缩后,进入 nginx-1.10.1 目录下
➜ ~ ls
anaconda-ks.cfg install.log install.log.syslog nginx-1.10.1 nginx-1.10.1.tar.gz
➜ ~ cd nginx-1.10.1
在目录下执行配置和make命令:
➜ nginx-1.10.1 ./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre
➜ nginx-1.10.1 make && make install
启动 停止 Nginx服务:
➜ ~ /usr/local/webserver/nginx/sbin/nginx #启动 nginx 服务
➜ ~ /usr/local/webserver/nginx/sbin/nginx -s stop #停止 nginx 服务
新建文件 /etc/init.d/nginx:
vim /etc/init.d/nginx
添加以下内容:
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
修改权限:
chmod 755 /etc/init.d/nginx
开机启动:
chkconfig nginx on
添加为系统服务后,启动等命令有点变化:
启动|关闭|重启|重载|服务状态|文件测试: service nginx start|stop|restart|reload|status|configtest