openshift编译安装nginx

2013-4-25 王健宇 Linux

前提:已经创建DIY APP,并SSH到APP。以下命令在SSH中操作。

环境变量自行查看

env

查看一下cpu信息

cat /proc/cpuinfo

下载zlib备用

cd $OPENSHIFT_TMP_DIR
wget http
://zlib.net/zlib-1.2.7.tar.gz
tar
-zxvf zlib-1.2.7.tar.gz

下载pcre备用,url重写需要

cd $OPENSHIFT_TMP_DIR
wget ftp
://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.32.tar.bz2
tar
-xjvf pcre-8.32.tar.bz2

下面编译nginx

cd $OPENSHIFT_TMP_DIR
wget http
://nginx.org/download/nginx-1.3.15.tar.gz
tar
-zxvf nginx-1.3.15.tar.gz
rm
-rf nginx-1.3.14.tar.gz
rm
-rf nginx-1.3.14
cd nginx
-1.3.15
make clean
CXX
=gcc
CHOST
="x86_64-redhat-linux-gnu"
CFLAGS
="-O3 \
-fomit-frame-pointer \
-march=native \
-pipe \
-mmmx \
-mfpmath=sse \
-m128bit-long-double \
-m64 \
-maccumulate-outgoing-args \
-ftree-loop-linear \
-fprefetch-loop-arrays \
-fno-omit-frame-pointer \
-freg-struct-return \
-fgcse-sm \
-fgcse-las \
-fforce-addr \
-fivopts \
-ftree-vectorize \
-ftracer \
-frename-registers \
-minline-all-stringops \
-fbranch-target-load-optimize2"

CXXFLAGS
="${CFLAGS}"
export CXX CFLAGS CXXFLAGS
./configure \
 
--prefix=$OPENSHIFT_DATA_DIR/nginx \
 
--with-ipv6 \
 
--with-http_ssl_module \
 
--with-http_addition_module \
 
--with-http_gzip_static_module \
 
--with-http_spdy_module \
 
--with-http_stub_status_module \
 
--with-pcre=$OPENSHIFT_TMP_DIR/pcre-8.32 \
 
--with-zlib=$OPENSHIFT_TMP_DIR/zlib-1.2.7
make
-j2
make install

nginx安装在$OPENSHIFT_DATA_DIR/nginx,物理路径自行查看

echo $OPENSHIFT_DATA_DIR

配置文件在

$OPENSHIFT_DATA_DIR/nginx/conf/nginx.conf

配置文件:

#user  nobody;
# 双核心,不能浪费了
worker_processes 
2;
worker_cpu_affinity
01 10;

# 自行echo $OPENSHIFT_DATA_DIR替换掉$OPENSHIFT_DATA_DIR
error_log  $OPENSHIFT_DATA_DIR
/nginx/logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

# pid放共享内存里面,不知道安不安全
pid       
/dev/shm/nginx.pid;

events
{
   
use epoll;
    worker_connections 
1024;
}

http
{
    include       mime
.types;
    default_type  application
/octet-stream;

    log_format  main 
'$remote_addr - $remote_user [$time_local] "$request" '
                     
'$status $body_bytes_sent "$http_referer" '
                     
'"$http_user_agent" "$http_x_forwarded_for"';

   
# 自行echo $OPENSHIFT_DATA_DIR替换掉$OPENSHIFT_DATA_DIR
    access_log  $OPENSHIFT_DATA_DIR
/nginx/logs/access.log  main;

    sendfile        on
;
    tcp_nopush      on
;
   
# 当nginx使用3xx重定向请求时,指向的是appname.rhcloud.com:8080,8080是内部监听端口,从外部请求8080端口将会超时,
   
# 解决这个问题需要在nginx配置的http部分中添加port_in_redirect off;
   
# http://www.zzsec.org/life/2013/03/22/install-nginx-in-openshift/
    port_in_redirect off
;

   
#keepalive_timeout  0;
    keepalive_timeout 
65;

    gzip  on
;
    gzip_buffers    
4 16k;
    gzip_http_version
1.0;
    gzip_comp_level
2;
    gzip_types       text
/plain application/x-javascript text/css application/xml;
    gzip_proxied        expired
no-cache no-store private auth;

    fastcgi_connect_timeout
300;
    fastcgi_send_timeout
300;
    fastcgi_read_timeout
300;
    fastcgi_buffer_size
32k;
    fastcgi_buffers
4 32k;
    fastcgi_busy_buffers_size
32k;
    fastcgi_temp_file_write_size
32k;

    server
{
       
# 自行echo $OPENSHIFT_INTERNAL_IP  echo $OPENSHIFT_INTERNAL_PORT替换掉
       
# openshift不能直接访问,所有数据通过$OPENSHIFT_INTERNAL_PORT中转给你的应用。
        listen       $OPENSHIFT_INTERNAL_IP
:$OPENSHIFT_INTERNAL_PORT;
        server_name  localhost
;

       
# 默认文档编码,自行决定,也许你需要gbk
        charset utf
-8;

       
# 自行echo $OPENSHIFT_DATA_DIR替换掉$OPENSHIFT_DATA_DIR
        access_log  $OPENSHIFT_DATA_DIR
/nginx/logs/access.log  main;
        error_log  $OPENSHIFT_DATA_DIR
/nginx/logs/error.log;

        location
/ {
            root   html
;
            index  index
.html index.htm;
       
}

       
#error_page  404              /404.html;

       
# redirect server error pages to the static page /50x.html
       
#
        error_page  
500 502 503 504  /50x.html;
        location
= /50x.html {
            root   html
;
       
}

       
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
       
#
       
#location ~ \.php$ {
       
#    proxy_pass   http://127.0.0.1;
       
#}

       
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
       
# php的设置,下篇文章介绍
        location
~ \.php$ {
            root           html
;
           
# 自行echo $OPENSHIFT_INTERNAL_IP替换掉
           
# 端口15123自行选择,范围15000 – 35530,和php-fpm.conf中保持一致就行。
           
# 需要提醒大家注意的一点是,如果你打算在 openshift 上编译并搭建它,你需要了解 openshift 不是每个端口都可以对外的。
           
# 大部分你熟悉的端口都已经在内外两边都为官方应用保留了,内部你只可以用 15000 – 35530,
           
# 对外只可以 bind 到 ${OPENSHIFT_INTERNAL_PORT} 也就是 8080,会通过 80 端口转发给外部。
            fastcgi_pass   $OPENSHIFT_INTERNAL_IP
:15123;
            fastcgi_index  index
.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name
;
            include        fastcgi_params
;
       
}

       
# deny access to .htaccess files, if Apache's document root
       
# concurs with nginx's one
       
#
        location
~ /\.ht {
            deny  all
;
       
}
       
# IPB的url重写规则,不用ipb的请删掉
       
if (!-e $request_filename){
            rewrite \.
(jpeg|jpg|gif|png|cgi|htm|html|css|xml|txt|rar|zip)$ /public/404.php break;
       
}
       
if (!-e $request_filename){
            rewrite
/. /index.php last;
       
}
       
# 删到这里为止
   
}
}

保存配置文件后开始启动nginx
先把自带的testrubyserver停掉,这占用着8080端口呢

killall ruby

启动nginx,编辑启动文件

vi ~/app-root/repo/.openshift/action_hooks/start

内容为:

#!/bin/bash
# The logic to start up your application should be put in this
# script. The application will work only if it binds to
# $OPENSHIFT_INTERNAL_IP:8080
# 注释掉testrubyserver的启动
# nohup $OPENSHIFT_REPO_DIR/diy/testrubyserver.rb $OPENSHIFT_INTERNAL_IP $OPENSHIFT_REPO_DIR/diy > $OPENSHIFT_HOMEDIR/diy-0.1/logs/server.log 2>&1 &
# 添加nginx启动命令
nohup $OPENSHIFT_DATA_DIR/nginx/sbin/nginx > $OPENSHIFT_DIY_LOG_DIR/server.log 2>&1 &

保存退出vi
编辑停止命令文件

vi ~/app-root/repo/.openshift/action_hooks/stop

内容为:

#!/bin/bash
# The logic to stop your application should be put in this script.
kill `ps -ef | grep testrubyserver.rb | grep -v grep | awk '{ print $2 }'` > /dev/null 2>&1
ps -ef | grep nginx | while read line
do
  kill -9 `echo $line | awk '{ print $2 }'`
done
exit 0

保存退出vi
现在可以用rhc app start|stop|restart 你的应用了。

标签: openshift编译安装nginx 红帽云编译安装nginx

发表评论:

Powered by emlog sitemap