文章目录
1. nginx的安装
nginx的安装有两种方法:
- 基于APT源安装
- 基于源码安装
1.1 基于APT源安装和介绍
1.1.1 APT源安装
sudo apt-get install nginx
通过源安装的方法,其文件夹放置的位置是安装linux的文件规则来的,比如配置文件一般放在/etc,日志一般放在/var/log
1.1.2 文件介绍
- /usr/sbin/nginx:主程序
- /etc/nginx:存放配置文件
- /usr/share/nginx:存放静态文件
- /var/log/nginx:存放日志
我们在使用的过程中,主要是修改nginx的配置文件,每次更改配置文件后可以使用指令,查看配置文件是否有语法错误
sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok │
nginx: configuration file /etc/nginx/nginx.conf test is successful
1.1.3 nginx的启动停止重启
sudo service nginx {start|stop|restart|reload|force-reload|status|configtest|rotate|upgrade}
例子:
- sudo service nginx start //nginx启动
- sudo service nginx stop //nginx停止
- sudo service nginx restart //nginx重启
- sudo service nginx reload //修改配置文件后常用此指令
1.2 基于nginx源码安装
这种方式可以自定安装指定的模块以及最新的版本。方式更灵活。
官方下载页面:http://nginx.org/en/download.html
configure配置文件详解:http://nginx.org/en/docs/configure.html
1.2.1 nginx安装和依赖库的安装
1. 安装gcc g++的依赖库
sudo apt-get update
sudo apt-get install build-essential
sudo apt-get install libtool
sudo apt-get install sysv-rc-conf(如果安装失败,需要添加软件源请参考如下)
sudo vim /etc/apt/sources.list
deb http://archive.ubuntu.com/ubuntu/ trusty main universe restricted multiverse
添加上面源即可找到
2. 安装pcre依赖库(http://www.pcre.org/)
sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev
apt-get install libperl-dev
3. 安装zlib依赖库(http://www.zlib.net)
sudo apt-get install zlib1g-dev
4. 安装SSL依赖库
sudo apt-get install openssl libssl-dev
5. nginx安装
#官网http://nginx.org/download/选择要下载的版本
wget http://nginx.org/download/nginx-1.13.6.tar.gz
#解压:
tar -zxvf nginx-1.13.6.tar.gz
#进入解压目录:
cd nginx-1.13.6
打开vim objs/Makefile
CFLAGS = -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g
中的 -Werror删掉 (-Werror,它要求GCC将所有的警告当成错误进行处理)
#配置,/usr/local/nginx 安装后文件的位置:
./configure --prefix=/usr/local/nginx
#编译:
make
#安装:
sudo make install
#启动:
sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
注意:-c 指定配置文件的路径,不加的话,nginx会自动加载默认路径的配置文件,可以通过-h查看帮助命令。
#查看进程:
ps -ef | grep nginx
# 上面启动需要输入路径太麻烦了
配置软链接
sudo ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
现在就可以不用路径直接输入nginx启动。
1.2.2 配置nginx的开机启动
在/etc/init.d/下创建nginx文件,sudo vim /etc/init.d/nginx,内容如下:
#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
# Author: licess
# website: http://lnmp.org
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/logs/$NAME.pid
case "$1" in
start)
echo -n "Starting $NAME... "
if netstat -tnpl | grep -q nginx;then
echo "$NAME (pid `pidof $NAME`) already running."
exit 1
fi
$NGINX_BIN -c $CONFIGFILE
if [ "$?" != 0 ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;
stop)
echo -n "Stoping $NAME... "
if ! netstat -tnpl | grep -q nginx; then
echo "$NAME is not running."
exit 1
fi
$NGINX_BIN -s stop
if [ "$?" != 0 ] ; then
echo " failed. Use force-quit"
exit 1
else
echo " done"
fi
;;
status)
if netstat -tnpl | grep -q nginx; then
PID=`pidof nginx`
echo "$NAME (pid $PID) is running..."
else
echo "$NAME is stopped"
exit 0
fi
;;
force-quit)
echo -n "Terminating $NAME... "
if ! netstat -tnpl | grep -q nginx; then
echo "$NAME is not running."
exit 1
fi
kill `pidof $NAME`
if [ "$?" != 0 ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;
restart)
$0 stop
sleep 1
$0 start
;;
reload)
echo -n "Reload service $NAME... "
if netstat -tnpl | grep -q nginx; then
$NGINX_BIN -s reload
echo " done"
else
echo "$NAME is not running, can't reload."
exit 1
fi
;;
configtest)
echo -n "Test $NAME configure files... "
$NGINX_BIN -t
;;
*)
echo "Usage: $0 {start|stop|force-quit|restart|reload|status|configtest}"
exit 1
;;
esac
#设置服务脚本有执行权限
sudo chmod +x /etc/init.d/nginx
#注册服务
sysv-rc-conf --list
sysv-rc-conf nginx on
sudo reboot -f,开机后即可看到nginx启动了
而且可以通过
sudo service nginx stop
sudo service nginx start
等上面脚本start|stop|force-quit|restart|reload|status|configtest指令开启关闭重启等nginx