nginx centos7.0-4

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

输入以下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
[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安装相关完毕,有问题留言哈,有留必回。

猜你喜欢

转载自blog.csdn.net/hellolovelife/article/details/78473855
今日推荐