使用yum快速搭建LAMP环境

yum 源配置

  1. 挂载光驱
  2. 配置yum源
  • 创建挂载点:[root@localhost mnt]# mkdir /mnt/cdrom
  • 挂载系统安装光盘:

    1
    2
    [root@localhost mnt]# mount /dev/cdrom /mnt/cdrom
    mount: block device /dev/sr0 is write-protected, mounting read-only
  • 配置本地yum源

    1
    2
    3
    4
    5
    6
    7
    8
    [root@localhost mnt]# cd /etc/yum.repos.d/
    [root@localhost yum.repos.d]# ll
    总用量 16
    -rw-r--r--. 1 root root 1926 11月 27 2013 CentOS-Base.repo //本地配置源
    -rw-r--r--. 1 root root 638 11月 27 2013 CentOS-Debuginfo.repo
    -rw-r--r--. 1 root root 630 11月 27 2013 CentOS-Media.repo //网络配置源
    -rw-r--r--. 1 root root 3664 11月 27 2013 CentOS-Vault.repo
    [root@localhost yum.repos.d]#
  • 修改CentOS-Media.repo在baseurl 中修改第2个路径为/mnt/cdrom(即为光盘挂载点)将enabled=0改为1

    1
    2
    3
    4
    5
    6
    7
    8
    9
    [root@localhost yum.repos.d]# vi CentOS-Media.repo
    [c6-media]
    name=CentOS-$releasever - Media
    baseurl=file:///media/CentOS/
    file:///mnt/cdrom/ //改为挂载点
    file:///media/cdrecorder/
    gpgcheck=1
    enabled=1 //改为1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
  • 禁用默认的yum 网络源
    [root@localhost yum.repos.d]# mv CentOS-Base.repo CentOS-Base.repo.bak

  1. yum方式安装gcc,gcc-c++、make、openssl-devel
    [root@localhost yum.repos.d]# yum -y install make gcc gcc-c++ openssl openssl-devel pcre-devel zlib-devel libxml2 libxml2-devel

Apache安装

[root@localhost ~]# yum -y install httpd
开机自启动
[root@localhost ~]# chkconfig httpd on
启动httpd服务
[root@localhost ~]# service httpd start
安装Apache一些扩展
[root@localhost ~]# yum -y install httpd-manual mod_ssl mod_perl mod_auth_mysql

关闭防火墙
[root@localhost ~]# service iptables stop
输入服务器IP地址测试

[root@localhost ~]# vim /var/www/html/

安装MySQL

[root@localhost ~]# yum -y install mysql mysql-server mysql-devel

mysql客户端程序
mysql-server 服务器程序
mysql-devel 开发设计的库

  1. 开机启动
    [root@localhost ~]# chkconfig mysqld on
    [root@localhost ~]# chkconfig --add mysqld
    查看开机启动设置是否成功:

    1
    2
    [root@localhost ~]# chkconfig --list | grep mysql*
    mysqld 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
  2. 启动mysqld服务
    [root@localhost ~]# service mysqld start
    或:[root@localhost ~]# /etc/init.d/mysqld start

  3. 登录客户端设置远程访问,默认没有密码。

  4. 修改密码:mysqladmin -u root password '123456'

    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
    [root@localhost ~]# mysql -uroot -p
    Enter password:
    mysql> use mysql;
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A

    Database changed
    mysql> select host,user from user;
    +-----------------------+------+
    | host | user |
    +-----------------------+------+
    | 127.0.0.1 | root |
    | localhost | |
    | localhost | root |
    | localhost.localdomain | |
    | localhost.localdomain | root |
    +-----------------------+------+
    5 rows in set (0.00 sec)

    mysql> update user set host='%' where user ='root' and host ='127.0.0.1';
    Query OK, 1 row affected (0.00 sec)
    Rows matched: 1 Changed: 1 Warnings: 0

    mysql> delete from user where host!='%';
    Query OK, 4 rows affected (0.00 sec)

    mysql> select host,user from user;
    +------+------+
    | host | user |
    +------+------+
    | % | root |
    +------+------+
    1 row in set (0.00 sec)

    mysql> FLUSH PRIVILEGES;
    Query OK, 0 rows affected (0.00 sec)
  5. 报错

    1
    2
    3
    [root@localhost yum.repos.d]# mysql -uroot -p
    Enter password:
    ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111)

mysql进程使用了同一个socket

1
2
[root@localhost yum.repos.d]# mv /var/lib/mysql/mysql.sock /var/lib/mysql/mysql.sock.bak
[root@localhost yum.repos.d]# service mysqld start

  1. 重启MySQL服务
    [root@localhost ~]# service mysqld restart

    /usr/bin/mysql_secure_installation进行安全性设置

安装PHP

[root@localhost ~]# yum -y install php php-mysql

1
2
3
4
[root@localhost ~]# yum -y install php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt

---
[root@localhost ~]# yum -y install php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc

配置文件路径

PHP配置文件
/etc/php.ini
Apache配置文件
/etc/httpd/conf/httpd.conf
MySQL配置文件
/etc/my.cnf
若my.cnf精简可以从安装目录拷贝
[root@localhost mysql]# cp /usr/share/mysql/my-medium.cnf /etc/my.cnf
mysql解锁mv /var/lib/mysql/mysql.sock /var/lib/mysql/mysql.sock.bak

测试

[root@localhost ~]# service httpd restart
[root@localhost ~]# vim /var/www/html/1.php

卸载

yum卸载:yum -y remove mysql*
rpm卸载:rpm -e mysql