一、通过wget下载nginx压缩包
1、进入home文件并创建nginx文件夹用来存放nginx压缩包
cd /home //进入home文件夹
mkdir nginx //创建nginx文件夹
cd nginx //进入nginx文件夹
2、下载nginx,我这里下载的是Nginx 1.24.0版本,如果要下载新版本可以去官网进行下载:https://nginx.org/en/download.html
wget下载命令:
wget https://nginx.org/download/nginx-1.24.0.tar.gz
3、解压文件
tar -zxvf nginx-1.24.0.tar.gz
4、编译和安装
//进入到 Nginx 解压目录
cd nginx-1.24.0
//编译前的配置和依赖检查
./configure
//编译
make
//安装
make install
Nginx安装完成后,默认自动创建 /usr/local/nginx 目录
三、启动nginx
1、防火墙开启80端口并重启防火墙
//打开80端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
//重启防火墙
firewalld-cmd --reload
或者直接关闭防火墙
//查看防火墙状态
systemctl status firewalld
//关闭防火墙
systemctl stop firewalld
//开启防火墙
systemctl start firewalld
//开机禁用防火墙
systemctl disable firewalld
2、进入Nginx的安装目录
cd /usr/local/nginx/sbin
3、启动Nginx
./nginx
4、在浏览器输入服务器ip查看是否安装成功
4、关闭nginx服务
//检查nginx启动状态
ps -ef|grep nginx
//停止nginx服务
./nginx -s stop
//重启nginx
./nginx -s reload
5、设置开机自动启动
1、在linux系统的/etc/init.d/目录下创建nginx文件
vim /etc/init.d/nginx
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
#以下路径为你nginx安装目录
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
输入i进行粘贴后,再点击键盘esc按钮退出后,再输入:wq!强制保存并退出
2、设置文件的访问权限:
//(a+x参数表示 ==> all user can execute 所有用户可执行)
chmod a+x /etc/init.d/nginx
3、将ngix加入到rc.local文件中,这样开机的时候nginx就默认启动了
vi /etc/rc.local
/etc/init.d/nginx start
下次重启就会生效,实现nginx的自启动。