开发者社区> kajweb> 正文

利用C语言制作网站

简介: 利用C语言制作网站
+关注继续查看


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

ls

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

582a17950d0798d503e036256113cfe75bb184b5


如果没有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


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

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

然后再按D删除注释

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

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

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

:wq

Center

最后重启nginx

nginx -s reload

或者

killall nginx

nginx


最后,运行一下

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

看到如下提示证明成功了

20170216191658252


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

curl http://localhost/index.aliC

或者

curl http://127.0.0.1/index.aliC

即可看到如下结果

20170216192037070




遇到的问题及解决方案

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

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


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

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
C语言程序设计第五版谭浩强课后答案 第五章习题答案(3-17题)
C语言程序设计第五版谭浩强课后答案 第五章习题答案(3-17题)
70 0
面向对象的程序设计C++课堂复盘总结 C语言复习+C++基础语法
Stay Hungry,Stay Foolish. 任何人都能写出机器能看懂的代码,但只有优秀的程序员才能写出人能看懂的代码。 有两种写程序的方式:一种是把代码写得非常复杂,以至于 “看不出明显的错误”;另一种是把代码写得非常简单,以至于 “明显看不出错误”。 “把正确的代码改快速”,要比 “把快速的代码改正确”,容易得太多。 C++ 庞大、复杂是无法改变的事实,所以我们要把这三条格言铭记在心,对它保持一颗 “敬畏” 的心,在学习语言特性的同时,千万不要滥用特性,谦虚谨慎,戒骄戒躁。 -------CSDN Albert Edison
29 0
C语言总结(第7章 用函数实现模块化程序设计)
为什么要用函数 为了更好的实现模块化程序设计 函数(function)也有功能的意思
49 0
C语言总结(第5章循环结构程序设计)
第5章循环结构程序设计 结构化程序设计的3种基本结构: 循环结构 顺序结构 选择结构
91 0
C语言总结(第2章算法——程序的灵魂 第3章最简单的C程序设计——顺序程序设计 第4章选择结构程序设计)
算法(algorithm)是解决一系列问题的清晰指令,也就是,能对一定规范的输入,在有限的时间内获得所要求的输出。 6是解决做什么和怎么做的问题。简单来说,算法就是解决一个问题的具体方法和步骤。算法是程序的灵魂。
252 0
C语言程序设计第五版 谭浩强 p292 6, 9 14 p330 1 , 5题解
C语言程序设计第五版 谭浩强p 215-216 1. 4. 6. 8.11题解 (其它) p292 6, 9 14 p330 1 , 5
13 0
C语言程序设计第五版 谭浩强p 215-216 1. 4. 6. 8.11题解
C语言程序设计第五版 谭浩强p 215-216 1. 4. 6. 8.11题解
15 0
C语言程序设计第五版 谭浩强 P137 3,6,8,9题解
C语言程序设计第五版 谭浩强 P137 3,6,8,9题解
18 0
C语言程序设计第五版 谭浩强 P107 3,4,6,8,9题解
1)3+4>5 优先3+4得到结果7,因此7>5结果为真; 4==5为假,一真一假逻辑与最终结果为假。结果为0 (2)优先算术运算4+5得到7,非0则为真,4-5得到-1,非0则为真,||和&&优先级最低,自左向右运算,3||7结果为1,1&&-1结果为1,一真一假逻辑与最终结果为假。结果为1
21 0
【C语言进阶】C语言程序设计:动态通讯录(顺序表实现)
目录 动态通讯录的实现 一、实验的目的和意义 二、实验内容描述 三、功能描述 四、数据结构 1、三大模块 2、结构设计 3、动态开辟 4、文件操作 5、主要函数 五、流程图及模块算法 1.Contacct 程序运行流程图 2、AddContct(增加)函数流程图 3、DelContct(删除)函数流程图 4、SearchContct(查找)函数流程图 5、ModifyContct(修改)函数流程图 6、SortContct(排序)函数流程图 六、实验测试结果 七、实验总结 八、源代码 1、DynamicContact.h 2、DynamicC
49 0
+关注
kajweb
文章
问答
视频
文章排行榜
最热
最新
相关课程
更多
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载