CentOS 6.5下LAMP环境安装

155 阅读2分钟

本文已参与 ⌈新人创作礼⌋ 活动,一起开启掘金创作之路

CentOS 6.5下LAMP环境安装

LAMP :Linux、Apache、Mysql、php

用到的安装包:

[root@www ~]# pwd
/root
[root@www ~]# ls | grep tar
cmake-2.8.12.2.tar.gz
mysql-5.6.32.tar.gz
php-5.6.39.tar.bz2
[root@www ~]# 

环境准备:

1、关闭防火墙和selinux
[root@www ~]# service iptables stop
[root@www ~]# chkconfig iptables off
[root@www ~]# setenforce 0
[root@www ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux

2、设置主机名
[root@www ~]# mv /etc/sysconfig/network /etc/sysconfig/network.bak
[root@www ~]# cat >/etc/sysconfig/network <<eof
> NETWORKING=yes
> HOSTNAME=www.hs.com
> eof
[root@www ~]# 

3、安装编译需要的依赖包

yum install ncurses-devel httpd-devel gcc* libxml2-devel -y

4、安装cmake编译工具

#tar -zxvf cmake-2.8.12.2.tar.gz
#cd cmake-2.8.12.2
# ./bootstrap
# gmake
# make install

开始安装mysql

1、创建账户和组
[root@www ~]# groupadd mysql  
[root@www ~]# useradd mysql -g mysql -M -s /sbin/nologin

2、解压安装包
[root@www ~]# tar -zxvf mysql-5.6.32.tar.gz && cd mysql-5.6.32

3、编译安装
[root@www mysql-5.6.32]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/tmp/mysqld.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci

[root@www mysql-5.6.32]# make && make install   (时间很长)
[root@www mysql-5.6.32]# chown mysql:mysql /usr/local/mysql
[root@www mysql-5.6.32]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
[root@www mysql-5.6.32]# cp support-files/my-default.cnf  /etc/my.cnf
编辑配置文件my.php
在[mysqld]添加如下三行
datadir=/usr/local/mysql/data
default-storage-engine=InnoDB
lower_case_table_names=1


#设置环境变量
[root@localhost ~]# vi /root/.bash_profile
 在修改PATH=$PATH:$HOME/bin为:
 PATH=$PATH:$HOME/bin:/usr/local/mysql/bin:/usr/local/mysql/lib
[root@localhost ~]# source /root/.bash_profile //使刚才的修改生效
 
#传统启动方式启动

[root@www ~]# mysqld_safe --user=mysql &  

#启动mysql,看是否成功  
netstat -tnl|grep 3306

#制作成服务启动
[root@www mysql-5.6.32]#cp support-files/mysql.server /etc/init.d/mysql
[root@www ~]# chmod 754 /etc/init.d/mysql
相关命令:
service mysql restart    [stop/restart]

#添加到开机启动项
chkconfig --add mysql

#修改root密码(前提是mysql启动成功)
/usr/local/mysql/bin/mysqladmin -u root password
 

开始安装php

[root@www ~]# 
[root@www ~]# tar -jxvf php-5.6.39.tar.bz2
[root@www ~]# cd php-5.6.39
[root@www php-5.6.39]# ./configure --prefix=/usr/local/php --with-apxs2=/usr/sbin/apxs --with-config-file-path=/etc  --with-mysql=/usr/local/mysql --with-mysqli
[root@www php-5.6.39]# make&&make install&&make test


产生并修改php配置文件
cp  php.ini-production /etc/php.ini

vim /etc/httpd/conf/httpd.conf   ##### 建议修改前先备份一个配置文件
打开httpd.conf文件,找到:(大约780行)
AddType  application/x-compress .Z
AddType application/x-gzip .gz .tgz

添加如下内容:

AddType application/x-httpd-php-source .phps
AddType application/x-httpd-php .php

检查模块   LoadModule php5_module modules/libphp5.so    是否已经添加(大约215行) ;
检查  libphp5.so  文件是否存在;


第四步:重启httpd服务,并验证安装

[root@www conf]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd: httpd: apr_sockaddr_info_get() failed for www.hs.com
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
                                                           [  OK  ]
[root@www conf]# 
解决报错方法:
找到#ServerName www.example.com:80(大约275行),在下方增加一行 ServerName www.hs.com:80


[root@localhost ~]# cd /var/www/html/
[root@localhost html]# cat phpinfo.php 
<?php
phpinfo();
?>
[root@localhost html]# 

cat > phpinfo.php <<eof
<?php
phpinfo();
?>
eof

使用浏览器访问http://ip/phpinfo.php

使用浏览器访问http://192.168.100.65/phpinfo.php

验证连库

<?php
$mysql_conf = array(
    'host'    => '127.0.0.1:3306', 
    'db'      => 'mysql', 
    'db_user' => 'root', 
    'db_pwd'  => 'yourpasswd', 
    );

$mysqli = @new mysqli($mysql_conf['host'], $mysql_conf['db_user'], $mysql_conf['db_pwd']);
if ($mysqli->connect_errno) {
    die("could not connect to the database:\n" . $mysqli->connect_error);//诊断连接错误
}
$mysqli->query("set names 'utf8';");//编码转化
$select_db = $mysqli->select_db($mysql_conf['db']);
if (!$select_db) {
    die("could not connect to the db:\n" .  $mysqli->error);
}$sql = "select host,user,password from user where user = 'root' limit 1;";
$res = $mysqli->query($sql);
if (!$res) {
    die("sql error:\n" . $mysqli->error);
}
 while ($row = $res->fetch_assoc()) {
        var_dump($row);
    }

$res->free();
$mysqli->close();
?>

启动服务

service mysql restart

service httpd restart

设置开机启动

chkconfig httpd on

chkconfig mysql on