搭建LAMP环境(源码方式)

时间:2022-03-13 03:06:46

一 关于LAMP

LAMP:Linux + Apache + MySQL + PHP

WAMP:Windows + Apache + MySQL + PHP


安装顺序:首先MySQL,然后Apache,最后PHP


二 搭建LAMP

第一步,安装MySQL

--1.安装Development tools和ncurses-devel
[root@serv01 ~]# yum grouplist|grep Devel
  Additional Development
  Desktop Platform Development
  Development tools
  Server Platform Development
[root@serv01 ~]# yum groupinstall"Development tools" -y
[root@serv01 ~]# yum install ncurses-devel-y
 
--2.解压
[root@serv01 ~]# tar -xf mysql-5.1.58.tar.gz-C /usr/src/
      
#文件错误,重新拷贝
[root@serv01 ~]# tar -xf mysql-5.1.58.tar.gz-C /usr/src/
tar: Skipping to next header
tar: Exiting with failure status due toprevious errors
[root@serv01 ~]# cd /usr/src/mysql-5.1.58/
#帮助文件
[root@serv01 ~]#./configure —help
--3.配置
[root@serv01 mysql-5.1.58]# ./configure--prefix=/usr/local/mysql --with-extra-charsets=gbk,gb2312—with-plugins=partition,innobase,innodb_plugin,myisam
--4.编译
[root@serv01 mysql-5.1.58]# make
--5.安装
[root@serv01 mysql-5.1.58]# make install
 
--6.拷贝配置文件和执行脚本
[root@serv02 mysql-5.1.58]# cpsupport-files/my-medium.cnf /etc/my.cnf
cp: overwrite `/etc/my.cnf'? y
[root@serv02 mysql-5.1.58]# cpsupport-files/mysql.server /etc/init.d/mysqld
[root@serv02 mysql-5.1.58]# chmod a+x/etc/init.d/mysqld
[root@serv02 mysql-5.1.58]# ls -l/etc/init.d/mysqld
-rwxr-xr-x. 1 root root 12302 Aug 15 23:36/etc/init.d/mysqld
 
--7.创建数据文件的存放路径,并修改my.cnf和mysqld文件
[root@serv02 mysql-5.1.58]# mkdir /usr/local/mysql/data
[root@serv02 mysql-5.1.58]# vim /etc/my.cnf
[root@serv02 mysql-5.1.58]# grep"^datadir" /etc/my.cnf -n
27:datadir           =/usr/local/mysql/data
 
[root@serv02 mysql-5.1.58]# vim/etc/init.d/mysqld
[root@serv02 mysql-5.1.58]# sed"46,47p" /etc/init.d/mysqld -n
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
 
--8.新增用户,并加入MySQL的用户
[root@serv02 mysql-5.1.58]# groupadd mysql
[root@serv02 mysql-5.1.58]# useradd -g mysqlmysql
[root@serv02 mysql-5.1.58]#./scripts/mysql_install_db --user=mysql
 
--9.启动MySQL,进入/usr/local/mysql/bin/,执行mysql,查询MySQL版本
[root@serv02 mysql-5.1.58]#/etc/init.d/mysqld start
Starting MySQL. SUCCESS!
[root@serv02 mysql-5.1.58]# cd/usr/local/mysql/bin/
[root@serv02 bin]# ./mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.58-log Sourcedistribution
 
Copyright (c) 2000, 2010, Oracle and/or itsaffiliates. All rights reserved.
This software comes with ABSOLUTELY NOWARRANTY. This is free software,
and you are welcome to modify andredistribute it under the GPL v2 license
 
Type 'help;' or '\h' for help. Type '\c' toclear the current input statement.
 
mysql> select version();
+------------+
| version() |
+------------+
| 5.1.58-log |
+------------+
1 row in set (0.00 sec)
 
mysql> create database larry defaultcharset utf8;
Query OK, 1 row affected (0.00 sec)
 
mysql> use larry;
Database changed
mysql> show tables;
Empty set (0.00 sec)
 
mysql> create table t_user(id int(11)primary key auto_increment, name varchar(20));
Query OK, 0 rows affected (0.01 sec)
 
mysql> insert into t_user(name)values('larrywen');
Query OK, 1 row affected (0.00 sec)
 
mysql> insert into t_user(name)values('justdb');
Query OK, 1 row affected (0.00 sec)
 
mysql> insert into t_user(name)values('wgb');
Query OK, 1 row affected (0.00 sec)
 
mysql> select * from t_user;
+----+----------+
| id | name     |
+----+----------+
|  1 |larrywen |
|  2 |justdb   |
|  3 |wgb      |
+----+----------+
3 rows in set (0.00 sec)
 
mysql> create table t_log(id int(11)primary key auto_increment, content varchar(20), t_user_id int(11), constraintfk_larry_t_log_usr_id foreign key(t_user_id) references t_user(id));
Query OK, 0 rows affected (0.01 sec)
 
mysql> insert into t_log(content,t_user_id) values('Logining System', 1);
Query OK, 1 row affected (0.01 sec)
 
mysql> select * from t_log;
+----+-----------------+-----------+
| id | content         | t_user_id |
+----+-----------------+-----------+
|  1 |Logining System |         1 |
+----+-----------------+-----------+
1 row in set (0.00 sec)
       mysql> exit
Bye
 
--10.修改mysql目录的所有者和组拥有者
[root@serv02 mysql-5.1.58]# cd/usr/local/mysql
[root@serv02 mysql]# chown -R mysql .
[root@serv02 mysql]# chgrp -R mysql .

第二步,安装Apache

--1.解压
[root@serv02 httpd-2.2.21]# tar -xfhttpd-2.2.21.tar.gz -C /usr/src
--2.进入安装目录,检查配置
[root@serv02 httpd-2.2.21]# cd/usr/src/httpd-2.2.21/
[root@serv02 httpd-2.2.21]# ./configure--help
[root@serv02 httpd-2.2.21]# ./configure--prefix=/usr/local/apache --enable-modules=all --enable-mods-shared=all--enable-so --with-mpm=worker
checking for zlib location... not found
#如果出现zlib not found,安装zlib-devel
[root@serv02 httpd-2.2.21]# yum installzlib-devel -y
 
--3.编译
[root@serv02 httpd-2.2.21]# make
 
--4.安装
[root@serv02 bin]# make install
 
--5.进入/usr/local/apache/bin/目录,启动
[root@serv02 apache]# cd/usr/local/apache/bin/
[root@serv02 bin]# ./apachectl -k start
httpd: apr_sockaddr_info_get() failed forserv02.host.com
httpd: Could not reliably determine theserver's fully qualified domain name, using 127.0.0.1 for ServerName
[root@serv02 bin]# netstat -langput | grephttpd
#出现上述问题,编辑httpd.conf文件,加上ServerName serv02.host.com;编辑hosts文件,加上192.168.1.12 serv02.host.com(注意,本机IP是192.168.1.12)
[root@serv02 bin]# vim ../conf/httpd.conf
[root@serv02 bin]# grep"ServerName" /usr/local/apache/conf/httpd.conf
# ServerName gives the name and port that theserver uses to identify itself.
#ServerName www.example.com:80
ServerName serv02.host.com
[root@serv02 bin]# echo "192.168.1.12serv02.host.com" >> /etc/hosts
hosts       hosts.allow  hosts.deny  
[root@serv02 bin]# echo "192.168.1.12serv02.host.com" >> /etc/hosts
[root@serv02 bin]# tail -n1 /etc/hosts
192.168.1.12 serv02.host.com
#再次启动,查看端口,成功
[root@serv02 bin]# ./apachectl -k start
[root@serv02 bin]# netstat -langput | grephttpd
tcp       0      0 :::80                       :::*                        LISTEN      20441/httpd
 
--6.测试
浏览器输入http://192.168.1.12/ 如果出现“It works”则成功

第三步,安装PHP

--1.解压
[root@serv02 ~]# tar -xf php-5.3.6.tar.bz2 -C/usr/src/
--2.进入/usr/src/php-5.3.6/目录,配置
[root@serv02 php-5.3.6]# cd/usr/src/php-5.3.6/
[root@serv02 php-5.3.6]# ./configure —help
[root@serv02 php-5.3.6]# ./configure--prefix=/usr/local/php5 --with-apxs2=/usr/local/apache/bin/apxs--with-mysql-sock=/tmp/mysql.sock --with-mysql=/usr/local/mysql/
 
checking libxml2 install dir... no
checking for xml2-config path...
configure: error: xml2-config not found.Please check your libxml2 installation.
#如果出现上述错误,安装libxml2
[root@serv02 php-5.3.6]# yum install libxml2*-y
 
+--------------------------------------------------------------------+
| License:                                                          |
| This software is subject to the PHPLicense, available in this     |
| distribution in the file LICENSE.  By continuing this installation |
| process, you are bound by the terms of thislicense agreement.     |
| If you do not agree with the terms of thislicense, you must abort |
| the installation process at thispoint.                            |
+--------------------------------------------------------------------+
#如果出现如下文本,则证明配置成功
 
Thank you for using PHP.
 
--3.编译
[root@serv02 php-5.3.6]# make
 
--4.安装
[root@serv02 php-5.3.6]# make install
 
--5.拷贝php.ini文件,修改httpd.conf文件
[root@serv02 bin]# cp php.ini-development/usr/local/php5/lib/php.ini
[root@serv02 bin]# grep -e"AddHandler" -e "AddType" /usr/local/apache/conf/httpd.conf
   AddHandler php5-script .php
AddType text/html .php

三 测试

--1.修改root用户密码,创建测试数据库和表
[root@serv02 bin]# ./mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.58-log Sourcedistribution
 
Copyright (c) 2000, 2010, Oracle and/or itsaffiliates. All rights reserved.
This software comes with ABSOLUTELY NOWARRANTY. This is free software,
and you are welcome to modify andredistribute it under the GPL v2 license
 
Type 'help;' or '\h' for help. Type '\c' toclear the current input statement.
 
mysql> set password=password("helloworld");
Query OK, 0 rows affected (0.00 sec)
 
mysql> exit
Bye
[root@serv02 bin]# ./mysql -uroot-phelloworld
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.1.58-log Sourcedistribution
 
Copyright (c) 2000, 2010, Oracle and/or itsaffiliates. All rights reserved.
This software comes with ABSOLUTELY NOWARRANTY. This is free software,
and you are welcome to modify andredistribute it under the GPL v2 license
 
Type 'help;' or '\h' for help. Type '\c' toclear the current input statement.
 
mysql> use larry
Database changed
mysql> show tables;
+-----------------+
| Tables_in_larry |
+-----------------+
| t_log           |
| t_user          |
+-----------------+
2 rows in set (0.00 sec)
 
mysql> select * from t_user;
+----+----------+
| id | name     |
+----+----------+
|  1 |larrywen |
|  2 |justdb   |
|  3 |wgb      |
+----+----------+
3 rows in set (0.00 sec)
 
--2.新建测试php文件
 
[root@serv02 php5]# cd/usr/local/apache/htdocs/
[root@serv02 htdocs]# vim index.php
[root@serv02 htdocs]# cat index.php
<?php
       phpinfo();
?>
[root@serv02 htdocs]# vim user_list.php
[root@serv02 htdocs]# pwd
/usr/local/apache/htdocs
[root@serv02 htdocs]# vim user_list.php
[root@serv02 htdocs]# cat user_list.php
<?php
       $conn=mysql_connect("localhost","root", "helloworld");
       mysql_select_db("larry",$conn);
       $users=mysql_query("select* from t_user");
 
       while(!!$user=mysql_fetch_array($users)){
              echo$user["id"]."------->".$user["name"]."<br>";     
       }
       mysql_close();
?>
[root@serv02 htdocs]#
 
--3.浏览器输入http://192.168.1.12/index.php,如果出现php相关的配置信息,则证明配置成功。输入http://192.168.1.12/user_list.php,如果能出现以下的信息,则证明PHP、MySQL、Apache整合成功。
 
1------->larrywen
2------->justdb
3------->wgb



  我的邮箱wgbno27@163.com   新浪微博@Wentasy27         
  微信公众平台:JustOracle(微信号:justoracle)
  数据库技术交流群:336882565(加群时验证 From CSDN XXX)
 By Larry Wen


搭建LAMP环境(源码方式) 搭建LAMP环境(源码方式) 搭建LAMP环境(源码方式)
@Wentasy 博文仅供参考,欢迎大家来访。如有错误之处,希望批评指正。原创博文如需转载请注明出处,谢谢 :) [CSDN博客]