Home > php教程 > php手册 > ubuntu12.04下安装Apache+PHP+MySQL

ubuntu12.04下安装Apache+PHP+MySQL

WBOY
Release: 2016-06-06 19:47:32
Original
1194 people have browsed it

一、Apache 1、安装apache2: sudo apt-get install apache2 2、重启apache2: sudo /etc/init.d/apache2 restart 3、在浏览器里输入http://localhost或者是http://127.0.0.1,如果看到了 那就说明Apache成功安装了。 Apache的默认安装,会在/var下建立一个

一、Apache
1、安装apache2:

<span><span>sudo</span> apt-get <span>install</span> apache2</span>
Copy after login

2、重启apache2:

<span><span>sudo</span> /etc/init.d/apache2 restart</span>
Copy after login
Copy after login

3、在浏览器里输入http://localhost或者是http://127.0.0.1,如果看到了

ubuntu12.04下安装Apache+PHP+MySQL

那就说明Apache成功安装了。
Apache的默认安装,会在/var下建立一个名为www的Web目录,所有要能通过浏览器访问的Web文件都要放到这个目录里。

4、若要关闭apache2,执行如下命令:

<span><span>sudo</span> service apache2 stop</span>
Copy after login


二、PHP
1、安装php(建议安装扩展php5-gd php5-mysql):

<span><span>sudo</span> apt-get <span>install</span> libapache2-mod-php5 php5</span>
Copy after login

2、重启apache2,让它加载PHP模块:

<span><span>sudo</span> /etc/init.d/apache2 restart</span>
Copy after login
Copy after login

3、PHP网络服务器根目录默认设置是在:/var/www。由于Linux系统的安全性原则,该目录下的文件读写权限是只允许root用户操作的,所以我们不能在www文件夹中新建php文件,也不能修改和删除,必须先修改/var/www目录的读写权限。在界面管理器中通过右键属性不能修改文件权限,得执行root终端命令:

<span><span>sudo</span> <span>chmod</span> <span>777</span> /var/www</span>
Copy after login

然后就可以写入html或php文件了。

4、在Web目录下面新建一个test.php文件来测试PHP是否能正常的运行:

<span><span>sudo</span> gedit /var/www/test.php</span>
Copy after login

然后输入:

<?php <span>echo "hello,world!!"; ?>
<?php <span>phpinfo(); ?>
Copy after login

保存文件,在浏览器里输入http://127.0.0.1/test.php,如果看到”hello,world!!“和php的信息,如下图所示:

ubuntu12.04下安装Apache+PHP+MySQL

在网页中显示那就说明PHP已经正常运行了。

5、如果输出汉字时在网页中出现乱码,则在/etc/apache2/apache2.conf 文件尾加入如下代码:

AddDefaultCharset UTF-<span>8</span>
Copy after login

保存后重启Apache2,再刷新网页,中文即可正常显示。


三、MySQL
1、安装mysql数据库(apt-get程序会自动下载安装最新的mysql版本):

<span><span>sudo</span> apt-get <span>install</span> mysql-server mysql-client</span>
Copy after login

在安装的最后,输入root的密码(这里的root密码不是Ubuntu的root密码,是你要给MySQL设定的root密码)。

注:MySQL的配置文件在/etc/mysql目录。

  为了避免中文可能带来的乱码问题,将默认字符集改成utf-8,修改 /etc/mysql/my.cnf 文件,在相应位置添加:

<span>[client]
default</span>-character-set=<span>utf8

[mysql]
default</span>-character-set=<span>utf8

[mysqld]
collation</span>-server =<span> utf8_unicode_ci
init</span>-connect=<span>'</span><span>SET NAMES utf8</span><span>'</span><span>
character</span>-set-server = utf8
Copy after login

2、在 /var/www 目录下新建 testmysql.php 文件,测试php连接MySQL,文件内容如下:

<span>php
    $link </span>= mysql_connect(<span>"</span><span>localhost</span><span>"</span>, <span>"</span><span>用户名</span><span>"</span>, <span>"</span><span>密码</span><span>"</span><span>);
    </span><span>if</span> (!<span>$link)
    {
            die(</span><span>'</span><span>Could not connect: </span><span>'</span><span>.mysql_error());
    }
    </span><span>else</span> <span>echo</span> <span>"</span><span>MySQL连接成功</span><span>"</span><span>;
    mysql_close($link);
</span>?>
Copy after login

再浏览器访问testmysql.php,若链接成功即可显示"MySQL连接成功"

3、安装phpmyadmin-Mysql数据库管理

<span><span>sudo</span> apt-get <span>install</span> phpmyadmin</span>
Copy after login

在安装过程中会要求选择Web server:apache2或lighttpd,如下图所示:

ubuntu12.04下安装Apache+PHP+MySQL

使用空格键选定apache2,按tab键,然后enter;

然后会要求输入设置的Mysql数据库密码 :

ubuntu12.04下安装Apache+PHP+MySQL
再设置phpmyadmin注册数据库的密码:

ubuntu12.04下安装Apache+PHP+MySQL
然后将phpmyadmin与apache2建立符号连接,因www目录在/var/www,phpmyadmin在/usr/share/phpmyadmin目录,所以:

<span><span>sudo</span> <span>ln</span> -s /usr/share/phpmyadmin /var/www</span>
Copy after login

最后phpmyadmin测试:打开http://localhost/phpmyadmin,就可看到phpMyAdmin的登陆界面,如下图所示:

ubuntu12.04下安装Apache+PHP+MySQL

也可参考网站:http://wiki.ubuntu.org.cn/Apache

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template