Home Backend Development PHP Tutorial 解析阿里云ubuntu12.04环境下配置Apache+PHP+PHPmyadmin+MYsql_php技巧

解析阿里云ubuntu12.04环境下配置Apache+PHP+PHPmyadmin+MYsql_php技巧

May 17, 2016 am 08:58 AM
apache Configuration

此教程中使用的相关IP等设置,在你的环境中要做相应修改。
使用之前更新apt-get,因为服务器基本上是一个裸系统
apt-get update;
apt-get upgrade;
1 我们使用root账户进行安装,首先切换到root账户,输入命令:
sudo su

2 安装 MySQL 5
输入命令:
apt-get install mysql-server mysql-client
安装过程中需要设置root账户密码,系统会作以下提示:
New password for the MySQL ”root” user:Repeat password for theMySQL ”root” user:

3 安装 Apache2
输入命令:
apt-get install apache2
在浏览器输入你服务器地址列入 http://192.168.0.100查看Apache2是否工作,如果显示(Itworks!),说明已经工作。
Apache 在 Ubuntu 中默认文档根目录为 /var/www,配置文件/etc/apache2/apache2.conf,额外配置存储子目录 /etc/apache2 例如/etc/apache2/mods-enabled (为 Apache 模块), /etc/apache2/sites-enabled(为虚拟主机 virtual hosts), 和 /etc/apache2/conf.d.

4 安装 PHP5
安装 PHP5 和 Apache PHP5 模块:
apt-get install php5 libapache2-mod-php5
(如果有安装的内容找不到,需要更新apt-get,执行apt-get update)
然后重启apache:
/etc/init.d/apache2 restart

5 测试 PHP5 / 可以建立一个探针页面
vi /var/www/info.php
输入下面的内容:
phpinfo();
?>
然后打开浏览器访问 (http://127.0.0.1/info.php):
你可以看到一些已经支持的模块。

6 为PHP5取得 MySQL 支持
我们需要安装 php5-mysql,先查看一下php5的模块
apt-cache search php5-mysql
php5-mysql - MySQL module for php5
php5-mysqlnd - MySQL module for php5 (Native Driver)
然后安装所需模块,例如下面的命令:
apt-get install php5-mysql
apt-get install php5-mysqlnd
sudo apt-get install php5 libapache2-mod-php5 php5-cgi php5-cli php5-common php5-curl php5-gd php5-mysql php5-pgsql
sudo a2enmod php5

差什么php5的模块,就安装php5的模块,ubuntu的php5安装module还是很方便的
以下模块自己选择安装,有些模块不一定正确
apt-get install php5-mysql php5-curl php5-gd php5-intlphp-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-mingphp5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidyphp5-xmlrpc php5-xsl
重启 Apache2:
/etc/init.d/apache2 restart
然后刷次你 http://127.0.0.1/info.php 查看模块支持是不是已经增加了。

7 phpMyAdmin
安装phpmyadmin来管理mysql:
apt-get install phpmyadmin
phpmyadmin设置:
在安装过程中会要求选择Web server:apache2或lighttpd,选择apache2,按tab键然后确定。然后会要求输入设置的Mysql数据库密码连接密码Password of the database's administrative user。
然后将phpmyadmin与apache2建立连接,以我的为例:www目录在/var/www,phpmyadmin在/usr/share /phpmyadmin目录,所以就用命令:sudo ln -s /usr/share/phpmyadmin /var/www建立连接。
phpmyadmin测试:在浏览器地址栏中打开http://localhost/phpmyadmin。
Phpmyadmin访问地址:http://127.0.0.1/phpmyadmin/

以上LAMP的基本组件就安装完毕了,下面我们再来看一些其他的设置:
设置Ubuntu文件执行读写权限
LAMP组建安装好之后,PHP网络服务器根目录默认设置是在:/var/www。由于Linux系统的安全性原则,改目录下的文件读写权限是只允许root用户操作的,所以我们不能在www文件夹中新建php文件,也不能修改和删除,必须要先修改/var/www目录的读写权限。在界面管理器中通过右键属性不能修改文件权限,得执行root终端命令:sudo chmod 777 /var/www。然后就可以写入html或php文件了。如果对777表示的文件权限不是很清楚可参考chmod命令。

配置Apache
1启用mod_rewrite模块
终端命令:sudo a2enmod rewrite
重启Apache服务器:sudo /etc/init.d/apache2 restart
Apache重启后我们可以测试一下,在/var/www目录下新建文件test.php,写入代码:保存,在地址栏输入http://127.0.0.1/test.php或http://localhost/test.php,如果正确出现了php配置信息则表明LAMP Apache已经正常工作了(记得重启Apache服务器后再测试)。

2设置Apache支持.htm .html .php
sudo gedit /etc/apache2/apache2.conf
在打开的文件中加上
AddType application/x-httpd-php .php .htm .html即可。
配置Mysql测试
上面php,Apache都已经测试过了,下面我们再测试一下Mysql数据库是否已经正确启用。
在/var/www目录下新建mysql_test.php:

复制代码 代码如下:

$link = mysql_connect(“localhost”,”root”,”020511″);
if (!$link)
{
die(‘Could not connect: ' . mysql_error());
}
else echo ”Mysql已经正确配置”;
mysql_close($link);
?>

保存退出,在地址栏输入http://127.0.0.1/mysql_test.php,显示”Mysql已经正确配置”则表示OK了,如果不行,重启Apache服务器后再试一下。
配置php5
sudo gedit /etc/php5/apache2/php.ini修改允许最大使用内存,查找
memory_limit = 8M修改为
memory_limit = 32M
修改允许最大上传尺寸,查找
upload_max_filesize = 2M修改为
upload_max_filesize = 8M
允许mysql和gd模块,检查文件最后是否包含下面的代码,如果没有添加上。(默认是在配置文件最后有添加的,检查一下以防万一)
extension=mysql.soextension=gd.so保存并关闭文件。
若出现乱码,解决方法如下:
配置apache字符编码:
sudo gedit /etc/apache2/conf.d/charset
将里面的内容改为AddDefaultCharset UTF-8
配置php字符编码:
sudo gedit /etc/php5/apache2/php.ini
找到
代码:
default_charset = “iso-8859-1″
改为
代码:
default_charset = “UTF-8″
然后重启apache:
代码:
sudo /etc/init.d/apache2 restart
 
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1248
24
How to set the cgi directory in apache How to set the cgi directory in apache Apr 13, 2025 pm 01:18 PM

To set up a CGI directory in Apache, you need to perform the following steps: Create a CGI directory such as "cgi-bin", and grant Apache write permissions. Add the "ScriptAlias" directive block in the Apache configuration file to map the CGI directory to the "/cgi-bin" URL. Restart Apache.

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

How to view your apache version How to view your apache version Apr 13, 2025 pm 01:15 PM

There are 3 ways to view the version on the Apache server: via the command line (apachectl -v or apache2ctl -v), check the server status page (http://<server IP or domain name>/server-status), or view the Apache configuration file (ServerVersion: Apache/<version number>).

What to do if the apache80 port is occupied What to do if the apache80 port is occupied Apr 13, 2025 pm 01:24 PM

When the Apache 80 port is occupied, the solution is as follows: find out the process that occupies the port and close it. Check the firewall settings to make sure Apache is not blocked. If the above method does not work, please reconfigure Apache to use a different port. Restart the Apache service.

How to view the apache version How to view the apache version Apr 13, 2025 pm 01:00 PM

How to view the Apache version? Start the Apache server: Use sudo service apache2 start to start the server. View version number: Use one of the following methods to view version: Command line: Run the apache2 -v command. Server Status Page: Access the default port of the Apache server (usually 80) in a web browser, and the version information is displayed at the bottom of the page.

How to configure zend for apache How to configure zend for apache Apr 13, 2025 pm 12:57 PM

How to configure Zend in Apache? The steps to configure Zend Framework in an Apache Web Server are as follows: Install Zend Framework and extract it into the Web Server directory. Create a .htaccess file. Create the Zend application directory and add the index.php file. Configure the Zend application (application.ini). Restart the Apache Web server.

How to solve the problem that apache cannot be started How to solve the problem that apache cannot be started Apr 13, 2025 pm 01:21 PM

Apache cannot start because the following reasons may be: Configuration file syntax error. Conflict with other application ports. Permissions issue. Out of memory. Process deadlock. Daemon failure. SELinux permissions issues. Firewall problem. Software conflict.

How to delete more than server names of apache How to delete more than server names of apache Apr 13, 2025 pm 01:09 PM

To delete an extra ServerName directive from Apache, you can take the following steps: Identify and delete the extra ServerName directive. Restart Apache to make the changes take effect. Check the configuration file to verify changes. Test the server to make sure the problem is resolved.

See all articles