CentOS上安装nginx服务器

在CentOS上安装nginx服务器

image

一、环境描述
1. 虚拟机配置
CPU:单核
内存:2 GB
硬盘:120 GB
IP:10.24.17.108
2. 操作系统

版本:CentOS 6.6 x86_64

安装方式:Minimal

3. 虚拟化环境

VMware Workstation 12.1.0

4. nginx

版本:nginx-1.10.1.tar.gz

安装方式:编译源码安装

二、下载nginx源码包

在bash中运行以下命令,下载nginx的源码包:

cd /root/Downloads

# 下载nginx源码包

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

# 下载pcre源码包

wget http://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.gz

# 下载zlib源码包

wget http://zlib.net/zlib-1.2.8.tar.gz

三、安装依赖包

在bash中运行以下命令,在服务器上安装nginx的依赖包:

yum –disablerepo=\* –enablerepo=c6-media install -y openssl openssl-devel

在bash中运行以下命令,安装编译环境:

yum –disablerepo=\* –enablerepo=c6-media groupinstall -y “Development Tools”

四、创建用户和组

在bash中运行以下命令,为nginx创建用户和组:

groupadd -r nginx

useradd -s /sbin/nologin -g nginx -r nginx

五、解压缩源码包

在bash中运行以下命令,解压缩nginx和依赖包的源码包:

tar xvzf nginx-1.10.1.tar.gz

tar xvzf pcre-8.38.tar.gz

tar xvzf zlib-1.2.8.tar.gz

cd nginx-1.10.1

六、编译安装

在bash中运行以下命令,编译安装nginx:

# 配置编译选项

./configure \

–prefix=/usr/local/nginx \

–user=nginx \

–group=nginx \

–with-select_module \

–with-poll_module \

–with-http_ssl_module \

–with-pcre=/root/Downloads/pcre-8.38 \

–with-pcre-jit \

–with-zlib=/root/Downloads/zlib-1.2.8

# 编译安装

make && make install

七、配置自动启动服务

在bash中运行以下命令,将nginx配置为自动启动的系统服务:

vi /etc/init.d/nginx

在上述init脚本中输入并保存以下内容:

#!/bin/sh

#

# nginx – this script starts and stops the nginx daemon

#

# 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: /usr/local/nginx/pid/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/nginx/sbin/nginx”

prog=$(basename $nginx)

NGINX_CONF_FILE=”/usr/local/nginx/conf/nginx.conf”

lockfile=/usr/local/nginx/lock/nginx.lock

make_dirs() {

# make required directories

user=`$nginx -V 2>&1 | grep “configure arguments:” | sed ‘s/[^*]*–user=\([^ ]*\).*/\1/g’ -`

if [ -z “`grep $user /etc/passwd`” ]; then

useradd -M -s /bin/nologin $user

fi

options=`$nginx -V 2>&1 | grep ‘configure arguments:’`

for opt in $options; do

if [ `echo $opt | grep ‘.*-temp-path’` ]; then

value=`echo $opt | cut -d “=” -f 2`

if [ ! -d “$value” ]; then

# echo “creating” $value

mkdir -p $value && chown -R $user $value

fi

fi

done

}

start() {

[ -x $nginx ] || exit 5

[ -f $NGINX_CONF_FILE ] || exit 6

make_dirs

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

sleep 1

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

修改脚本权限,然后设置为随系统自动启动:

mkdir -p /usr/local/nginx/pid

mkdir -p /usr/local/nginx/lock

chmod 755 /etc/init.d/nginx

chkconfig nginx on

配置环境变量:

sed -i ’80s/$/&:\/usr\/local\/nginx\/sbin/’ /etc/profile

source /etc/profile

至此,可以通过以下命令操作nginx服务:

# 启动nginx服务

service nginx start

# 停止nginx服务

service nginx stop

# 重启nginx服务

service nginx restart

# 查看nginx服务状态

service nginx status

八、开启gzip压缩

在bash中运行以下命令,为nginx开启gzip压缩功能,减少网络带宽占用:

# 配置pid文件位置

sed -i ‘9s/#pid logs\/nginx.pid;/pid pid\/nginx.pid;/’ /usr/local/nginx/conf/nginx.conf

# 启用gzip压缩

sed -i ’33s/#gzip on;/gzip on;/’ /usr/local/nginx/conf/nginx.conf

# 启用gzip压缩的最小文件

sed -i ’33a\ gzip_min_length 1k;’ /usr/local/nginx/conf/nginx.conf

# gzip压缩级别

sed -i ’34a\ gzip_comp_level 2;’ /usr/local/nginx/conf/nginx.conf

# 进行压缩的文件类型

sed -i ’35a\ gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;’ /usr/local/nginx/conf/nginx.conf

# 是否在http header中添加Vary: Accept-Encoding

sed -i ’36a\ gzip_vary on;’ /usr/local/nginx/conf/nginx.conf

# 禁用IE 6 gzip

sed -i ’37a\ gzip_disable “MSIE [1-6]\\.”;’ /usr/local/nginx/conf/nginx.conf

# 为ttf、otf和svg字体启用gzip

sed -i ’38a\ gzip_types font/ttf font/otf image/svg+xml;’ /usr/local/nginx/conf/nginx.conf

# 上传文件最大尺寸

sed -i ’39a\ client_max_body_size 20m;’ /usr/local/nginx/conf/nginx.conf

九、开启缓存

在bash中运行以下命令,为nginx开启静态内容缓存,减少网络带宽占用:

# 为图片文件设置缓存

sed -i ’85a\\n’ /usr/local/nginx/conf/nginx.conf

sed -i ’86a\ location ~* ^.+\\.(ico|gif|jpg|jpeg|png)$ {‘ /usr/local/nginx/conf/nginx.conf

sed -i ’87a\ access_log off;’ /usr/local/nginx/conf/nginx.conf

sed -i ’88a\ expires 30d;’ /usr/local/nginx/conf/nginx.conf

sed -i ’89a\ }’ /usr/local/nginx/conf/nginx.conf

# 为文本文件设置缓存

sed -i ’90a\’ /usr/local/nginx/conf/nginx.conf

sed -i ’91a\ location ~* ^.+\\.(css|js|txt|xml|swf|wav)$ {‘ /usr/local/nginx/conf/nginx.conf

sed -i ’92a\ access_log off;’ /usr/local/nginx/conf/nginx.conf

sed -i ’93a\ expires 24h;’ /usr/local/nginx/conf/nginx.conf

sed -i ’94a\ }’ /usr/local/nginx/conf/nginx.conf

# 为静态页面设置缓存

sed -i ’95a\\n’ /usr/local/nginx/conf/nginx.conf

sed -i ’96a\ location ~* ^.+\\.(html|htm)$ {‘ /usr/local/nginx/conf/nginx.conf

sed -i ’97a\ expires 1h;’ /usr/local/nginx/conf/nginx.conf

sed -i ’98a\ }’ /usr/local/nginx/conf/nginx.conf

# 为字体设置缓存

sed -i ’99a\’ /usr/local/nginx/conf/nginx.conf

sed -i ‘100a\ location ~* ^.+\\.(eot|ttf|otf|woff|svg)$ {‘ /usr/local/nginx/conf/nginx.conf

sed -i ‘101a\ access_log off;’ /usr/local/nginx/conf/nginx.conf

sed -i ‘102a\ expires max;’ /usr/local/nginx/conf/nginx.conf

sed -i ‘103a\ }’ /usr/local/nginx/conf/nginx.conf

十、启动服务

在bash中运行以下命令,启动nginx服务:

service nginx start