利用C语言制作网站



进入终端后,我们先尝试ls命令

ls

ls命令,是显示目录内的文件,类似CMD中的dir。和powershell中的ls类似。

利用C语言制作网站


如果没有nginx-1.6.0.sh这份文件,我们可以运行一下VIM把下面的文件保存为nginx-1.6.0.sh


#!/bin/bash

SRC_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.0
PREFIX=/alidata/nginx
LOGS=$PREFIX/logs
VHOSTS=$PREFIX/conf/vhosts
DIR=`pwd`
DATE=`date +%Y%m%d%H%M%S`
CPU_NUM=$(cat /proc/cpuinfo | grep processor | wc -l)

\mv $PREFIX ${PREFIX}.bak.$DATE &> /dev/null
mkdir -p $PREFIX
mkdir -p $VHOSTS
mkdir -p /alidata/install

####---- user add ----begin####
groupadd www &> /dev/null
useradd -g www -M -d /alidata/www -s /sbin/nologin www &> /dev/null
####---- user add ----end####
cd /alidata/install

if [ ! -s $PKG_NAME ]; then
  wget -c $SRC_URI
fi

####---- 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-devel
elif [ "$(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-devel
elif [ "$(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-dev
fi
####---- install dependencies ----end####

######----- install ----begin######
rm -rf $PKG_NAME_DIR
tar zxvf $PKG_NAME
cd $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_module
if [ $CPU_NUM -gt 1 ];then
    make -j$CPU_NUM
else
    make
fi
make install
######---- install ----end######

cat > $PREFIX/conf/nginx.conf << EOF
user  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;
}
EOF

mkdir -p /alidata/www/default
echo '<html><head><title>Welcome to nginx!</title></head><body bgcolor="white" text="black"><center><h1>Welcome to nginx!</h1></center></body></html>' > /alidata/www/default/index.html
chown www:www /alidata/www/default/index.html
cat > $VHOSTS/default.conf << EOF
server {
    listen       80 default;
    server_name  _;
	#index.php or index.jsp ???
    index index.html index.htm;
    root /alidata/www/default;
	####<<<PHP settings>>>####
	#location ~ .*\.(php|php5)?$
	#{
	#	fastcgi_pass  127.0.0.1:9000;
	#	fastcgi_index index.php;
	#	include fastcgi.conf;
	#}
	
    ####<<<Tomcat settings>>>####
    #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;
    #}
	
    ####<<<Cache settings>>>####
	location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
	{
		expires 1d;
	}
	location ~ .*\.(js|css)?$
	{
		expires 1d;
	}
	####<<<The log path set>>>####
	access_log  $LOGS/default.log;
}
EOF

echo '#!/bin/bash' >> /etc/init.d/nginx
cat > /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.com

nginxd=$PREFIX/sbin/nginx
nginx_config=$PREFIX/conf/nginx.conf
nginx_pid=$PREFIX/logs/nginx.pid

RETVAL=0
prog="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" in
start)
        start
        ;;

stop)
        stop
        ;;

reload)
        reload
        ;;

restart)
        stop
        start
        ;;

*)
        echo \$"Usage: $prog {start|stop|restart|reload|help}"
        exit 1
esac

exit \$RETVAL
EOF
chmod 755 /etc/init.d/nginx

chown -R www:www $LOGS
chmod -R 775 /alidata/www
chown -R www:www /alidata/www
cd ..
sed -i 's/worker_processes  2/worker_processes  '"$CPU_NUM"'/' $PREFIX/conf/nginx.conf
chmod 755 $PREFIX/sbin/nginx

#add PATH
if ! cat /etc/profile | grep "export PATH=\$PATH:$PREFIX/sbin" &> /dev/null;then
	echo "export PATH=\$PATH:$PREFIX/sbin" >> /etc/profile
fi
source /etc/profile
#add rc.local
if ! cat /etc/rc.local | grep "/etc/init.d/nginx start" &> /dev/null;then
    echo "/etc/init.d/nginx start" >> /etc/rc.local
fi
/etc/init.d/nginx start
cd $DIR
bash

然后,我们需要更改文件的运行权限
chmod +x nginx-1.6.0.sh

然后在运行nginx-1.6.0.sh文件
./nginx-1.6.0.sh

等待运行结束,可以看到这个界面

这个时候,我们再浏览一下IP,可以看到

则证明nginx安装成功。


接下来我们需要安装一下spawn-fcgi和fcgi,在shell界面下执行
yum -y install spawn-fcgi
yum -y install fcgi-devel

切换到网站运行目录
cd /alidata/www/default/
新建一份C文件
vim a.cpp
按下i进入编辑模式

#include "fcgi_stdio.h"
#include <stdlib.h>
#include <stdio.h>

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

利用C语言制作网站

轻轻按下ctrl+v,进入编辑模式

按j选中我们要修改代码的注释

然后再按D删除注释

选到第65行,按i进入编辑模式把php改为aliC

同时,找到第66,68,69,70行可以删除,不删也没关系。

按下ESC然后输入:wq保存及退出

:wq

利用C语言制作网站

最后重启nginx

nginx -s reload

或者

killall nginx

nginx


最后,运行一下

spawn-fcgi -f /alidata/www/default/index.aliC -p 9000

看到如下提示证明成功了

利用C语言制作网站


接下来运行浏览器,打开http://localhost/index.aliC,或者执行

curl http://localhost/index.aliC

或者

curl http://127.0.0.1/index.aliC

即可看到如下结果

利用C语言制作网站




遇到的问题及解决方案

曾经遇到spawn-fcgi返回child exited with: 0的问题,经过搜索,解决方案很少,几乎找不到。

经过排查,发现是C的源码问题。需要加上


    while(FCGI_Accept() >= 0) 
这句代码才能正常运行。

上一篇:CentOS 6.5上安装python2.7、pip以及Python命令行补全和yum冲突解决


下一篇:[书籍翻译] 《JavaScript并发编程》第一章 JavaScript并发简介