本文共 7719 字,大约阅读时间需要 25 分钟。
进入终端后,我们先尝试ls命令
ls
ls命令,是显示目录内的文件,类似CMD中的dir。和powershell中的ls类似。
如果没有nginx-1.6.0.sh这份文件,我们可以运行一下VIM把下面的文件保存为nginx-1.6.0.sh
#!/bin/bashSRC_URI="http://t-down.oss-cn-hangzhou.aliyuncs.com/nginx-1.6.0.tar.gz"PKG_NAME=`basename $SRC_URI`PKG_NAME_DIR=nginx-1.6.0PREFIX=/alidata/nginxLOGS=$PREFIX/logsVHOSTS=$PREFIX/conf/vhostsDIR=`pwd`DATE=`date +%Y%m%d%H%M%S`CPU_NUM=$(cat /proc/cpuinfo | grep processor | wc -l)\mv $PREFIX ${PREFIX}.bak.$DATE &> /dev/nullmkdir -p $PREFIXmkdir -p $VHOSTSmkdir -p /alidata/install####---- user add ----begin####groupadd www &> /dev/nulluseradd -g www -M -d /alidata/www -s /sbin/nologin www &> /dev/null####---- user add ----end####cd /alidata/installif [ ! -s $PKG_NAME ]; then wget -c $SRC_URIfi####---- install dependencies ----begin####if [ "$(cat /proc/version | grep redhat)" != "" ];then wget http://git.jiagouyun.com/operation/operation/raw/master/linux/redhat/CentOS-Base.repo -O /etc/yum.repos.d/CentOS-Base.repo yum makecache yum -y install gcc gcc-c++ gcc-g77 make unzip automake openssl openssl-devel curl curl-devel pcre-develelif [ "$(cat /proc/version | grep centos)" != "" ];then#note : The CentOS 5 series, Yum will install 32 bit packet, then filter out 32. if [ `uname -m` == "x86_64" ];then if cat /etc/issue |grep "5\." &> /dev/null;then if ! cat /etc/yum.conf |grep "exclude=\*\.i?86" &> /dev/null;then sed -i 's;\[main\];\[main\]\nexclude=*.i?86;' /etc/yum.conf fi rpm --import /etc/pki/rpm-gpg/RPM* fi fi yum makecache yum -y install gcc gcc-c++ gcc-g77 make unzip automake openssl openssl-devel curl curl-devel pcre-develelif [ "$(cat /proc/version | grep ubuntu)" != "" ];then sed -i 's/exit 0//' /etc/rc.local apt-get -y update apt-get -y install unzip libcurl4-openssl-dev libpcre3-dev elif [ "$(cat /proc/version | grep -i debian)" != "" ];then apt-get -y update apt-get -y install unzip libcurl4-openssl-dev libpcre3-devfi####---- install dependencies ----end##########----- install ----begin######rm -rf $PKG_NAME_DIRtar zxvf $PKG_NAMEcd $PKG_NAME_DIR./configure --user=www \--group=www \--prefix=$PREFIX \--with-http_stub_status_module \--without-http-cache \--with-http_ssl_module \--with-http_gzip_static_moduleif [ $CPU_NUM -gt 1 ];then make -j$CPU_NUMelse makefimake install######---- install ----end######cat > $PREFIX/conf/nginx.conf << EOFuser www www;worker_processes 2;error_log $LOGS/error.log crit;pid $LOGS/nginx.pid;#Specifies the value for maximum file descriptors that can be opened by this process. worker_rlimit_nofile 65535;events { use epoll; worker_connections 65535;}http { include mime.types; default_type application/octet-stream; #charset gb2312; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 8m; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; gzip on; gzip_min_length 1k; 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_vary on; #limit_zone crawler \$binary_remote_addr 10m; log_format main '\$remote_addr - \$remote_user [\$time_local] "\$request" ' '\$status \$body_bytes_sent "\$http_referer" ' '"\$http_user_agent" "\$http_x_forwarded_for"'; include $VHOSTS/*.conf;}EOFmkdir -p /alidata/www/defaultecho 'Welcome to nginx! ' > /alidata/www/default/index.htmlchown www:www /alidata/www/default/index.htmlcat > $VHOSTS/default.conf << EOFserver { listen 80 default; server_name _; #index.php or index.jsp ??? index index.html index.htm; root /alidata/www/default; ####<< Welcome to nginx!
>>#### #location ~ .*\.(php|php5)?$ #{ # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # include fastcgi.conf; #} ####<< >>#### #location / { #or : location ~ \.jsp\$ { # proxy_pass http://server:8080; # proxy_set_header Host \$host; # proxy_set_header X-Real-IP \$remote_addr; # proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; #} ####<< >>#### location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 1d; } location ~ .*\.(js|css)?$ { expires 1d; } ####<< >>#### access_log $LOGS/default.log;}EOFecho '#!/bin/bash' >> /etc/init.d/nginxcat > /etc/init.d/nginx << EOF# nginx Startup script for the Nginx HTTP Server# this script create it by ruijie. at 2014.02.26# if you find any errors on this scripts,please contact ruijie.# and send mail to ruijie at gmail dot com.# ruijie.qiao@gmail.comnginxd=$PREFIX/sbin/nginxnginx_config=$PREFIX/conf/nginx.confnginx_pid=$PREFIX/logs/nginx.pidRETVAL=0prog="nginx"[ -x \$nginxd ] || exit 0# Start nginx daemons functions.start() { if [ -e \$nginx_pid ] && netstat -tunpl | grep nginx &> /dev/null;then echo "nginx already running...." exit 1 fi echo -n \$"Starting \$prog!" \$nginxd -c \${nginx_config} RETVAL=\$? echo [ \$RETVAL = 0 ] && touch /var/lock/nginx return \$RETVAL}# Stop nginx daemons functions.stop() { echo -n \$"Stopping \$prog!" \$nginxd -s stop RETVAL=\$? echo [ \$RETVAL = 0 ] && rm -f /var/lock/nginx}# reload nginx service functions.reload() { echo -n \$"Reloading $prog!" #kill -HUP \`cat \${nginx_pid}\` \$nginxd -s reload RETVAL=\$? echo}# See how we were called.case "\$1" instart) start ;;stop) stop ;;reload) reload ;;restart) stop start ;;*) echo \$"Usage: $prog {start|stop|restart|reload|help}" exit 1esacexit \$RETVALEOFchmod 755 /etc/init.d/nginxchown -R www:www $LOGSchmod -R 775 /alidata/wwwchown -R www:www /alidata/wwwcd ..sed -i 's/worker_processes 2/worker_processes '"$CPU_NUM"'/' $PREFIX/conf/nginx.confchmod 755 $PREFIX/sbin/nginx#add PATHif ! cat /etc/profile | grep "export PATH=\$PATH:$PREFIX/sbin" &> /dev/null;then echo "export PATH=\$PATH:$PREFIX/sbin" >> /etc/profilefisource /etc/profile#add rc.localif ! cat /etc/rc.local | grep "/etc/init.d/nginx start" &> /dev/null;then echo "/etc/init.d/nginx start" >> /etc/rc.localfi/etc/init.d/nginx startcd $DIRbash
#include "fcgi_stdio.h"#include#include int main(){ while(FCGI_Accept() >= 0) { printf("Content-type: text/*\r\n\r\n"); printf("欢迎来到由阿里云运行的C语言"); } return 0;}
按下ESC退出编辑模式,输入
:wq
保存及退出VIM
编译这个文件
g++ a.cpp -o index.aliC -lfcgi
等待完成后,我们就可以去编译我们的C语言了
接下来,把文件替换一下,因aliyun是经过优化的,我们需要他原来的那份默认配置编辑
\cp -rf /alidata/nginx/conf/nginx.conf.default /alidata/nginx/conf/nginx.conf
运行VIM编辑文件并定位到第65行
vim /alidata/nginx/conf/nginx.conf +65
轻轻按下ctrl+v,进入编辑模式
按j选中我们要修改代码的注释
然后再按D删除注释
选到第65行,按i进入编辑模式把php改为aliC
同时,找到第66,68,69,70行可以删除,不删也没关系。
按下ESC然后输入:wq保存及退出
:wq
最后重启nginx
nginx -s reload
或者
killall nginx
nginx
最后,运行一下
spawn-fcgi -f /alidata/www/default/index.aliC -p 9000
看到如下提示证明成功了
接下来运行浏览器,打开http://localhost/index.aliC,或者执行
curl http://localhost/index.aliC
或者
curl http://127.0.0.1/index.aliC
即可看到如下结果
遇到的问题及解决方案
曾经遇到spawn-fcgi返回child exited with: 0的问题,经过搜索,解决方案很少,几乎找不到。
经过排查,发现是C的源码问题。需要加上
while(FCGI_Accept() >= 0)这句代码才能正常运行。