linux下安装nginx及服务配置(转载)

本文是写给那些刚刚接触linux的新手写的,#符号后面的都是命令可以复制进去,你有发展linux的打算也可以手打加深对命令的了解。
第一步:# yum -y installgcc-c++(C编译器下面的安装包需要)


Linux系统下Nginx的源码编译安装模块依赖性,需要依赖下面3个安装包(下面的演示版本不是最新版本,你也可以下载最新的版本,只要把版本号修改一下即可):
1:ssl 功能需要 openssl 库 ( 下载: http://www.openssl.org/source)

# wget http://www.openssl.org/source/openssl-fips-2.0.10.tar.gz

2:gzip 模块需要 zlib 库 ( 下载: http://www.zlib.net/)



# wget http://zlib.net/zlib-1.2.11.tar.gz

3:rewrite 模块需要 pcre 库 ( 下载: http://www.pcre.org/ )



# wget http://nchc.dl.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz

这个 是本文的核心,不下载能行?

Nginx包(下载: http://nginx.org/en/download.html)



# wget http://nginx.org/download/nginx-1.8.0.tar.gz

第二步:下载完后按顺序依次安装openssl-fips-2.0.10.tar.gz,zlib-1.2.11.tar.gz, pcre-8.37.tar.gz,nginx-1.8.0.tar.gz

#su -(可以返回localhost)



1.安装openssl-fips-2.0.10.tar.gz

[root@localhost]# tar -zxvfopenssl-fips-2.0.10.tar.gz

[root@localhost]# cdopenssl-fips-2.0.10

[[email protected]]# ./config

[[email protected]]# make

[[email protected]]# make install



2.安装zlib-1.2.11.tar.gz(返回localhost)

[root@localhost]# tar -zxvfzlib-1.2.11.tar.gz

[root@localhost]# cdzlib-1.2.11

[root@localhost zlib-1.2.11]#./configure

[root@localhost zlib-1.2.11]#make

[root@localhost zlib-1.2.11]#make install



3.安装pcre-8.37.tar.gz(返回localhost)

[root@localhost]# tar -zxvfpcre-8.37.tar.gz

[root@localhost]# cdpcre-8.37

[root@localhost pcre-8.37]#./configure

[root@localhost pcre-8.37]#make

[root@localhost pcre-8.37]# makeinstall



4、安装nginx-1.8.0.tar.gz(返回localhost)

[root@localhost]# tar -zxvfnginx-1.8.0.tar.gz

[root@localhost]# cdnginx-1.8.0

[root@localhost nginx-1.8.0]#./configure  --with-openssl=../openssl-fips-2.0.10--with-zlib=../zlib-1.2.11 --with-pcre=../pcre-8.37



(注释:../pcre-8.37=/usr/local/pcre-8.37指的是pcre-8.37的源码路径。)



[root@localhost nginx-1.8.0]#make

[root@localhost nginx-1.8.0]#make install



第三步:检测是否安装成功并启动

检查配置文件     #  /usr/local/nginx/sbin/nginx-t

启动nginx         #  /usr/local/nginx/sbin/nginx

端口查看            #  ps -ef|grep nginx



NGINX启动时提示错误:

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx: error while loading shared libraries:libpcre.so.1: cannot open shared object file: No such file ordirectory



输入下列命令,会出现以下提示:



# ldd $(which /usr/local/nginx/sbin/nginx)



linux-vdso.so.1 => (0x00007fff48ff0000)
libcrypt.so.1 => /lib64/libcrypt.so.1 (0×0000003065800000)
libpcre.so.1 => not found
libssl.so.6 => /lib64/libssl.so.6 (0×0000003067000000)
libcrypto.so.6 => /lib64/libcrypto.so.6(0×0000003066400000)
libdl.so.2 => /lib64/libdl.so.2 (0×0000003063000000)
libz.so.1 => /lib64/libz.so.1 (0x0000003063c00000)
libc.so.6 => /lib64/libc.so.6 (0x0000003062c00000)
libgssapi_krb5.so.2 => /usr/lib64/libgssapi_krb5.so.2(0x0000003066c00000)
libkrb5.so.3 => /usr/lib64/libkrb5.so.3(0x0000003069c00000)
libcom_err.so.2 => /lib64/libcom_err.so.2(0×0000003068800000)
libk5crypto.so.3 => /usr/lib64/libk5crypto.so.3(0×0000003069000000)
/lib64/ld-linux-x86-64.so.2 (0×0000003062800000)
libkrb5support.so.0 => /usr/lib64/libkrb5support.so.0(0x000000306a800000)
libkeyutils.so.1 => /lib64/libkeyutils.so.1(0x0000003067c00000)
libresolv.so.2 => /lib64/libresolv.so.2(0×0000003068400000)
libselinux.so.1 => /lib64/libselinux.so.1(0×0000003064400000)
libsepol.so.1 => /lib64/libsepol.so.1(0×0000003064000000)

解决方法:64位系统

# ln -s /usr/local/lib/libpcre.so.1/lib64

32位系统则:

# ln -s/usr/local/lib/libpcre.so.1 /lib

第四步:关闭防火墙
关闭:#chkconfig iptables off 或者#/sbin/chkconfig--level 2345 iptables off

重启系统后生效

  重启:# reboot(重启后需要启动nginx,命令在上面)

第五步:检测是否成功运行



打开浏览器访问此机器的 IP,如果浏览器出现 Welcometonginx! 则表示 Nginx 已经安装并运行成功。



第六步:修改nginx.conf配置文件(简单的反向代理配置)



# vi  /usr/local/nginx/conf/nginx.conf

在文本编辑中按 I键可以进入编辑模式,Esc键可以退出编辑模式(只是退出编辑模式不是退出vi模式。 :wq   可以在退出编辑模式输入这个命令保存退出vi模式。

nginx.conf文件中找出下列这段代码

   server {

       listen      80;

       server_name  localhost;



       #charset koi8-r;



       #access_log logs/host.access.log  main;



       location / {

           root   html;

           index  index.html index.htm;

       }

更改成下列文本。  注:www.xxx.com是代表你的域名

   server {

       listen      80;

       server_name www.xxx.com;



       #charset koi8-r;



       #access_log logs/host.access.log  main;



      location / {

           root   html;

           index  index.html index.htm;

                       proxy_pass http://www.xxx.com;



       }


如果出现乱码文本中    #charset koi8-r 改成  charset utf-8 (把#符号去掉,文本中#代表着注释)

第七步:

http选项下

  gzip on;
  gzip_min_length 1k;
  gzip_buffers 16 64k;
  gzip_http_version 1.1;
  gzip_comp_level 6;
  gzip_types application/javascript text/javascript text/plain application/x-javascript text/css application/xml;
  gzip_vary on;



注意:gzip_types必须加上application/javascript 否则js不会被压缩



第八步:重启服务



# /usr/local/nginx/sbin/nginx -s reload 或     #  /etc/init.d/nginx reload

重启之后看看是否设置成功



以下代码是在前人的基础上,结合自己服务器实际情况修改的,本人服务器环境是:CentOS 6.3


1、创建启动脚本,

     root用户执行  vi /etc/init.d/nginx ,保存如下脚本

Python代码  收藏代码
#!/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   
# chkconfig: 2345 90 91   
# description: nginx web server  
# processname: nginx  
# config: /opt/nginx/conf/nginx.conf  
# pidfile: /opt/nginx/nginx.pid  
  
# Source function library.  
. /etc/init.d/functions  
  
# Source networking configuration.  
. /etc/sysconfig/network  
  
  
if [ -f /etc/sysconfig/nginx ];then  
. /etc/sysconfig/nginx  
fi  
  
# Check that networking is up.   
[ "$NETWORKING" = "no" ] && exit 0  
  
nginx="/opt/nginx/sbin/nginx"   
prog=$(basename $nginx)  
  
NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"  
  
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx  
  
lockfile=/var/lock/subsys/nginx  
  
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   
killall -9 nginx   
}  
  
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)   
    $1   
    ;;   
test)   
    configtest   
    ;;   
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|test}"   
exit 2   
esac   



2、为nginx脚本添加权限

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

3、添加nginx服务
chkconfig --add nginx
chkconfig nginx on



4、测试

service nginx start

service nginx stop

service nginx reload





启动操作

nginx -c /usr/local/nginx/conf/nginx.conf
-c参数指定了要加载的nginx配置文件路径

停止操作
停止操作是通过向nginx进程发送信号来进行的

步骤1:查询nginx主进程号

ps -ef | grep nginx
在进程列表里 面找master进程,它的编号就是主进程号了。



步骤2:发送信号

复制代码
从容停止Nginx:
kill -QUIT 主进程号 
例如:kill -QUIT 16391

快速停止Nginx:
kill -TERM 主进程号 

强制停止Nginx:
kill -9 主进程号 
复制代码
另外, 若在nginx.conf配置了pid文件存放路径则该文件存放的就是Nginx主进程号,如果没指定则放在nginx的logs目录下。有了pid文 件,我们就不用先查询Nginx的主进程号,而直接向Nginx发送信号了,命令如下:

kill -信号类型'/usr/local/nginx/logs/nginx.pid'


平滑重启

如果更改了配置就要重启Nginx,要先关闭Nginx再打开?不是的,可以向Nginx 发送信号,平滑重启。
平滑重启命令:

kill -HUP 住进称号或进程号文件路径 
或者使用
/usr/nginx/sbin/nginx -s reload 
注意,修改了配置文件后最好先检查一下修改过的配置文件是否正 确,以免重启后Nginx出现错误影响服务器稳定运行。判断Nginx配置是否正确命令如下:

nginx -t -c /usr/nginx/conf/nginx.conf
或者
/usr/nginx/sbin/nginx -t

猜你喜欢

转载自cd0281.iteye.com/blog/2406641