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

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

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

Nginx的历史不在此赘述,轻量,快是它的特性。只是由于如今的模块没有达到apache的模块数量级,将来有超越apache的势头。c++

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

#yum install wget正则表达式

 由于Nginx以来与gcc的编译环境,因此,在mini centos中须要安装编译环境来使Nginx可以编译起来。apache

#yum install gcc-c++小程序

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

#yum -y install pcre pcre-devel微信小程序

依赖的解压包centos

#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

输入一下内容

 1 #!/bin/bash
 2 # nginx Startup script for the Nginx HTTP Server
 3 # it is v.0.0.2 version.
 4 # chkconfig: - 85 15
 5 # description: Nginx is a high-performance web and proxy server.
 6 #              It has a lot of features, but it's not for everyone.
 7 # processname: nginx
 8 # pidfile: /var/run/nginx.pid
 9 # config: /usr/local/nginx/conf/nginx.conf
10 nginxd=/usr/local/nginx/sbin/nginx
11 nginx_config=/usr/local/nginx/conf/nginx.conf
12 nginx_pid=/var/run/nginx.pid
13 RETVAL=0
14 prog="nginx"
15 # Source function library.
16 . /etc/rc.d/init.d/functions
17 # Source networking configuration.
18 . /etc/sysconfig/network
19 # Check that networking is up.
20 [ ${NETWORKING} = "no" ] && exit 0
21 [ -x $nginxd ] || exit 0
22 # Start nginx daemons functions.
23 start() {
24 if [ -e $nginx_pid ];then
25    echo "nginx already running...."
26    exit 1
27 fi
28    echo -n $"Starting $prog: "
29    daemon $nginxd -c ${nginx_config}
30    RETVAL=$?
31    echo
32    [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
33    return $RETVAL
34 }
35 # Stop nginx daemons functions.
36 stop() {
37         echo -n $"Stopping $prog: "
38         killproc $nginxd
39         RETVAL=$?
40         echo
41         [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
42 }
43 # reload nginx service functions.
44 reload() {
45     echo -n $"Reloading $prog: "
46     #kill -HUP `cat ${nginx_pid}`
47     killproc $nginxd -HUP
48     RETVAL=$?
49     echo
50 }
51 # See how we were called.
52 case "$1" in
53 start)
54         start
55         ;;
56 stop)
57         stop
58         ;;
59 reload)
60         reload
61         ;;
62 restart)
63         stop
64         start
65         ;;
66 status)
67         status $prog
68         RETVAL=$?
69         ;;
70 *)
71         echo $"Usage: $prog {start|stop|restart|reload|status|help}"
72         exit 1
73 esac
74 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安装相关完毕,有问题留言哈,有留必回。

相关文章
相关标签/搜索