【CNMP系列】CentOS7.0下安装Nginx服务

时间:2023-03-09 05:44:54
【CNMP系列】CentOS7.0下安装Nginx服务

话步前言,CNMP之路,系统起步:http://www.cnblogs.com/riverdubu/p/6425028.html

这回我来讲解下CentOS7.0下如何安装和配置Nginx服务

Nginx的历史不在此赘述,轻量,快是它的特性。只是因为现在的模块没有达到apache的模块数量级,未来有超越apache的势头。

首先,我们要安装个必要的软件(上节提到过,可能有人并未安装)

#yum install wget

因为Nginx以来与gcc的编译环境,所以,在mini centos中需要安装编译环境来使Nginx能够编译起来。

#yum install gcc-c++

Nginx的http模块需要使用pcre来解析正则表达式。

#yum -y install pcre pcre-devel

依赖的解压包

#yum -y install zlib zlib-devel

openssl安装,Nginx提供http和https协议,https是大势所趋,坑爹的微信小程序需要支持https,其实https也不难,只需要配置下即可,关键是证书麻烦,建议大家去startssl申请证书,免费,只是需要的材料较多,以后有机会给大伙写一篇关于申请ssl证书的博文。

#yum install -y openssl openssl-devel

好了,现在就轮到主角登场啦!!!

下载Nginx源码

先去Nginx官网查看最新版的Nginx源码地址:

https://nginx.org/en/download.html

作为新手,还是下载stable version比较保险,下载前最好将下载目录定位到自己新建的目录中去。

https://nginx.org/download/nginx-1.10.3.tar.gz

#wget -c https://nginx.org/download/nginx-1.10.3.tar.gz

下面开始对其解压

#tar -zxvf nginx-1.10.3.tar.gz

进入Nginx目录

#cd nginx-1.10.3

这里就是Nginx的所有源码啦,感兴趣的朋友可以先对源码多了解,后续我会针对源码推出一系列博文。

好了,下面就要对Nginx的源码进行编译啦!编译命令三剑客登场!!!

#./configure

creating objs/Makefile这步之后,就成功啦!

开始编译

#make

#make install

OK,所有的工作都已经做完啦,下面开始启动Nginx服务并在远程测试,想想是不是很激动。

一般编译安装完的软件都会放在/usr里,这不是user,这是Unix System Resource,是Unix系统资源的缩写。

我们在/user/local/里面发现了nginx,进入。

#cd /usr/local/nginx/

如果你找不到,试试这条命令吧

#whereis nginx

它会告诉你nginx在哪,nginx的命令在/usr/local/nginx/sbin目录下

对于nginx的启动,停止,我简单的列举下

./nginx
./nginx -s stop
./nginx -s quit
./nginx -s reload

./nginx -s quit:此方式停止步骤是待nginx进程处理任务完毕进行停止。
./nginx -s stop:此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。

查询nginx进程:

ps aux|grep nginx

root      23045  0.0  0.0  24468   764 ?        Ss   23:02   0:00 nginx: master process sbin/nginx

nobody    23046  0.0  0.1  24888  1232 ?        S    23:02   0:00 nginx: worker process

看到这两条进程状态,你成功了。PS:grep是筛选,|是管道,Linux里筛选的常用方式。

现在,在你的浏览器中输入你远端服务器的ip,看看是否有Nginx欢迎你的字样。

如果没有,关闭CentOS的防火墙试试。PS:防火墙关闭之后注意配置iptables

CentOS7.0以上默认firewall为防火墙配置,我们这里改为iptables配置。

停止firewall

#systemctl stop firewalld.service

禁止firewall开机启动

#systemctl disable firewalld.service

查看默认防火墙状态(关闭后显示not running,开启后显示running)

#firewall-cmd --state

配置iptables,首先需要安装iptables服务

#yum install iptables-services

编辑防火墙配置文件

#vim /etc/sysconfig/iptables

加入下面的几行,22是默认存在的

-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -jACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp --dport 8080-j ACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT

vim里面是直接yy然后p的,不懂的朋友去看下vim编辑器的基本操作哈,里面有具体的详情。vim里面撤销编辑是回到初始页面,就是按esc,然后点击u即可。

22端口是供ssh访问的,80,8080端口是http服务访问的,以后用到https,也需要打开443端口的访问权限。

保存,重启iptables服务

最后重启防火墙使配置生效

#systemctl restart iptables.service

设置防火墙开机启动

#systemctl enable iptables.service

再次访问远程服务器的ip,是不是有Nginx欢迎你的页面了?

好了,这节就先说到这里,关于Nginx服务器的配置,我们下节再说。 ^_^

重启之后firewall又被打开,所以我们要设置禁止firewall开机自启动

停止firewall

#systemctl stop firewalld.service

禁止firewall开机启动

#systemctl disable firewalld.service

nginx服务未被加入到开机自启动列表,重启服务器后,未发现nginx服务,我们需要手动加入开机自启动

第一步,添加一个新文件,nginx.service

#vim /lib/systemd/system/nginx.service

输入以下内容

[Unit]
Description=nginx
After=network.target [Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true [Install]
WantedBy=multi-user.target

更改文件权限

#chmod 745 /lib/systemd/system/nginx.service

设置开机自启动

#systemctl enable nginx.service

赶紧开机试试看吧!

注2

设置简便的开启和关闭nginx服务

编辑init.d启动服务文件

#vim /etc/init.d/nginx

输入一下内容

 #!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0. version.
# chkconfig: -
# 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
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=
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
[ -x $nginxd ] || exit
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = ] && 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
esac
exit $RETVAL

保存退出

修改该文件权限

#chmod a+x /etc/init.d/nginx

现在就可以开心的使用nginx服务啦

开启:/etc/init.d/nginx start

关闭:/etc/init.d/nginx stop

状态:/etc/init.d/nginx status

重启:/etc/init.d/nginx restart

至此,所有nginx安装相关完毕,有问题留言哈,有留必回。