一、准备 nginx 和 依赖环境的包。
链接:pan.baidu.com/s/1UbL-wT2m… 提取码:9999
百度网盘自行提取即可。
二、安装 nginx。
1、上传安装包到 /usr/local/nginx 下。
2、安装 gcc
cd /usr/local/nginx/gcc
rpm -Uvh *.rpm --nodeps --force
检测 gcc 是否安装成功
gcc -v
如上图所示,代表安装成功!
3、安装 g++
切换目录,执行命令安装 rpm 包
cd /usr/local/nginx/gcc-c++/
rpm -Uvh *.rpm --nodeps --force
安装完毕如上图所示。
检测 g++ 版本。
g++ -v
最后一行可以看到版本号。
4、安装 pcre ,先解压 (pcre-8.35.tar.gz) 再安装。
切换目录到 nginx 下
cd ..
解压压缩包
tar -zxvf pcre-8.35.tar.gz
开始安装
cd pcre-8.35
./configure
make
make install
5、安装 libtool
切换目录
cd /usr/local/nginx/
解压 libtool-2.4.2.tar.gz
tar -zxvf libtool-2.4.2.tar.gz
cd libtool-2.4.2/
./configure
make
make install
6、安装 nginx 。
切换目录
cd /usr/local/nginx/
解压 nginx
tar -zxvf nginx-1.14.0.tar.gz
cd nginx-1.14.0/
./configure
上面命令是默认的,下面是带参数
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
注:configure,这一步一般用来生成 Makefile,为下一步的编译做准备,你可以通过在 configure 后加上参数来对安装进行控制。
make
make install
7、启动 nginx 服务。
nginx 安装目录地址 -c nginx配置文件地址
注:这里如果按照我的方式安装完,会看到如下目录
会看到在 /usr/local/nginx 下多了几个目录。
其实这里默认安装 nginx 的目录就是在 /usr/local/nginx
| 命令 | 操作内容 |
|---|---|
| /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf | 启动nginx |
| /usr/local/nginx/sbin/nginx -s stop (quit) | 停止nginx |
| /usr/local/nginx/sbin/nginx -s reload | 重启nginx |
| netstat -tunlp | 查看端口占用 |
| netstat -tunlp | grep |
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
curl http://127.0.0.1:80
看到上面输出内容,说明 nginx 已经启动成功!
三、设置其他启动方式。
1、注册 nginx 为服务。
1)创建服务脚本
vi /etc/init.d/nginx
脚本内容如下:
#! /bin/sh
# chkconfig: - 85 15
PATH=/usr/local/nginx/sbin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/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
2)添加服务并授权执行权限给脚本
sudo chkconfig --add nginx
sudo chmod a+x /etc/init.d/nginx
3)添加开机自启动
chkconfig nginx on
4)服务启动 | 停止 | 重启 命令 启动 nginx:
service nginx start
停止 nginx:
service nginx stop
重启 nginx:
service nginx restart
修改配置文件后,重载 nginx 服务
service nginx reload
至此,我们的 nginx 离线安装就完成了。
2、设置为systemctl启动方式
1)创建文件
vi /usr/lib/systemd/system/nginx.service
2)编写脚本内容
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
3)文件设为755权限并使文件生效
chmod 755 /usr/lib/systemd/system/nginx.service
systemctl daemon-reload
4)设置开机启动
systemctl enable nginx.service
6)命令
systemctl is-enabled servicename.service #查询服务是否开机启动
systemctl enable *.service #开机运行服务
systemctl disable *.service #取消开机运行
systemctl start *.service #启动服务
systemctl stop *.service #停止服务
systemctl restart *.service #重启服务
systemctl reload *.service #重新加载服务配置文件
systemctl status *.service #查询服务运行状态
systemctl --failed #显示启动失败的服务