Lamp
1.lamp简介
所谓lamp,其实就是由Linux+Apache+Mysql/MariaDB+Php/Perl/Python的一组动态网站或者服务器的开源软件,除Linux外其它各部件本身都是各自独立的程序,但是因为经常被放在一起使用,拥有了越来越高的兼容度,共同组成了一个强大的Web应用程序平台。
知名网站架构
|| System | Server | Storage |Script| | -------- | -------- | -------- | | Yahoo|FreeBSD + Linux|Apache|MySQL|PHP| |Facebook|FreeBSD|Apache|MySQL + Memcached|PHP| |Wikimedia|Linux|Apache + Lighttpd|MySQL + Memcached|PHP|
2. web服务器工作流程
建立连接——接受一个客户端连接,或者如果不希望与这个客户端建立连接,就将其关闭。 接收请求——从网络中读取一条 HTTP 请求报文。 处理请求——对请求报文进行解释,并采取行动。 访问资源——访问报文中指定的资源。 构建响应——创建带有正确首部的 HTTP 响应报文。 发送响应——将响应回送给客户端。 记录事务处理过程——将与已完成事务有关的内容记录在一个日志文件中。
web服务器的资源分为两种,静态资源和动态资源
静态资源就是指静态内容,客户端从服务器获得的资源的表现形式与原文件相同。可以简单的理解为就是直接存储于文件系统中的资源 动态资源则通常是程序文件,需要在服务器执行之后,将执行的结果返回给客户端
2.1 cgi与fastcgi
上图阶段①中提到了FastCGI,下面我们来了解下CGI与FastCGI。
CGI(Common Gateway Interface,通用网关接口),CGI是外部应用程序(CGI程序)与WEB服务器之间的接口标准,是在CGI程序和Web服务器之间传递信息的过程。CGI规范允许Web服务器执行外部程序,并将它们的输出发送给Web浏览器,CGI将web的一组简单的静态超媒体文档变成一个完整的新的交互式媒体。
FastCGI(Fast Common Gateway Interface)是CGI的改良版,CGI是通过启用一个解释器进程来处理每个请求,耗时且耗资源,而FastCGI则是通过master-worker形式来处理每个请求,即启动一个master主进程,然后根据配置启动几个worker进程,当请求进来时,master会从worker进程中选择一个去处理请求,这样就避免了重复的生成和杀死进程带来的频繁cpu上下文切换而导致耗时
2.2 httpd与php结合的方式
httpd与php结合的方式有以下三种:
modules:php将以httpd的扩展模块形式存在,需要加载动态资源时,httpd可以直接通过php模块来加工资源并返回给客户端 httpd prefork:libphp5.so(多进程模型的php) httpd event or worker:libphp5-zts.so(线程模型的php) CGI:httpd需要加载动态资源时,通过CGI与php解释器联系,获得php执行的结果,此时httpd负责与php连接的建立和断开等 FastCGI:利用php-fpm机制,启动为服务进程,php自行运行为一个服务,https通过socket与php通信 较于CGI方式,FastCGI更为常用,很少有人使用CGI方式来加载动态资源
3. lamp平台构建
适用环境 系统支持:CentOS-5 (32bit/64bit)、CentOS-6 (32bit/64bit)等linux系统 内存要求:≥256M 需要安装有 1、Apache 2、MySQL 3、PHP 4、phpmyadmin 3.5.1 5、ZendOptimizer 3.3.9(可选,只适合PHP 5.2.17) 6、xcache 1.3.2(可选) 7、pure-ftpd-1.0.36(可选)
lamp平台软件安装次序:httpd --> mysql --> php 注意:php要求httpd使用prefork MPM
3.1 安装httpd
//安装开发工具包
[root@system1 yum.repos.d]# yum groups mark install 'Development Tools'Loaded plugins: langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Marked install: Development Tools
//创建apache服务的用户和组
[root@system1 yum.repos.d]# groupadd -r apache
[root@system1 yum.repos.d]# useradd -r -M -s /sbin/nologin -g apache apache
[root@system1 yum.repos.d]# yum -y install openssl-devel pcre-devel expat-devel libtool
Loaded plugins: langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
//略
[root@system1 Desktop]# ls apache apr-1.6.5.tar.bz2 apr-util-1.6.1.tar.bz2 copy httpd-2.4.37.tar.bz2 [root@system1 Desktop]# tar xf apr-1.6.5.tar.bz2 [root@system1 Desktop]# tar xf apr-util-1.6.1.tar.bz2 [root@system1 Desktop]# cd apr-1.6.5/ [root@system1 apr-1.6.5]# ls apr-config.in build-outputs.mk helpers misc strings apr.dep CHANGES include mmap support apr.dsp CMakeLists.txt libapr.dep network_io tables apr.dsw config.layout libapr.dsp NOTICE test apr.mak configure libapr.mak NWGNUmakefile threadproc apr.pc.in configure.in libapr.rc passwd time apr.spec docs LICENSE poll tools atomic dso locks random user build emacs-mode Makefile.in README buildconf encoding Makefile.win README.cmake build.conf file_io memory shmem [root@system1 apr-1.6.5]# vim configure
trap "$RM \"$cfgfile\"; exit 1" 1 2 15
// $RM "$cfgfile"``` //将此句注释或删去
[root@system1 apr-1.6.5]# ./configure --prefix=/usr/local/apr //配置后略
[root@system1 apr-1.6.5]# make && make install //编译后略
[root@system1 apr-1.6.5]# cd ..
[root@system1 Desktop]# cd apr-util-1.6.1/
[root@system1 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@system1 apr-util-1.6.1]# make && make install
### 3.2安装Mysql
//安装依赖包
[root@system1 Desktop]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
Installing:
cmake x86_64 2.8.12.2-2.el7 myrepo 7.0 M
mariadb-devel x86_64 1:5.5.56-2.el7 myrepo 752 k
ncurses-devel x86_64 5.9-13.20130511.el7 myrepo 713 k
Installing for dependencies:
libarchive x86_64 3.1.2-10.el7_2 myrepo 319 k
Transaction Summary
================================================================================================
Install 3 Packages (+1 Dependent package)
Total download size: 8.8 M
.....
Installed:
cmake.x86_64 0:2.8.12.2-2.el7 mariadb-devel.x86_64 1:5.5.56-2.el7
ncurses-devel.x86_64 0:5.9-13.20130511.el7
Dependency Installed:
libarchive.x86_64 0:3.1.2-10.el7_2
Complete!
//创建用户和组
[root@system1 Desktop]# groupadd -r -g 306 mysql
[root@system1 Desktop]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql
//下载二进制格式的mysql软件包
[root@system1 ~]# cd /usr/src/
[root@system1 src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
--2018-08-13 23:56:27-- https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
Resolving downloads.mysql.com (downloads.mysql.com)... 137.254.60.14
Connecting to downloads.mysql.com (downloads.mysql.com)|137.254.60.14|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz [following]
......
Saving to: ‘mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz’
100%[=====================================>] 643,790,848 2.46MB/s in 4m 20s
2018-08-14 00:00:50 (2.36 MB/s) - ‘mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz’saved [643790848/643790848]
//解压软件至/usr/local/
[root@system1 src]# ls
debug kernels mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
[root@system1 src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@system1 ~]# ls /usr/local/
bin games lib libexec sbin src
etc include lib64 mysql-5.7.22-linux-glibc2.12-x86_64 share
[root@system1 ~]# cd /usr/local/
[root@system1 local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
‘mysql’ -> ‘mysql-5.7.22-linux-glibc2.12-x86_64/’
[root@system1 local]# ll
total 0
drwxr-xr-x. 2 root root 6 Mar 10 2016 bin
drwxr-xr-x. 2 root root 6 Mar 10 2016 etc
drwxr-xr-x. 2 root root 6 Mar 10 2016 games
drwxr-xr-x. 2 root root 6 Mar 10 2016 include
drwxr-xr-x. 2 root root 6 Mar 10 2016 lib
drwxr-xr-x. 2 root root 6 Mar 10 2016 lib64
drwxr-xr-x. 2 root root 6 Mar 10 2016 libexec
lrwxrwxrwx 1 root root 36 Aug 14 16:00 mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/
drwxr-xr-x 9 root root 129 Aug 14 00:16 mysql-5.7.22-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 Mar 10 2016 sbin
drwxr-xr-x. 5 root root 49 Jun 13 19:03 share
drwxr-xr-x. 2 root root 6 Mar 10 2016 src
//修改目录/usr/local/mysql的属主属组
[root@system1 ~]# chown -R mysql.mysql /usr/local/mysql
[root@system1 ~]# ll /usr/local/mysql -d
lrwxrwxrwx 1 mysql mysql 36 Aug 14 16:00 /usr/local/mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/
//添加环境变量
[root@system1 ~]# ls /usr/local/mysql
bin COPYING docs include lib man README share support-files
[root@system1 ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@system1 ~]# . /etc/profile.d/mysql.sh
[root@system1 ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
//建立数据存放目录
[root@system1 mysql]# mkdir /opt/data
[root@system1 mysql]# chown -R mysql.mysql /opt/data/
[root@system1 mysql]# ll /opt/
total 0
drwxr-xr-x 2 mysql mysql 6 Aug 14 16:54 data
//初始化数据库
[root@system1 ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2018-08-15T07:57:46.168380Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-08-15T07:57:50.542516Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-08-15T07:57:50.927286Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-08-15T07:57:51.071260Z 0 [Warning] No existing UUID has been found, so we assume that this is the first
//这个命令的最后会生成一个临时密码
//配置mysql
[root@system1 ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
‘/usr/local/include/mysql’ -> ‘/usr/local/mysql/include/’
[root@system1 ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@system1 ~]# ldconfig -v
ldconfig: Can't stat /libx32: No such file or directory
ldconfig: Path `/usr/lib' given more than once
ldconfig: Path `/usr/lib64' given more than once
ldconfig: Can't stat /usr/libx32: No such file or directory
/usr/lib64/mysql:
libmysqlclient.so.18 -> libmysqlclient_r.so
/usr/local/mysql/lib:
libmysqlclient.so.20 -> libmysqlclient.so.20.3.9
......
/lib/sse2: (hwcap: 0x0000000004000000)
/lib64/sse2: (hwcap: 0x0000000004000000)
/lib64/tls: (hwcap: 0x8000000000000000)
[root@system1 ~]# ldconfig -p |grep mysql
libmysqlclient.so.20 (libc6,x86-64) => /usr/local/mysql/lib/libmysqlclient.so.20
libmysqlclient.so.18 (libc6,x86-64) => /usr/lib64/mysql/libmysqlclient.so.18
libmysqlclient.so (libc6,x86-64) => /usr/lib64/mysql/libmysqlclient.so
libmysqlclient.so (libc6,x86-64) => /usr/local/mysql/lib/libmysqlclient.so
//生成配置文件
[root@system1 ~]# cat > /etc/my.cnf <<EOF
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
EOF
//配置服务启动脚本
[root@system1 ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@system1 ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@system1 ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
//启动mysql
[root@system1 ~]# service mysqld start
Starting MySQL.. SUCCESS!
[root@system1 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306 :::*
//修改密码
### 3.3安装php
### 3.4配置Apache