lamp是一个常用的web架构
所谓lamp,其实就是由Linux+Apache+Mysql/MariaDB+Php/Perl/Python的一组动态网站或者服务器的开源软件,除Linux外其它各部件本身都是各自独立的程序,但是因为经常被放在一起使用,拥有了越来越高的兼容度,共同组成了一个强大的Web应用程序平台。
LAMP指的是Linux(操作系统)、Apache(HTTP服务器)、MySQL(也指MariaDB,数据库软件)和PHP(有时也是指Perl或Python)的第一个字母,一般用来建立web应用平台。
web服务器的资源分为两种,静态资源和动态资源
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上下文切换而导致耗时
httpd与php结合的方式有以下三种:
较于CGI方式,FastCGI更为常用,很少有人使用CGI方式来加载动态资源
环境:
lamp平台软件安装顺序:
httpd ——> mysql ——> php
//安装开发工具包
[root@xaii ~] yum grouplist
已加载插件:fastestmirror
没有安装组信息文件
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
* base: mirrors.163.com
* extras: mirror.bit.edu.cn
* updates: mirror.bit.edu.cn
可用的环境分组:
最小安装
基础设施服务器
计算节点
文件及打印服务器
基本网页服务器
虚拟化主机
带 GUI 的服务器
GNOME 桌面
KDE Plasma Workspaces
开发及生成工作站
可用组:
传统 UNIX 兼容性
兼容性程序库
图形管理工具
安全性工具
开发工具
控制台互联网工具
智能卡支持
科学记数法支持
系统管理
系统管理工具
完成
[root@xaii ~]# yum groups mark install '开发工具'
已加载插件:product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
没有安装组信息文件
Maybe run: yum groups mark convert (see man yum)
Marked install: 开发工具
//创建apache服务的用户和组
[root@xaii ~]# groupadd -r apache
[root@xaii ~]# useradd -r -M -s /sbin/nologin -g apache apache
-r:创建系统用户;-M:不创建用户的主目录;-s:不允许交互
//安装依赖包
[root@xaii ~]# yum -y install openssl-devel pcre-devel expat-devel libtool
正在解决依赖关系
--> 正在检查事务
---> 软件包 expat-devel.x86_64.0.2.1.0-10.el7_3 将被 安装
---> 软件包 libtool.x86_64.0.2.4.2-22.el7_3 将被 安装
安装过程略......
完毕!
//下载和安装apr以及apr-util
[root@xaii src]# ls
apr-1.6.5.tar.gz apr-util-1.6.1.tar.gz debug kernels
[root@xaii src]# tar xf apr-1.6.5.tar.gz
[root@xaii src]# tar xf apr-util-1.6.1.tar.gz
[root@xaii src]# cd apr-1.6.5
[root@xaii apr-1.6.5]# vim configure
cfgfile="${ofile}T"
trap "$RM \"$cfgfile\"; exit 1" 1 2 15
# $RM "$cfgfile" //将此行加上注释,或者删除此行
[root@xaii apr-1.6.5]# ./configure --prefix=/usr/local/apr
配置过程略......
[root@xaii apr-1.6.5]# make -j 4
配置过程略......
[root@xaii apr-1.6.5]# make install
配置过程略......
[root@xaii apr-1.6.5]# cd /usr/src/apr-util-1.6.1
[root@xaii apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
配置过程略......
[root@xaii apr-util-1.6.1]# make -j 4
配置过程略......
[root@xaii apr-util-1.6.1]# make install
配置过程略......
//编译安装httpd
[root@xaii ~]# ls
anaconda-ks.cfg httpd-2.4.37.tar.gz
[root@xaii ~]# tar xf httpd-2.4.37.tar.gz
[root@xaii ~]# cd httpd-2.4.37
[root@xaii httpd-2.4.37]# ./configure --prefix=/usr/local/apache \
--sysconfdir=/etc/httpd24 \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util/ \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork
[root@xaii httpd-2.4.37]# make -j 4
配置过程略......
[root@xaii httpd-2.4.37]# make install
配置过程略......
//安装后的配置(添加环境变量,创建软连接,man文档)
[root@xaii ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[root@xaii ~]# source /etc/profile.d/httpd.sh
[root@xaii ~]# ln -s /usr/local/apache/include/ /usr/include/httpd
[root@xaii ~]# echo 'MANPATH /usr/local/apache/man' >> /etc/man.config
//取消ServerName前面的注释
[root@xaii ~]# sed -i '/#ServerName/s/#//g' /etc/httpd24/httpd.conf
//启动apache服务
[root@xaii ~]# apachectl start
[root@xaii ~]# ss -antl |grep 80
LISTEN 0 128 :::80 :::*
//安装依赖包
[root@xaii ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
安装过程省略......
作为依赖被安装:
libarchive.x86_64 0:3.1.2-10.el7_2
完毕!
//创建用户和组
[root@xaii ~]# groupadd -r -g 306 mysql
[root@xaii ~]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql
//将mysql压缩包解压至/usr/local/,并创建软连接
[root@xaii src]# tar xf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@xaii src]# cd /usr/local/
[root@xaii local]# ls
apache apr-util etc include lib64 mysql-5.7.23-linux-glibc2.12-x86_64 share
apr bin games lib libexec sbin src
[root@xaii local]# ln -sv mysql-5.7.23-linux-glibc2.12-x86_64/ mysql
"mysql" -> "mysql-5.7.23-linux-glibc2.12-x86_64/"
[root@xaii local]# ll
总用量 0
drwxr-xr-x. 13 root root 152 2月 20 15:07 apache
drwxr-xr-x. 6 root root 58 2月 20 15:00 apr
drwxr-xr-x. 5 root root 43 2月 20 15:04 apr-util
drwxr-xr-x. 2 root root 6 3月 10 2016 bin
drwxr-xr-x. 2 root root 6 3月 10 2016 etc
drwxr-xr-x. 2 root root 6 3月 10 2016 games
drwxr-xr-x. 2 root root 6 3月 10 2016 include
drwxr-xr-x. 2 root root 6 3月 10 2016 lib
drwxr-xr-x. 2 root root 6 3月 10 2016 lib64
drwxr-xr-x. 2 root root 6 3月 10 2016 libexec
lrwxrwxrwx. 1 root root 36 2月 20 15:26 mysql -> mysql-5.7.23-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root root 129 2月 20 15:24 mysql-5.7.23-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 3月 10 2016 sbin
drwxr-xr-x. 5 root root 49 1月 17 22:15 share
drwxr-xr-x. 2 root root 6 3月 10 2016 src
//修改目录/usr/local/mysql的属主属组
[root@xaii ~]# chown -R mysql.mysql /usr/local/mysql
//添加环境变量
[root@xaii ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@xaii ~]# . /etc/profile.d/mysql.sh
[root@xaii ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/apache/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
//建立数据存放目录
[root@xaii ~]# mkdir /opt/data
[root@xaii ~]# chown -R mysql.mysql /opt/data/
[root@xaii ~]# ll /opt/
总用量 0
drwxr-xr-x. 2 mysql mysql 6 2月 20 15:30 data
//初始化数据库
[root@xaii ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2019-02-20T07:31:58.480376Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-02-20T07:32:01.076333Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-02-20T07:32:01.329100Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-02-20T07:32:01.741364Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 9cf9ccfb-34e1-11e9-839d-000c29758baa.
2019-02-20T07:32:02.103007Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-02-20T07:32:02.103909Z 1 [Note] A temporary password is generated for root@localhost: gnefEHu*g3to
[root@xaii ~]# echo 'gnefEHu*g3to' > passwd
//请注意,这个命令的最后会生成一个临时密码,此处密码是gnefEHu*g3to
//再次注意,这个密码是随机的,一定要记住这个密码,因为一会登录时会用到,所以最好将它写入到一个文件中,防止遗忘
//配置mysql
[root@xaii ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
"/usr/local/include/mysql" -> "/usr/local/mysql/include/"
[root@xaii ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@xaii ~]# ldconfig
//生成配置文件
[root@xaii ~]# 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@xaii ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@xaii ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@xaii ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
//启动mysql
[root@xaii ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/xaii.err'.
SUCCESS!
[root@xaii ~]# ss -atnl|grep 3306
LISTEN 0 80 :::3306 :::*
//提示没有mysql命令
[root@xaii mysql]# mysql
-bash: mysql: 未找到命令
原因:这是由于系统默认会查找/usr/bin下的命令,如果这个命令不在这个目录下,当然会找不到命令,我们需要做的就是映射一个链接到/usr/bin目录下,相当于建立一个链接文件。
首先得知道mysql命令或mysqladmin命令的完整路径,比如mysql的路径是:/usr/local/mysql/bin/mysql,我们则可以这样执行命令:
# ln -s /usr/local/mysql/bin/mysql /usr/bin
//修改密码
//使用刚才的临时密码登录
[root@xaii ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23
Copyright (c) 2000, 2018, 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> set password = password('lizhao123!');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye
//配置yum源
[root@xaii ~]# cd /etc/yum.repos.d/
[root@xaii yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@xaii ~]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@xaii ~]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@xaii ~]# yum -y install epel-release
[root@xaii ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
安装过程省略......
//安装依赖包
[root@xaii ~]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel libpcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php72w-mysqlnd
安装过程省略......
//编译安装php
[root@xaii ~]# cd /usr/src/
[root@xaii src]# tar xf php-7.2.8.tar.xz
[root@xaii src]# cd php-7.2.8
[root@xaii php-7.2.8]# ./configure --prefix=/usr/local/php7 \
--with-config-file-path=/etc \
--enable-fpm \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-openssl \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--enable-exif \
--enable-ftp \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir \
--with-freetype-dir \
--with-gettext \
--enable-json \
--enable-mbstring \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-readline \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-pcntl \
--enable-posix
//错误一:
configure: error: Please reinstall the libzip distribution
//解决方案:
#先删除旧版本
yum remove -y libzip
#下载编译安装
wget https://nih.at/libzip/libzip-1.2.0.tar.gz
tar -zxvf libzip-1.2.0.tar.gz
cd libzip-1.2.0
./configure
make && make install
//错误二:
重新编译php:
configure: error: off_t undefined; check your library configuration
//解决方案:
#添加搜索路径到配置文件
vim /etc/ld.so.conf.d/ld.so.conf
添加以下内容:
/usr/local/lib64
/usr/local/lib
/usr/lib
/usr/lib64
#然后 更新配置
ldconfig -v
[root@xaii php-7.2.8]# make -j $(cat /proc/cpuinfo |grep processor|wc -l)
编译过程省略......
[root@xaii php-7.2.8]# make install
编译过程省略......
再次报错:
In file included from /root/devdir/php-7.3.4/ext/zip/php_zip.h:31:0,
from /root/devdir/php-7.3.4/ext/zip/php_zip.c:36:
/usr/local/include/zip.h:59:21: 致命错误:zipconf.h:没有那个文件或目录
#include
^
编译中断。
make: *** [ext/zip/php_zip.lo] 错误 1
//解决方案:
cp /usr/local/lib/libzip/include/zipconf.h /usr/local/include/zipconf.h
[root@localhost php-7.3.4]# cp /usr/local/lib/libzip/include/zipconf.h /usr/local/include/
//安装后配置
[root@xaii php-7.2.8]#
[root@xaii php-7.2.8]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@xaii php-7.2.8]# source /etc/profile.d/php7.sh
[root@xaii php-7.2.8]# which php
/usr/local/php7/bin/php
[root@xaii php-7.2.8]# php -v
PHP 7.2.8 (cli) (built: Feb 20 2019 15:58:54) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
//配置php-fpm
[root@xaii php-7.2.8]# cp php.ini-production /etc/php.ini
[root@xaii php-7.2.8]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@xaii php-7.2.8]# chmod +x /etc/rc.d/init.d/php-fpm
[root@xaii php-7.2.8]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[root@xaii php-7.2.8]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
//编辑php-fpm的配置文件(/usr/local/php7/etc/php-fpm.conf)
//配置fpm的相关选项为你所需要的值
[root@xaii php-7.2.8]# vim /usr/local/php7/etc/php-fpm.conf
[root@xaii php-7.2.8]# tail /usr/local/php7/etc/php-fpm.conf
; Relative path can also be used. They will be prefixed by:
; - the global prefix if it's been set (-p argument)
; - /usr/local/php7 otherwise
include=/usr/local/php7/etc/php-fpm.d/*.conf
pm.max_children = 50 //最多同时提供50个进程提供50个并发服务
pm.start_servers = 5 //启动时启动5个进程
pm.min_spare_servers = 2 //最小空闲进程数
pm.max_spare_servers = 8 //最大空闲进程数
//启动php-fpm
[root@xaii ~]# service php-fpm start
Starting php-fpm done
//默认情况下,fpm监听在127.0.0.1的9000端口,也可以使用如下命令验证其是否已经监听在相应的套接字
[root@xaii ~]# 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 127.0.0.1:9000 *:*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306 :::*
[root@xaii ~]# ps -ef|grep -v grep|grep php
root 74751 1 0 16:22 ? 00:00:00 php-fpm: master process (/usr/local/php7/etc/php-fpm.conf)
nobody 74752 74751 0 16:22 ? 00:00:00 php-fpm: pool www
nobody 74753 74751 0 16:22 ? 00:00:00 php-fpm: pool www
nobody 74754 74751 0 16:22 ? 00:00:00 php-fpm: pool www
nobody 74755 74751 0 16:22 ? 00:00:00 php-fpm: pool www
nobody 74756 74751 0 16:22 ? 00:00:00 php-fpm: pool www
在apache httpd 2.4以后已经专门有一个模块针对FastCGI的实现,此模块为mod_proxy_fcgi.so,它其实是作为mod_proxy.so模块的扩展,因此,这两个模块都要加载,编辑httpd.conf文件,取消以下两行内容的注释:
//启用httpd的相关模块
[root@xaii ~]# sed -i '/proxy_module/s/#//g' /etc/httpd24/httpd.conf
[root@xaii ~]# sed -i '/proxy_fcgi_module/s/#//g' /etc/httpd24/httpd.conf
在需要使用fcgi的虚拟主机中添加类似如下两行:
例如:
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/lizhao.com/$1
以上设置表示把以.php结尾的文件请求发送到php-fpm进程,php-fpm至少需要知道运行的目录和URI,所以这里直接在fcgi://127.0.0.1:9000后指明了这两个参数,其它参数的传递已经被mod_proxy_fcgi.so进行了封装,不需要手动指定。
注意:
这里写的 /usr/local/apache/htdocs/ 是yum源安装方式生成的网页存放目录,这里必须改成你编译安装指定的网页存放路径,禁止直接复制我这里的路径
这里的 lizhao.com是域名,你必须改成你所使用的域名,禁止直接复制此处的域名
这里的$1表示匹配所有以.php结尾的http请求
//创建虚拟主机目录并生成php测试页面
[root@xaii ~]# mkdir /usr/local/apache/htdocs/lizhao.com
[root@xaii ~]# cat > /usr/local/apache/htdocs/lizhao.com/index.php <<EOF
> <?php
> phpinfo();
> ?>
> EOF
[root@xaii ~]# chown -R apache.apache /usr/local/ap
apache/ apr/ apr-util/
[root@xaii ~]# chown -R apache.apache /usr/local/apache/htdocs/
[root@xaii ~]# ll /usr/local/apache/htdocs/ -d
drwxr-xr-x. 3 apache apache 42 2月 20 16:37 /usr/local/apache/htdocs/
//编辑apache的配置文件
[root@xaii ~]# vim /etc/httpd24/httpd.conf
//在配置文件的最后加入以下内容
[root@xaii ~]# tail -12 /etc/httpd24/httpd.conf
<VirtualHost *:80>
DocumentRoot "/usr/local/apache/htdocs/lizhao.com"
ServerName www.lizhao.com
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/lizhao.com/$1
<Directory "/usr/local/apache/htdocs/lizhao.com">
Options none
AllowOverride none
Require all granted
</Directory>
</VirtualHost>
//搜索AddType,添加以下内容
[root@xaii ~]# vim /etc/httpd24/httpd.conf
# If the AddEncoding directives above are commented-out, then you
# probably should define those extensions to indicate media types:
#
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php //添加此行
AddType application/x-httpd-php-source .phps //添加此行
[root@xaii ~]# sed -i '/ DirectoryIndex/s/index.html/index.php index.html/g' /etc/httpd24/httpd.conf
//重启apache服务
[root@xaii ~]# apachectl restart
[root@xaii ~]# 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 127.0.0.1:9000 *:*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306 :::*
1. 修改/etc/hosts文件,添加域名与IP的映射
C:\Windows\System32\drivers\etc,用记事本打开hosts文件,添加以下内容:
192.168.157.59(本机IP地址) www.lizhao.com
2. 在浏览器上使用域名访问,若看到以下界面则表示lamp架构搭建成功,否则请检查你的操作
yum install php mysql-server httpd php-mysql php-gd -y