Ubuntu的Server和Desktop上都配过php的LAMP环境,步骤没什么区别,相关的教程网上也很多,这里把自己的配置过程记录下来,省得每次都google…供参考
整个环境用到Apache2、MySQL、php5和phpMyAdmin
1、安装Apache2
1
|
$
sudo
apt
-
get
install
apache2
|
装完后在浏览器中访问 http://[server-ip-address] 如看到类似下图的页面,则apache2安装成功
Ubuntu下,apache的配置信息在/etc/apache2目录,默认根目录在/var/www
2、安装MySQL,修改默认字符集
1
|
$
sudo
apt
-
get
install
mysql
-
server
mysql
-
client
|
安装过程需要设置root的密码。Ubuntu下,MySQL的配置信息在/etc/mysql目录
MySQL默认的字符集是latin1
为了避免中文可能带来的乱码问题,将默认字符集改成utf-8,具体可以参考这篇文章,修改 /etc/mysql/my.cnf 文件,在相应位置添加
1
2
3
4
5
6
7
8
9
10
|
[
client
]
default
-
character
-
set
=
utf8
[
mysql
]
default
-
character
-
set
=
utf8
[
mysqld
]
collation
-
server
=
utf8_unicode_ci
init
-
connect
=
'SET NAMES utf8'
character
-
set
-
server
=
utf8
|
修改后是这样的
1
|
$
apt
-
get
install
php5
libapache2
-
mod
-
php5
|
装完之后重启apache
1
2
3
4
|
$
sudo
service
apache2
restart
*
Restarting
web
server
apache2
.
.
.
waiting
[
OK
]
$
|
在 /var/www 目录下新建 info.php 文件,内容如下
1
|
<?php
phpinfo
(
)
?>
|
在浏览器中访问 http://[server-ip-address]/info.php 如看到类似下图的页面,则php5安装成功
在 /var/www 目录下新建 testmysql.php 文件,测试php连接MySQL,文件内容如下
1
2
3
4
5
6
7
8
9
|
<?php
$link
=
mysql_connect
(
"localhost"
,
"[用户名]"
,
"[密码]"
)
;
if
(
!
$link
)
{
die
(
'Could not connect: '
.
mysql_error
(
)
)
;
}
else
echo
"MySQL连接成功"
;
mysql_close
(
$link
)
;
?>
|
在浏览器中访问 http://[server-ip-address]/testmysql.php,HTTP错误500…
4、安装php5的MySQL模块,安装php需要的其它模块
默认安装完的php5是不能与MySQL交互的。通过命令
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
$
sudo
apt
-
cache
search
php5
libapache2
-
mod
-
php5
-
server
-
side
,
HTML
-
embedded
scripting
language
(
Apache
2
module
)
php
-
pear
-
PEAR
-
PHP
Extension
and
Application
Repository
php5
-
server
-
side
,
HTML
-
embedded
scripting
language
(
metapackage
)
php5
-
cgi
-
server
-
side
,
HTML
-
embedded
scripting
language
(
CGI
binary
)
php5
-
cli
-
command
-
line
interpreter
for
the
php5
scripting
language
php5
-
common
-
Common
files
for
packages
built
from
the
php5
source
php5
-
curl
-
CURL
module
for
php5
php5
-
dbg
-
Debug
symbols
for
PHP5
php5
-
dev
-
Files
for
PHP5
module
development
php5
-
gd
-
GD
module
for
php5
php5
-
gmp
-
GMP
module
for
php5
php5
-
ldap
-
LDAP
module
for
php5
php5
-
mysql
-
MySQL
module
for
php5
php5
-
odbc
-
ODBC
module
for
php5
php5
-
pgsql
-
PostgreSQL
module
for
php5
php5
-
pspell
-
pspell
module
for
php5
php5
-
recode
-
recode
module
for
php5
php5
-
snmp
-
SNMP
module
for
php5
php5
-
sqlite
-
SQLite
module
for
php5
php5
-
tidy
-
tidy
module
for
php5
php5
-
xmlrpc
-
XML
-
RPC
module
for
php5
php5
-
xsl
-
XSL
module
for
php5
cakephp
-
MVC
rapid
application
development
framework
for
PHP
dwoo
-
PHP5
template
engine
jffnms
-
web
-
based
Network
Management
System
(
NMS
)
for
IP
networks
libapache2
-
mod
-
php5filter
-
server
-
side
,
HTML
-
embedded
scripting
language
(
apache
2
filter
module
)
libexpect
-
php5
-
expect
module
for
PHP
5
libgv
-
php5
-
PHP5
bindings
for
graphviz
libkohana2
-
modules
-
php
-
lightweight
PHP5
MVC
framework
(
extension
modules
)
libkohana2
-
php
-
lightweight
PHP5
MVC
framework
libkohana3
.
1
-
core
-
php
-
PHP5
framework
core
classes
libkohana3
.
1
-
php
-
PHP5
framework
metapackage
libkohana3
.
2
-
core
-
php
-
PHP5
framework
core
classes
libkohana3
.
2
-
php
-
PHP5
framework
metapackage
libow
-
php5
-
Dallas
1
-
wire
support
:
PHP5
bindings
libphp
-
jpgraph
-
Object
oriented
graph
library
for
php5
libphp
-
jpgraph
-
examples
-
Object
oriented
graph
library
for
php5
(
examples
)
php
-
auth
-
PHP
PEAR
modules
for
creating
an
authentication
system
php
-
codesniffer
-
tokenises
PHP
code
and
detects
violations
of
a
defined
set
of
coding
standards
php
-
doc
-
Documentation
for
PHP5
php
-
imlib
-
PHP
Imlib2
Extension
php5
-
adodb
-
Extension
optimising
the
ADOdb
database
abstraction
library
php5
-
auth
-
pam
-
A
PHP5
extension
for
PAM
authentication
php5
-
enchant
-
Enchant
module
for
php5
php5
-
exactimage
-
fast
image
manipulation
library
(
PHP
bindings
)
php5
-
ffmpeg
-
audio
and
video
support
via
ffmpeg
for
php5
php5
-
fpm
-
server
-
side
,
HTML
-
embedded
scripting
language
(
FPM
-
CGI
binary
)
php5
-
geoip
-
GeoIP
module
for
php5
php5
-
imagick
-
ImageMagick
module
for
php5
php5
-
imap
-
IMAP
module
for
php5
php5
-
interbase
-
interbase
/
firebird
module
for
php5
php5
-
intl
-
internationalisation
module
for
php5
php5
-
lasso
-
Library
for
Liberty
Alliance
and
SAML
protocols
-
PHP
5
bindings
php5
-
librdf
-
PHP5
language
bindings
for
the
Redland
RDF
library
php5
-
mapscript
-
php5
-
cgi
module
for
MapServer
php5
-
mcrypt
-
MCrypt
module
for
php5
php5
-
memcache
-
memcache
extension
module
for
PHP5
php5
-
memcached
-
memcached
extension
module
for
PHP5
,
uses
libmemcached
php5
-
midgard2
-
Midgard2
Content
Repository
-
PHP5
language
bindings
and
module
php5
-
ming
-
Ming
module
for
php5
php5
-
mysqlnd
-
MySQL
module
for
php5
(
Native
Driver
)
php5
-
ps
-
ps
module
for
PHP
5
php5
-
radius
-
PECL
radius
module
for
PHP
5
php5
-
remctl
-
PECL
module
for
Kerberos
-
authenticated
command
execution
php5
-
rrd
-
rrd
module
for
PHP
5
php5
-
sasl
-
Cyrus
SASL
extension
for
PHP
5
php5
-
suhosin
-
advanced
protection
module
for
php5
php5
-
svn
-
PHP
Bindings
for
the
Subversion
Revision
control
system
php5
-
sybase
-
Sybase
/
MS
SQL
Server
module
for
php5
php5
-
tokyo
-
tyrant
-
PHP
interface
to
Tokyo
Cabinet'
s
network
interface
,
Tokyo
Tyrant
php5
-
uuid
-
OSSP
uuid
module
for
php5
php5
-
xcache
-
Fast
,
stable
PHP
opcode
cacher
php5
-
xdebug
-
Xdebug
Module
for
PHP
5
phpunit
-
Unit
testing
suite
for
PHP5
$
|
查看php5的模块都有哪些,这里安装以下模块
1
|
$
sudo
apt
-
get
install
php5
-
mysql
php5
-
curl
php5
-
gd
php5
-
intl
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
|
重启apache服务器,在浏览器中刷新 http://[server-ip-address]/info.php 可以看到类似下图的页面,ctrl+f mysql可以发现php的MySQL模块已经安装
刷新 http://[server-ip-address]/testmysql.php,页面如下
如果页面乱码,可以参考这篇文章,修改 /etc/apache2/apache2.conf,在文件尾加上
1
|
AddDefaultCharset
UTF
-
8
|
5、安装phpMyAdmin管理MySQL
1
|
$
sudo
apt
-
get
install
phpmyadmin
|
按照提示依次设置即可,过程截图如下
phpMyAdmin的地址为 http://[server-ip-address]/phpmyadmin
参 考
http://imcn.me/html/y2012/12401.html
http://www.howtoforge.com/installing-apache2-with-php5-and-mysql-support-on-ubuntu-12.04-lts-lamp
http://www.linuxidc.com/Linux/2012-05/61079.htm