搭建Ubuntu的Web服务器 无疑是目前非常热门的技术。下面我们来看一下如何在Ubuntu下搭建大家熟悉的Web服务器。
Linux在服务器领域表现却尤为喜人,据IDC数据显示,部署了Linux系统的服务器占到20%的市场份额。其中Red Hat和Ubuntu在服务器市场表现抢眼,由它们搭建的Web服务器在企业中应用最为广泛。
同时,Ubuntu创始人表示近年来Ubuntu形式已经开始逆转,从2011年开始采用Ubuntu作为网页服务器的用户比RHEL更多,Ubuntu 在企业市场正不断扩张,相反RHEL却呈现下跌倾向。无疑Ubuntu有如此强势表现离不开其在Web服务器上的应用。而搭建Ubuntu的Web服务器 无疑是目前非常热门的技术。下面我们来看一下如何在Ubuntu下搭建大家熟悉的Web服务器。
Ubuntu 11.10是目前官方发布的最新的正式版本,其在用户界面和内核架构等方面都有较大改善,是非常受欢迎的一个版本,下面这里给大家介绍一下如何在 Ubuntu 11.10下与Apache 2、PHP5、MySQL搭建Web服务器,希望与大家一同探讨用Ubuntu搭建Web服务器的优缺点。
1.开始说明
首先需要跟大家说明一下,这个教程中使用的IP地址是192.168.0.23,主机名称为Server1.example.com这些设置可能会有所不同,你需要根据不同情况进行修改。
Ubuntu 查看版本命令,在终端中执行下列指令:
cat /etc/issue
安装Apache2
2.切换管理员身份
在Ubuntu中需要用root身份进行操作,所以用下面的命令确保以root身份登录:
sudo su
3.开始安装MySQL5
apt-get install mysql-server mysql-client
你将被要求提供一个MySQL的root用户的密码,我们需要在红色区域设置密码。
New password for the MySQL "root" user: <-- yourrootsqlpassword
Repeat password for the MySQL "root" user: <-- yourrootsqlpassword
4.安装Apache2
Apache2的是作为一个Ubuntu的软件包,因此我们可以直接用下面命令安装它:
apt-get install apache2
现在,您的浏览器到http://192.168.0.23,你应该看到Apache2的测试页:
Apache 2首页
Apache的默认文档根目录是在Ubuntu上的/var/www目录 ,配置文件是/ etc/apache2/apache2.conf。配置存储在的子目录在/etc/apache2目录。
安装PHP5
5.安装PHP5
我们可以直接安装PHP5和Apache的PHP5的模块,如下:
apt-get install php5 libapache2-mod-php5
安装完需要重启
/etc/init.d/apache2 restart
6.测试的PHP5并获取PHP5安装的详细信息:
默认网站的文件根目录是在/var/www下中。现在我们将在该目录中创建一个小型PHP文件(info.php的)在浏览器中调用它。该文件将显示很多关于我们的PHP安装,如安装的PHP版本和有用的一些细节。
gedit /var/www/info.php
<?php
phpinfo();
?php>
显示代码
现在,我们可以用浏览器访问例如http://192.168.0.23/info.php文件
PHP页面
正如你所看到的,PHP5正常工作,它通过Apache 2.0的处理程序,在服务器API线。如果你继续向下滚动,你会看到所有在PHP5中已经启用的模块。MySQL是没有列出,这意味着我们没有在PHP5支持MySQL。
7.PHP5获得MySOL的支持
让PHP在MySQL中获得支持,我们可以安装的php-mysql软件包。安装一些其他的PHP5模块,以及您可能需要的应用程序,这是一个好主意:
apt-cache search php5
还安装需要安装的
apt-get install php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl
现在重新启动Apache2的:
/etc/init.d/apache2 restart
现在,重载http://192.168.0.23/info.php在您的浏览器,并再次向下滚动到模块部分。现在,你应该找到许多新的模块,包括MySQL模块:
PHP支持
安装phpMyAdmin
8.安装phpMyAdmin
phpMyAdmin是一个网络接口,通过它可以管理你的MySQL数据库。
apt-get install phpmyadmin
你会看到以下问题:
Web server to reconfigure automatically: <-- apache2
Configure database for phpmyadmin with dbconfig-common? <-- No
在地址栏输入http://192.168.0.100/phpMyAdmin/:你就可以访问phpMyAdmin了。
错误1
我试的地址是http://127.0.0.1/phpMyAdmin/,php和mysql正常,但phpmyadmin
出现下面的请况,
Not Found
The requested URL /phpMyAdmin was not found on this server.
解决办法:试一下http://localhost/phpmyadmin/,如可以的话请留言,还不行的话,则需要做/usr/share/phpmyadmin/个的软连接到/var/www目录下:
ln -s /usr/share/phpmyadmin/ /var/www/
最后在浏览器中输入:http://localhost/phpmyadmin/
成功后会进入登录界面
9、安装lighttpd的过错中出现的问题和解决方法
apt-get install lighttpd
从配置文件启动,输入指令
1 sudo lighttpd -f /etc/lighttpd/lighttpd.conf
我的机器上提示 2013-01-13 14:36:08: (network.c.379) can't bind to port: 80 Address already in use
我的80端口被Apache服务器占用了,所以我们要修改配置文件
gedit /etc/lighttpd/lighttpd.conf
加上
server.port = 8090 ##设置端口
保存退出 ,输入指令
1 sudo lighttpd -f /etc/lighttpd/lighttpd.conf
启动 lighttpd 服务器
浏览器中输入
localhost:8080 /默认访问的是 /var/www/ 目录
gedit /etc/lighttpd/lighttpd.conf
sudo lighttpd -f /etc/lighttpd/lighttpd.conf
安装Lighttpd
Lighttpd是一个Ubuntu包,因此我们可以这样安装它:
Aptitude install lighttpd
在浏览器输入你的本机IP就可以看到Lighttpd的页面了。
在ubuntu中,Lighttpd的文件根目录默认是在/var/www,配置文件是在/etc/lighttpd/lighttpd.conf。额外的配置文件存储在/etc/lighttpd/conf-available目录,可以用lighttpd-enable-mod启用,也可以用lighttpd-disable-mod禁用。
3、 安装php5
我们使php5通过FastCGI工作在Lighttpd下。Ubuntu提供了一个这样的包:
Aptitude install php5-cgi
4、 配置Lighttpd和php5
为了在Lighttpd中启用php5,必须修改在/etc/php5/cgi/php.ini配置文件下添加一行:
Cgi.fix_pathinfo=1
要启用fastcgi(存放在/etc/lighttpd/conf-available/10-fastcgi.conf)运行下面的命令:
Lighttpd-enable-mod fastcgi
然后创建一个链接/etc/lighttpd/conf-enables/10-fastcgi.conf指向/etc/lighttpd/conf-available/10-fastcgi.conf
sudo ln -s /etc/lighttpd/conf-available/10-fastcgi.conf /etc/lighttpd/conf-enabled/10-fastcgi.conf
再重新加载Lighttpd:
/etc/init.d/lighttpd force-reload
安装完毕
上面就是搭建Web服务器的整个步骤,Ubuntu搭建起来相对更容易一些,而且由Ubuntu搭建的Web服务器应用也非常广泛,大家可以按照步骤自己大家来看看,非常简单。
错误2
Ubuntu下Apache重启出现错误提示:Could not reliably determin
Linuxidc.com@Ubuntu
错误信息:
* Restarting web server apache2
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
解决方法:
sudo gedit /etc/apache2/apache2.conf
在文件后面加上:
#Server Name
ServerName 127.0.0.1
错误3
/etc/init.d/apache2 restart
* Restarting web server apache2 /usr/sbin/apache2ctl: 87: ulimit: error setting limit (Operation not permitted)
/usr/sbin/apache2ctl: 87: ulimit: error setting limit (Operation not permitted)
(13)Permission denied: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
Unable to open logs
Action 'start' failed.
The Apache error log may have more information.
解决办法:不要用sudo /etc/init.d/apache2 restart 和 service apache2 restart 改用 sudo /etc/init.d/apache2 restart 和 sudo service apache2 restart
403 forbidden
问题:
按照 http://wiki.ubuntu.org.cn/Lighttpd 的说明装好Lighttpd并启动之后,在访问php页面的时候,提示403 forbidden,html页面正常。
解决
这个403错误其实是误报,并非没有权限访问php文件,而是没有权限访问php-cgi模块。
Lighttpd安装后,默认只启用了一个mod,就是fastcgi,但没有启用php支持。必须手动启用fastcgi-php模块才支持php访问:
1 2 | lighty-enable-mod fastcgi-php service lighttpd force-reload |
Ubuntu英文官方Wiki说得很清楚: https://wiki.ubuntu.com/Lighttpd+PHP。
启用PHP-FPM
问题
按照上面的方式启用的php运行在fast-cgi模式。如何启用PHP-FPM?
解决
建立 /etc/lighttpd/conf-availeable/10-fastcgi-fpm.conf
文件,写入如下内容:
1 2 3 4 5 6 7 | server.modules += ("mod_fastcgi") fastcgi.server = ( ".php" => (( "host" => "127.0.0.1", "port" => "9000" )) ) |
禁用fastcgi模块和fastcgi的php支持,使用刚才建立的 fastcgi-fpm
模块同时支持两者。
1 2 3 4 | lighty-disable-mod fastcgi lighty-disable-mod fastcgi-php lighty-enable-mod fastcgi-fpm service lighttpd force-reload |
查看phpinfo,可以看到已经是FPM支持了:
参考文献:
Ubuntu12.10下安装lighttpd1.4.32详细步骤Ubuntu 9.10下安装Lighttpd支持PHP和MySQL