源码搭建LAMP
CentOS6源码安装LAMP----基于fastcgi方式(2台机器)
软件包下载源:
搜狐镜像源:http://mirrors.sohu.com/
apache下载网:http://mirror.bit.edu.cn/apache/
pcre官网:http://www.pcre.org/
apr官网:http://apr.apache.org/
apache官网:http://httpd.apache.org/
mysql官网:https://www.mysql.com/
php官网:http://php.net/
freetds:http://download.csdn.net/download/xhu_eternalcc/7457555
官网:http://www.freetds.org/
实验环境:
CentOS6.5-x86_64 单台机器
IP 192.168.9.176 php
IP 192.168.9.225 apache+mysql
版本:
apr-1.5.2
apr-util-1.5.4
httpd-2.4.20
php-5.6.22
mysql-5.6.32
说明,因为php要连接mysql,所以安装顺序为apache,mysql,php
注:httpd2.4版本需要较新的apr和apr-util
注:新机器要初始化:参考http://wupengfei.blog.51cto.com/7174803/1955545
注:(生产环境一般包放内网ftp下载比较快,一般前端用nginx反向代理至后端apache,通过fastcgi连接php,mysql独立并做集群)
1
|
一、编译安装apache
|
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
|
1、
#yum安装httpd依赖软件
[root@192-168-9-176 ~]
# yum install -y gcc gcc-c++ libtool libtool-ltdl-devel \
make
gd-devel freetype-devel libxml2-devel \
libjpeg-devel libpng-devel openssl-devel patch
[root@192-168-9-176 ~]
# yum install -y curl-devel bzip2 pcre-devel zip2-devel bzip2-devel
[root@192-168-9-176 ~]
# yum install -y mcrypt-devel zlib-devel wget
2、
#编译安装apr
[root@192-168-9-176 ~]
# cd /usr/local/src/
[root@192-168-9-176 src]
# wget https://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-1.5.2.tar.bz2
[root@192-168-9-176 src]
# tar xf apr-1.5.2.tar.bz2
[root@192-168-9-176 src]
# cd apr-1.5.2
[root@192-168-9-176 apr-1.5.2]
# ./configure --prefix=/usr/local/apr
[root@192-168-9-176 apr-1.5.2]
# make && make install
3、编译安装apr-util
[root@192-168-9-176 apr-1.5.2]
# cd /usr/local/src/
[root@192-168-9-176 src]
# wget https://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-util-1.5.4.tar.bz2
[root@192-168-9-176 src]
# tar xf apr-util-1.5.4.tar.bz2
[root@192-168-9-176 src]
# cd apr-util-1.5.4
[root@192-168-9-176 apr-util-1.5.4]
# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@192-168-9-176 apr-util-1.5.4]
# make && make install
4、编译安装httpd
[root@192-168-9-176 apr-util-1.5.4]
# useradd -s /sbin/nologin www
[root@192-168-9-176 apr-util-1.5.4]
# cd /usr/local/src/
[root@192-168-9-176 src]
# wget http://apache.fayea.com//httpd/httpd-2.4.20.tar.bz2
[root@192-168-9-176 src]
# tar xf httpd-2.4.20.tar.bz2
[root@192-168-9-176 src]
# cd httpd-2.4.20
[root@192-168-9-176 httpd-2.4.20]
# ./configure --prefix=/usr/local/apache \
--with-apr=
/usr/local/apr
\
--with-apr-util=
/usr/local/apr-util/
\
--
enable
-so \
--
enable
-ssl \
--
enable
-cgi \
--
enable
-rewrite \
--with-zlib \
--with-pcre \
--
enable
-modules=most \
--
enable
-mpms-shared=all \
--with-mpm=prefork
[root@192-168-9-176 httpd-2.4.20]
# make
[root@192-168-9-176 httpd-2.4.20]
# make install
[root@192-168-9-176 httpd-2.4.20]
# sed -i 's/User daemon/User www/g' /usr/local/apache/conf/httpd.conf
[root@192-168-9-176 httpd-2.4.20]
# sed -i 's/Group daemon/Group www/g' /usr/local/apache/conf/httpd.conf
[root@192-168-9-176 httpd-2.4.20]
# sed -i "/ServerName/s/#ServerName.*/ServerName localhost/g" /usr/local/apache/conf/httpd.conf
6、启动方法
#/usr/local/apache/bin/apachectl -k start|reload|restart|stop
7、测试验证
[root@192-168-9-176 httpd-2.4.20]
# curl -I http://192.168.9.176
HTTP
/1
.1 200 OK
Date: Fri, 11 Aug 2017 15:22:43 GMT
Server: Apache
/2
.4.20 (Unix)
Last-Modified: Mon, 11 Jun 2007 18:53:14 GMT
ETag:
"2d-432a5e4a73a80"
Accept-Ranges: bytes
Content-Length: 45
Content-Type: text
/html
|
二、编译安装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
|
1、
#下载安装包
[root@192-168-9-176 httpd-2.4.20]
# cd /usr/local/src/
[root@192-168-9-176 src]
# wget https://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.37-linux-glibc2.12-x86_64.tar.gz
2、
#建立运行mysql用户和组为系统账号
[root@192-168-9-176 src]
# groupadd -r mysql
[root@192-168-9-176 src]
# useradd -r -g mysql -s /sbin/nologin mysql
3、
#编译安装
[root@192-168-9-176 src]
# yum install -y libaio libaio-devel numactl numactl-devel cmake ncurses-devel libxml2-devel libtool-ltdl-devel gcc-c++ autoconf automake bison zlib-devel
[root@192-168-9-176 src]
# tar xf mysql-5.6.37-linux-glibc2.12-x86_64.tar.gz
[root@192-168-9-176 src]
# mv mysql-5.6.37-linux-glibc2.12-x86_64 /usr/local/mysql
#建议mysql使用独立设备独立目录,本实验只建立目录,独立设备请参考磁盘分区与挂载
[root@192-168-9-176 src]
# mkdir -p /data0/mysql
#更改属主属组
[root@192-168-9-176 src]
# chown -R mysql.mysql /usr/local/mysql
#初始化数据库
[root@192-168-9-176 src]
# cd /usr/local/mysql
[root@192-168-9-176 mysql]
# ./scripts/mysql_install_db --datadir=/data0/mysql --user=mysql
[root@192-168-9-176 mysql]
# cp ./support-files/my-default.cnf /data0/mysql/my.cnf
[root@192-168-9-176 mysql]
# echo "basedir = /usr/local/mysql
datadir =
/data0/mysql
port = 3306
server_id = 1000
socket =
/tmp/mysql
.sock" >>
/data0/mysql/my
.cnf
[root@192-168-9-176 mysql]
# chown -R mysql.mysql /data0/mysql/
4,启动:
[root@192-168-9-176 mysql]
# /usr/local/mysql/bin/mysqld_safe --defaults-file=/data0/mysql/my.cnf &
[root@192-168-9-176 mysql]
# echo "/usr/local/mysql/bin/mysqld_safe --defaults-file=/data0/mysql/my.cnf &" >> /etc/rc.d/rc.local
6、
#安全初始化
#安全验证初始化,设置root用户密码,删除匿名用户,禁止root用户远程连接,删除test库
[root@192-168-9-176 mysql]
# ./bin/mysql_secure_installation
#导出头文件
[root@192-168-9-176 mysql]
# ln -sv /usr/local/mysql/include/ /usr/include/mysql
#加载库文件
[root@192-168-9-176 mysql]
# echo "/usr/local/mysql/lib/mysql/" >>/etc/ld.so.conf
[root@192-168-9-176 mysql]
# ldconfig
[root@192-168-9-176 mysql]
# echo "PATH=$PATH:/usr/local/mysql/bin/" >> /etc/profile
|
三、编译安装PHP
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
1、
#编译安装libmcrypt扩展
[root@192-168-9-176 ~]
# yum install -y gcc gcc-c++ libtool libtool-ltdl-devel \
make
gd-devel freetype-devel libxml2-devel \
libjpeg-devel libpng-devel openssl-devel patch
[root@192-168-9-176 ~]
# yum install -y curl-devel bzip2 pcre-devel zip2-devel bzip2-devel
[root@192-168-9-176 ~]
# yum install -y mcrypt-devel zlib-devel wget mysql mysql-devel
[root@192-168-9-225 ~]
# cd /usr/local/src/
[root@192-168-9-225 src]
# wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz
[root@192-168-9-225 src]
# tar xf libmcrypt-2.5.7.tar.gz
[root@192-168-9-225 src]
# cd libmcrypt-2.5.7
[root@192-168-9-225 libmcrypt-2.5.7]
# ./configure
[root@192-168-9-225 libmcrypt-2.5.7]
# make && make install
2、
#编译安装freetds扩展
[root@192-168-9-225
local
]
# cd /usr/local/src/
[root@192-168-9-225 src]
# wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-patched.tar.gz
[root@192-168-9-225 src]
# tar xf freetds-patched.tar.gz
[root@192-168-9-225 src]
# cd freetds-1.00.53/
[root@192-168-9-225 freetds-1.00.53]
# ./configure --prefix=/usr/local/freetds --with-tdsver=7.4 --enable-msdblib
[root@192-168-9-225 freetds-1.00.53]
# make && make install
[root@192-168-9-225 freetds-1.00.53]
# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/freetds/lib/
[root@192-168-9-225 freetds-1.00.53]
# echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/freetds/lib/" >> /etc/profile
3、
#下载安装包
[root@192-168-9-225 src]
# cd /usr/local/src/
[root@192-168-9-225 src]
# wget http://cn2.php.net/distributions/php-5.6.22.tar.bz2
[root@192-168-9-225 src]
# tar xf php-5.6.22.tar.bz2
[root@192-168-9-225 src]
# cd php-5.6.22
[root@192-168-9-225 php-5.6.22]
# ./configure --prefix=/usr/local/php \
--with-mysql=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-openssl \
--
enable
-mbstring \
--with-freetype-
dir
\
--with-jpeg-
dir
\
--with-png-
dir
\
--with-zlib \
--with-libxml-
dir
=
/usr/
\
--
enable
-xml \
--
enable
-sockets \
--with-mcrypt \
--with-mssql=
/usr/local/freetds
\
--with-bz2 \
--
enable
-maintainer-zts \
--with-curl \
--
enable
-
ftp
\
--
enable
-bcmath \
--with-gd \
--with-freetype-
dir
\
--
enable
-gd-native-ttf \
--with-iconv-
dir
\
--
enable
-mbstring \
--
enable
-calendar \
--with-gettext \
--
enable
-dom \
--
enable
-fpm
#说明:如果php5.3以上的版本,为了链接数据库,可以指定mysqlnd,这样本机就可以不用安装mysql和mysql开发包。5.4已经是默认设置。
如: --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd
[root@192-168-9-225 php-5.6.22]
# make
[root@192-168-9-225 php-5.6.22]
# make install
4/
#为php提供配置文件
[root@192-168-9-225 php-5.6.22]
# cp php.ini-production /etc/php.ini
[root@192-168-9-225 php-5.6.22]
# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
[root@192-168-9-225 php-5.6.22]
# chmod +x /etc/rc.d/init.d/php-fpm
[root@192-168-9-225 php-5.6.22]
# chkconfig --add php-fpm
[root@192-168-9-225 php-5.6.22]
# chkconfig php-fpm on
[root@192-168-9-225 php-5.6.22]
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@192-168-9-225 php-5.6.22]
# vim /usr/local/php/etc/php-fpm.conf
#配置fpm的相关选项为你所需要的值,并启用pid文件(如下最后一行):
listen = 192.168.9.225:9000
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
pid =
/usr/local/php/var/run/php-fpm
.pid
[root@192-168-9-225 ~]
# mkdir -pv /data0/web_root/domain/www
[root@192-168-9-225 ~]
# echo "<?php
phpinfo();
?>" >
/data0/web_root/domain/www/index
.php
[root@192-168-9-225 php-5.6.22]
# service php-fpm start
5、编辑apache配置文件httpd.conf,apache支持php
#启用httpd的相关模块
[root@192-168-9-176 ~]
# vim /usr/local/apache/conf/httpd.conf
#在Apache httpd 2.4以后已经专门有一个模块针对FastCGI的实现,此模块为mod_proxy_fcgi.so,它其实是作为mod_proxy.so模块的扩充,因此,这两个模块都要加载
LoadModule proxy_module modules
/mod_proxy
.so
LoadModule proxy_fcgi_module modules
/mod_proxy_fcgi
.so
#配置虚拟主机支持使用fcgi
#注释根,打开虚拟主机
#DocumentRoot "/usr/local/apache/htdocs"
Include conf
/extra/vhosts/
*.conf
#编辑apache配置文件httpd.conf,让apache能识别php格式的页面,并支持php格式的主页
#添加如下二行
AddType application
/x-httpd-php
.php
AddType application
/x-httpd-php-source
.phps
#定位至DirectoryIndex index.html
修改为:
DirectoryIndex index.php index.html
[root@192-168-9-176 ~]
# mkdir -p /usr/local/apache/conf/extra/vhosts/
[root@192-168-9-176 ~]
# vim /usr/local/apache/conf/extra/vhosts/domain.conf
#ProxyRequests Off:关闭正向代理
#ProxyPassMatch:把以.php结尾的文件请求发送到php-fpm进程,php-fpm至少需要知道运行的目录和URI,所以这里直接在fcgi://192.168.0.17:9000后指明了这两个参数,其它的参数的传递已经被mod_proxy_fcgi.so进行了封装,不需要手动指定。
<VirtualHost *:80>
ServerAdmin webadmin@a.com
DocumentRoot
"/data0/web_root/domain/www"
DirectoryIndex index.php index.html
ServerName www.domain.com
ServerAlias domain.com
ErrorLog
"logs/www.domain.com-error_log"
CustomLog
"logs/www.domain.com-access_log"
common
<Directory
"/data0/web_root/domain/www"
>
Options None
Require all granted
<
/Directory
>
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi:
//192
.168.9.225:9000
/data0/web_root/domain/www/
$1
<
/VirtualHost
>
#重启httpd
[root@192-168-9-176 ~]
# /usr/local/apache/bin/apachectl -k restart
6、浏览器访问:
http:
//192
.168.9.176
7、测试验证php连接数据库
[root@192-168-9-225 php-5.6.22]
# vim index.php
<?php
$link = mysql_connect(
'192.168.9.176'
,
'root'
,
'123456'
);
if
($link)
echo
"Success..."
;
else
echo
"Failure..."
;
mysql_close()
?>
#浏览器访问
http:
//192
.168.9.176
6、
#ab压力测试
[root@192-168-9-276 php-5.6.22]
# ab -c 100 -n 10000
|