Centos7服务器Nginx 1.8.1编译安装过程

Nginx是一个高性能的HTTP和反向代理服务器,其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,特别是在处理静态资源请求的时候。所以,我们一般可以用Nginx服务器来处理静态资源请求和负载均衡。

下面我们开始在Centos7上安装Nginx 1.8.1:

一、安装Nginx前的准备工作:
(1)安装gcc等编译Nginx需要的工具.

yum -y install gcc gcc-c++ autoconf automake

(2)安装prce(重定向支持), zlib(nginx 的gzip模块,传输数据打包,省流量,但消耗资源), openssl(https支持,如果不需要https可以不安装。

yum -y install zlib zlib-devel openssl openssl-devel pcre-devel

二、Nginx编译和安装
(1)下载稳定版的Nginx

wget http://nginx.org/download/nginx-1.8.1.tar.gz

(2)解压和编译

tar -zxvf nginx-1.8.1.tar.gz

进入目录

cd nginx-1.8.1/

编译代码

./configure --prefix=/usr/local/nginx --pid-path=/run/nginx.pid  --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --add-module=/nginx/nginx_upstream_check_module-master --with-http_ssl_module --with-http_spdy_module --with-http_stub_status_module --with-pcre

安装

make&&make install

(3)如果一切顺利,那么Nginx应该是安装成功了,如果遇到其他问题,请继续百度查询。我们来启动Nginx服务器

/usr/local/nginx/sbin/nginx

Nginx的重启命令和关闭命令

/usr/local/nginx/sbin/nginx -s reload
/usr/local/nginx/sbin/nginx -s stop

然后,用浏览器输入ip地址就能访问啦,注意如果服务器已经安装了Apache,应该避免80端口冲突。如果浏览器访问不显示任何内容,可能需要开放服务器的80端口。

访问Nginx默认页面

三、关闭防火墙,开放80端口(一般来说,都应该配置防火墙)
关闭防火墙

service iptables stop

编辑配置文件

vi /etc/sysconfig/iptables

添加一条开放80端口的规则后,保存并关闭文件

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

重启防火墙

service iptables start

防火墙正常重启后,再用浏览器访问应该就可以啦。

四、把Nginx添加到服务: 现在启动和停止服务器的命令太长了,我们要把Nginx添加到服务中,然后用服务命令来控制Nginx的启动和停止。
(1)新建脚本

vi /etc/init.d/nginx

输入以下内容:
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /run/nginx/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
lockfile=/var/lock/nginx.lock
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
需要注意如下几个位置的替换,他们应该是您服务器上Nginx实际安装路径

# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /run/nginx.pid
nginx="/usr/local/nginx/sbin/nginx"
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
lockfile=/var/lock/nginx.lock

(2)添加到服务

chmod a+x /etc/init.d/nginx
chkconfig  --add nginx
chkconfig  --list nginx

上面其实就是在/etc/rc.d/rc5.d/目录下创建了一个链接。如下:

cd /etc/rc.d/rc5.d/
ll |grepnginx

显示结果:

lrwxrwxrwx1 root root 15 Feb5 09:33 K15nginx-> ../init.d/nginx

(3)使用service命令 操作Nginx

service nginx start  //启动
service nginx stop  //停止
service nginx restart   //重启
service nginx reload    //重新加载配置文件

五、编译安装Nginx比较麻烦,可以使用Centos的yum来安装Nginx,方法如下:
(1)下载对应当前系统版本的Nginx包(package)

wget  http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

(2)建立Nginx的yum仓库

rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm

(3)下载并安装Nginx

yum install nginx

(4)设置开机启动Nginx服务

sudo systemctl enable nginx.service

(5)启动Nginx服务

systemctl start nginx

Nginx logo
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容