LAMP源码环境搭建技术文档

LAMP源码环境搭建技术文档


一、LAMP环境的介绍

1.LAMP组件介绍

​ 在安装LAMP之前,对LAMP进行了解是肯定的了, 首先就说说其含义:
LAMP指的是linux,Apache,Mysql以及Php的简称,通过大量的生产环境的实践证明,在网络应用和开发环境方面,LAMP组合是非常棒的黄金搭档,它们的结合提供了非常强大功能。
linux作为底层的操作系统,提供灵活且可定制性的应用平台,为其他组件稳定高效的运行在其之上提供了保障。
Apache作为Web服务器,提供了功能强大,稳定且支撑能力突出的web平台,为网站提供了强力支柱。
Mysql也是一款非常优秀的数据库,从其产生的从多衍生数据库就可见证明其强大。
Php是一种开发源代码的多用途脚本语言,可嵌入html中,适用于web开发,且其编写的数据可访问mysql数据库及linux提供的动态内容。
(此章摘抄)

2.安装相应软件包

由于整个环境会涉及比较多的依赖关系包,所以我们先将一些依赖包装上。(根据不同的环境可能不仅限于这些包)

# yum groupinstall "Development tools" -y
# yum groupinstall "Desktop Platform Development" -y   桌面开发工具包(图形化相关包)
# yum install cmake
# yum install ncurses-devel
# yum -y install pcre-devel

二、Apache源码安装

1.下载Apache及相关依赖包

1.apr(Apache依赖包) apr-1.5.2.tar.bz2

2.apr-util(Apache依赖包)apr-util-1.5.4.tar.bz2

3.pcre(Apache依赖包)

4.Apache httpd-2.4.12.tar.bz2

2.安装Apache及相关依赖包

(将软件包解压到目录 /usr/src/)

2-1.解压安装apr

# tar xf apr-1.5.2.tar.bz2 -C /usr/src/
# cd /usr/src/apr-1.5.2
# ./configure
# make
# make install

2-2.解压安装apr-util

# tar xf apr-util-1.5.4.tar.bz2 -C /usr/src/
# cd /usr/src/apr-util-1.5.4/
# ./configure --with-apr=/usr/local/apr/bin/apr-1-config    指定软件apr的路径
# make
# make install

2-3.安装pcre

# yum -y install pcre-devel

2-4.安装Apache

[root@lamp src]# cd httpd-2.4.12/
配置:
[root@lamp httpd-2.4.12]# vim apache.sh
  1 ./configure \
  2 --enable-modules=all \
  3 --enable-mods-shared=all \
  4 --enable-so \
  5 --enable-rewrite \
  6 --with-mpm=prefork \
  7 --with-apr=/usr/local/apr/bin/apr-1-config \
  8 --with-apr-util=/usr/local/apr/bin/apu-1-config

给相应权限并执行脚本
[root@lamp httpd-2.4.12]# chmod +x apache.sh
[root@lamp httpd-2.4.12]# ./apache.sh
[root@lamp httpd-2.4.12]# make && make install
确认是否安装成功Apache:
[root@lamp httpd-2.4.12]#  ls /usr/local/apache2/    
bin  build  cgi-bin  conf  error  htdocs  icons  include  logs  man  manual  modules
注意:
确认这个目录产生后,说明apache编译安装成功

配置参数说明:
# ./configure \
--enable-modules=all \           加载所有支持模块
--enable-mods-shared=all \       共享方式加载大部分常用的模块
--enable-so \                   启动动态模块加载功能
--enable-rewrite  \             启用地址重写功能
--with-mpm=prefork \    插入式并行处理模块,称为多路处理模块,Prefork 是类UNIX平台上默认的MPM1)prefork
    多进程模型,每个进程响应一个请求
(2)worker
    多进程多线程模型,每个线程处理一个用户请求 
(3)event(最优)
    事件驱动模型,多进程模型,每个进程响应多个请求
--with-apr=/usr/local/apr/bin/apr-1-config \    指定依赖软件apr路径
--with-apr-util=/usr/local/apr/bin/apu-1-config 

3.配置Apache服务

3-1.启动Apache服务

为了方便启动,做成脚本启动:
[root@lamp httpd-2.4.12]# # cp /usr/local/apache2/bin/apachectl /etc/init.d/apache
[root@lamp httpd-2.4.12]# # service apache start

3-2.查看Apache服务端口情况

[root@lamp ~]# netstat -ntlp | grep 80
tcp        0      0 :::80                       :::*                        LISTEN      4836/httpd  

3-3.关闭防火墙和selinux

1.关闭iptables
[root@lamp ~]# service iptables stop
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]

2.关闭selinux
方法1.使用命令 setenforce 0 系统重启失效。(不需要重启,立即生效)
方法2.编辑配置文件/etc/selinux/config将SELINUX=enforcing改为disabled状态( 须重启生效)
1.[root@lamp ~]# setenforce 0

2.[root@lamp ~]# cat /etc/selinux/config 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

3-4.网页测试

打开网页,输入http://ip即可查看网页内容

三、源码安装MySQL

1.下载mysql及相关软件包

mysql版本 mysql-5.6.25.tar.gz
需求:
1. 安装目录     /usr/local/mysql
2. 数据目录     /usr/local/mysql/data
3. 端口               3306
4. socket文件     /tmp/mysql.sock

2.安装mysql

前提
安装cmake
安装ncurses
安装bison

1.解压mysql
[root@lamp LAMP]# tar xf mysql-5.6.25.tar.gz -C /usr/src/
[root@lamp LAMP]# cd /usr/src/mysql-5.6.25
[root@lamp mysql-5.6.25]# pwd
/usr/src/mysql-5.6.25

2. 安装
 1)创建相应的用户(如果存在就省略)
# useradd mysql -r -s /sbin/nologin
-r:创建系统用户(UID500以内)
-s:指定默认的shell(非交互式)
2)配置
[root@lamp mysql-5.6.25]#  vim cmake.sh
cmake . \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql/ \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DSYSCONFDIR=/usr/local/mysql/etc \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DMYSQL_USER=mysql

[root@lamp mysql-5.6.25]#  chmod +x cmake.sh
[root@lamp mysql-5.6.25]#  ./cmake.sh
[root@lamp mysql-5.6.25]# make && make install

参数说明:
cmake . \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql/ \       指定安装路径
-DMYSQL_DATADIR=/usr/local/mysql/data \          指定数据目录
-DENABLED_LOCAL_INFILE=1 \                      开启加载外部文件功能,1开启;0关闭
-DWITH_INNOBASE_STORAGE_ENGINE=1 \          开启innodb存储引擎
-DSYSCONFDIR=/usr/local/mysql/etc \         初始化参数配置文件路径
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \         socket文件路径
-DMYSQL_TCP_PORT=3306 \                       指定端口号
-DDEFAULT_CHARSET=utf8 \                      默认字符集
-DDEFAULT_COLLATION=utf8_general_ci \       默认校对规则(排序规则)
-DWITH_EXTRA_CHARSETS=all \                     扩展字符集
-DMYSQL_USER=mysql                             运行mysql用户身份

3.配置MySQL服务

1.初始化MySQL数据库
[root@lamp ~]# cd /usr/local/mysql/
[root@lamp mysql]# scripts/mysql_install_db --user=mysql

2. 找到mysql的启动脚本,拷贝到/etc/init.d目录里
进入到安装路径里/usr/local/mysql/里
[root@lamp mysql]# pwd
/usr/local/mysql
[root@lamp mysql]# cp support-files/mysql.server /etc/init.d/mysql25

3.启动mysql服务
[root@lamp mysql]# service mysql25 start
Starting MySQL. ERROR! The server quit without updating PID file (/usr/local/mysql/data/lamp.itcast.cc.pid).

分析:
[root@lamp data]# ll -d /usr/local/mysql/data/
drwxr-xr-x 3 root root 4096 Sep  7 08:48 /usr/local/mysql/data/

解决:
[root@lamp data]# chown mysql. -R /usr/local/mysql/data/
[root@lamp data]# ll -d /usr/local/mysql/data/
drwxr-xr-x 3 mysql mysql 4096 Sep  7 08:48 /usr/local/mysql/data/

4启动数据库:
[root@lamp mysql]# service mysql25 start

5.检查mysql服务是否启动
[root@lamp mysql]#  netstat -tulnp |grep 3306
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      24351/mysqld 

6.给mysql数据库的管理员root设置密码
[root@lamp mysql]# ./bin/mysqladmin -u root password '123'
Warning: Using a password on the command line interface can be insecure.
[root@lamp mysql]# ./bin/mysqladmin -u root -h lamp.itcast.cc password '123'
Warning: Using a password on the command line interface can be insecure.

7.设置环境变量
在文件/etc/profile末尾添加
PATH=/usr/local/mysql/bin:$PATH
or
export PATH=$PATH:/usr/local/mysql/bin
[root@lamp mysql]# source /etc/profile  or
[root@lamp mysql]# . /etc/profile
使修改的环境变量立即生效

7.登录mysql数据库:
[root@lamp mysql]# mysql -uroot -p123
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.25 Source distribution

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit

四、源码安装php

1.下载php

版本 php-5.6.11.tar.xz

2.安装php

1.解压
  进入解压目录

2.配置
[root@server php-5.6.11]# vim php.sh
./configure \
--with-apxs2=/usr/local/apache2/bin/apxs \
--with-mysql=/usr/local/mysql/ \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-pdo-mysql=/usr/local/mysql/ \
--with-zlib \
--with-zlib-dir=/usr/local/mysql/zlib \
--with-curl \
--enable-zip \
--with-gd \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--enable-sockets \
--with-xmlrpc \
--enable-soap \
--enable-opcache \
--enable-mbstring \
--enable-mbregex \
--enable-pcntl \
--enable-shmop \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-calendar \
--enable-bcmath

配置错误:
checking for cURL support... yes
checking for cURL in default path... not found
configure: error: Please reinstall the libcurl distribution -
    easy.h should be in <curl-dir>/include/curl/
解决:
yum -y install libcurl-devel

3.编译
make            //make成功后,会显示让你make test,不用做
4.安装
make install

//确认php成功安装:
[root@server php-5.6.11]# ls /usr/local/apache2/modules/libphp5.so  
注意:确认有这个libphp5.so模块文件,就表示编译php成功


php编译相关参数介绍:
--with-apxs2=/usr/local/apache2/bin/apxs    //指定apxs路径
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,with-apxs2构建apache2.0版本的处理模块
--with-config-file-path 和 --with-config-file-scan-dir   //在指定 php 配置文件的路径
--with-mysql 和 --with-mysqli                //在指定你的 mysql 的位置和它的相关工具
--with-iconv-dir
--with-freetype-dir
--with-jpeg-dir
--with-png-dir
--with-gd
--with-zlib
--with-libxml-dir                   //这些都是在启用对某种文件的支持
--with-curl 和 --with-curlwrappers   //用于支持 curl 函数,此函数允许你用不同的协议连接和沟通                                    不同的服务器
--with-openssl,--with-mhash,--with-mcrypt   //这都是和加密有关的参数,启用它们是为了让php可以                                         更好的支持各种加密。
--enable-bcmath                         //高精度数学运算组件。
--enable-shmop和 --enable-sysvsem        //使得你的PHP系统可以处理相关的IPC函数.
//IPC是一个Unix标准通讯机制,它提供了使得在同一台主机不同进程之间可以互相通讯的方法。
--enable-inline-optimization        //栈堆指针和优化线程。
--enable-pcntl                      //多线程优化。

with-apxs2      调用apache加载模块支持PHP
gd              画图库
libiconv        字符变换转换
libmcrypt       字符加密
mcrypt          字符加密
mhash           哈希运算

五、后续配置

1.配置Apache和php联系

1. 修改apache主配置文件
# vim /usr/local/apache2/conf/httpd.conf

//配置语言支持
LoadModule negotiation_module modules/mod_negotiation.so    此模块打开注释
Include conf/extra/httpd-languages.conf         打开此选项,扩展配置文件就生效了
...

//打开对虚拟主机的支持
Include conf/extra/httpd-vhosts.conf

//加载php模块解析php页面,添加两行,告诉httpd把.php文件交给模块去编译
LoadModule php5_module    modules/libphp5.so    找到这一句,在这句下面加上两句
//添加以下两行意思是以.php结尾的文件都认为是php程序文件,注意两句话的.php前面都是有一个空格的
AddHandler php5-script   .php       
AddType text/html  .php

//默认主页加上index.php,并放在index.html前,支持php的首页文件
<IfModule dir_module>
    DirectoryIndex index.php index.html
</IfModule>


注意:默认的网站目录是:/usr/local/apache2/htdocs/
2. 修改apache的子配置文件,优先支持中文
# vim /usr/local/apache2/conf/extra/httpd-languages.conf
DefaultLanguage zh-CN       打开注释,默认语言集改为中文
LanguagePriority zh-CN en ca cs da de el eo es et fr he hr it ja ko ltz nl nn no pl pt pt-BR ru sv tr zh-TW     语言集优先集,把zh-CN 写到前面

2.php支持连接本地数据库

说明:

本地数据库一般是通过socket文件连接,而本地数据库的socket文件如果不在默认路径,就必须告诉php从哪里读取socket文件。

# cp /usr/src/php-5.6.11/php.ini-production /usr/local/lib/php.ini
# vim /usr/local/lib/php.ini
.....
[MySQL]
mysql.default_port = 3306       改成对应的mysql的端口
mysql.default_socket = /tmp/mysql.sock  对应的socket文件地址

[MySQLi]
mysqli.default_port = 3306
mysqli.default_socket = /tmp/mysql.sock

3.测试是否支持php

# vim /usr/local/apache2/htdocs/index.php
<?php
        phpinfo();
?>

4.测试环境

浏览器里输入:http://ip //看到php的测试页表示ok

到此,lamp的编译安装和基本配置完毕,下面就可以根据需求部署web应用了.

六、根据需求部署web应用

  • 通过虚拟主机将web应用发布出去
0. 明确需要发布的网站
/www/admin      存放的是web界面管理mysql数据库的一个网站
phpMyAdmin-4.4.11-all-languages.zip

/www/myblog     存放的是一个博客或者论坛相关的网站
wordpress-4.7.3-zh_CN.tar.gz


1. 创建相应的数据目录
[root@lamp LAMP]# mkdir /www/{admin,myblog} -p

2.2个网站相关的程序文件解压拷贝到指定的目录里
[root@lamp LAMP]# unzip phpMyAdmin-4.4.11-all-languages.zip
[root@lamp LAMP]# cp -a phpMyAdmin-4.4.11-all-languages/* /www/admin/

[root@lamp LAMP]# tar xf wordpress-4.7.3-zh_CN.tar.gz
[root@lamp LAMP]# cp -a wordpress/* /www/myblog/

3. 修改网站数据目录的属主和属组
[root@lamp LAMP]# chown -R daemon. /www/

4. 配置虚拟主机:
注意:
在主配置文件打开以下内容:
# Virtual hosts
Include conf/extra/httpd-vhosts.conf

[root@lamp LAMP]# cd /usr/local/apache2/conf/extra/
[root@lamp extra]# vim httpd-vhosts.conf 
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "/www/admin"
    ServerName www.admin.cc
    ServerAlias www.admin.cc
    ErrorLog "logs/admin-error_log"
    CustomLog "logs/admin-access_log" common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot "/www/myblog"
    ServerName www.myblog.net
    ErrorLog "logs/myblog-error_log"
    CustomLog "logs/myblog-access_log" common
</VirtualHost>


5. 修改每个网站各自连接mysql数据库的配置文件
1) mysql的web管理网站phpMyAdmin相关文件
[root@lamp admin]#  cd /www/admin/
[root@lamp admin]# cp config.sample.inc.php config.inc.php
[root@lamp admin]# vim config.inc.php
....
/* Authentication type */
$cfg['Servers'][$i]['auth_type'] = 'cookie';
/* Server parameters */
$cfg['Servers'][$i]['host'] = '127.0.0.1';  ————>将localhost改为127.0.0.1
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['compress'] = false;
$cfg['Servers'][$i]['AllowNoPassword'] = false;
....

2) myblog网站wordpress
进入到mysql数据库创建一个myblog库
[root@lamp extra]# mysql -uroot -p123
mysql> create database myblog;
Query OK, 1 row affected (0.00 sec)

[root@lamp admin]#  cd /www/myblog/
[root@server myblog]# cp wp-config-sample.php wp-config.php
[root@server myblog]# vim wp-config.php
...
// ** MySQL 设置 - 具体信息来自您正在使用的主机 ** //
/** WordPress数据库的名称 */
define('DB_NAME', 'myblog');

/** MySQL数据库用户名 */
define('DB_USER', 'root');

/** MySQL数据库密码 */
define('DB_PASSWORD', '123');

/** MySQL主机 */
define('DB_HOST', '127.0.0.1');

/** 创建数据表时默认的文字编码 */
define('DB_CHARSET', 'utf8');

/** 数据库整理类型。如不确定请勿更改 */
define('DB_COLLATE', '');
...

问题:在重启apache的时候报如下错误:
[root@LAMP myblog]# service apache restart
AH00557: httpd: apr_sockaddr_info_get() failed for LAMP
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
httpd (no pid file) not running

原因:自己的主机名和IP地址没有绑定在hosts文件中



[root@lamp myblog]# mysql -p123
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.25 Source distribution

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database myblog;          //创建myblog库来存放wordpress的数据
Query OK, 1 row affected (0.01 sec)

mysql> 


[root@lamp ~]# vim /usr/local/apache2/conf/httpd.conf
215 <Directory />
216     AllowOverride none
217     #Require all denied
218     Require all granted
219 </Directory>

七、测试验证

Linux:
修改hosts文件
# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.1.1.4 www.admin.cc
10.1.1.4 www.myblog.net

http://www.admin.cc
http://www.myblog.net

猜你喜欢

转载自blog.csdn.net/SU_Devops/article/details/82534093