简介
所谓lamp,其实就是由Linux+Apache+Mysql/MariaDB+Php/Perl/Python的一组动态网站或者服务器的开源软件,除Linux外其它各部件本身都是各自独立的程序,但是因为经常被放在一起使用,拥有了越来越高的兼容度,共同组成了一个强大的Web应用程序平台。
LAMP指的是Linux(操作系统)、Apache(HTTP服务器)、MySQL(也指MariaDB,数据库软件)和PHP(有时也是指Perl或Python)的第一个字母,一般用来建立web应用平台。
web服务器的资源分为两种,静态资源和动态资源
静态资源就是指静态内容,客户端从服务器获得的资源的表现形式与原文件相同。可以简单的理解为就是直接存储于文件系统中的资源
动态资源则通常是程序文件,需要在服务器执行之后,将执行的结果返回给客户端
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上下文切换而导致耗时
httpd与php结合的方式
web工作流程
客户端通过http协议请求web服务器资源
web服务器收到请求后判断客户端请求的资源是静态资源或是动态资源
若是静态资源则直接从本地文件系统取之返回给客户端。
否则若为动态资源则通过FastCGI协议与php服务器联系,通过CGI程序的master进程调度worker进程来执行程序以获得客户端请求的动态资源,并将执行的结果通过FastCGI协议返回给httpd服务器,httpd服务器收到php的执行结果后将其封装为http响应报文响应给客户端。在执行程序获取动态资源时若需要获得数据库中的资源时,由Php服务器通过mysql协议与MySQL/MariaDB服务器交互,取之而后返回给httpd,httpd将从php服务器收到的执行结果封装成http响应报文响应给客户端。
httpd安装
安装依赖
[root@host ~]# yum -y install openssl-devel pcre-devel expat-devel libtool wget make
解压安装包
[root@host src]# wget https://mirrors.bfsu.edu.cn/apache/apr/apr-1.7.0.tar.gz
[root@host src]# wget https://mirrors.bfsu.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
[root@host src]# wget https://mirrors.bfsu.edu.cn/apache/httpd/httpd-2.4.46.tar.gz
[root@host src]# tar xf apr-1.7.0.tar.gz
[root@host src]# tar xf apr-util-1.6.1.tar.gz
[root@host src]# tar xf httpd-2.4.46.tar.gz
[root@host src]#cd apr-1.7.0/
[root@host apr-1.7.0]# vim configure
# $RM "$cfgfile" //将此行加上注释,或者删除此行
配置
[root@host apr-1.7.0]# ./configure --prefix=/usr/local/apr
安装
[root@host apr-1.7.0]# make && make install
[root@host apr-1.7.0]# cd /usr/src/apr-util-1.6.1/
配置
[root@host apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
安装
[root@host apr-util-1.6.1]# make && make install
编译安装apache
[root@host src]# cd httpd-2.4.46
[root@host httpd-2.4.46]# ./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@host httpd-2.4.46]# make && make install
[root@host httpd-2.4.46]# systemctl stop firewalld
[root@host httpd-2.4.46]# setenforce 0
[root@host httpd-2.4.46]# /usr/local/apache/bin/apachectl start
[root@host ~]# yum groups mark install 'Development Tools'
[root@host ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[root@host ~]# source /etc/profile.d/httpd.sh
[root@host ~]# ln -s /usr/local/apache/include/ /usr/include/httpd
[root@host ~]# echo 'MANPATH /usr/local/apache/man' >> /etc/man.config
[root@host ~]# sed -i '/#ServerName/s/#//g' /etc/httpd24/httpd.conf
[root@host ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
mysql配置
安装依赖
[root@host ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
[root@host ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
[root@host ~]# useradd -r -M -s /sbin/nologin mysql
[root@host ~]# tar xf mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz -C /usr/local
[root@host local]# mv mysql-5.7.33-linux-glibc2.12-x86_64/ mysql
[root@host local]# ls
apache apr apr-util bin etc games include lib lib64 libexec mysql sbin share src
[root@host local]# chown -R mysql.mysql /usr/local/mysql/
[root@host local]# ll
总用量 0
drwxr-xr-x. 13 root root 152 5月 12 18:39 apache
drwxr-xr-x. 6 root root 58 5月 12 18:34 apr
drwxr-xr-x. 5 root root 43 5月 12 18:36 apr-util
drwxr-xr-x. 2 root root 6 5月 19 2020 bin
drwxr-xr-x. 2 root root 6 5月 19 2020 etc
drwxr-xr-x. 2 root root 6 5月 19 2020 games
drwxr-xr-x. 2 root root 6 5月 19 2020 include
drwxr-xr-x. 2 root root 6 5月 19 2020 lib
drwxr-xr-x. 3 root root 17 3月 29 10:43 lib64
drwxr-xr-x. 2 root root 6 5月 19 2020 libexec
drwxr-xr-x. 9 mysql mysql 129 5月 12 19:08 mysql
drwxr-xr-x. 2 root root 6 5月 19 2020 sbin
drwxr-xr-x. 5 root root 49 3月 29 10:43 share
drwxr-xr-x. 2 root root 6 5月 19 2020 src
[root@host local]# vim /etc/profile.d/mysql.sh
[root@host local]# cat /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
[root@host local]# . /etc/profile.d/mysql.sh
[root@host local]# which mysql
/usr/local/mysql/bin/mysql
[root@host ~]# mkdir /opt/mysql-data
[root@host ~]# chown -R mysql.mysql /opt/mysql-data/
[root@host ~]# ll /opt/
总用量 0
drwxr-xr-x. 2 mysql mysql 6 5月 12 20:57 mysql-data
[root@host ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/mysql-data/
2021-05-12T12:58:03.278874Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-05-12T12:58:04.125188Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-05-12T12:58:04.270664Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-05-12T12:58:04.351540Z 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: b0a03970-b321-11eb-ae24-000c2941ee19.
2021-05-12T12:58:04.352956Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-05-12T12:58:05.496781Z 0 [Warning] CA certificate ca.pem is self signed.
2021-05-12T12:58:05.701902Z 1 [Note] A temporary password is generated for root@localhost: Hh_u;fud3yep
[root@host local]# vim /etc/my.cnf
[root@host ~]# cat /etc/my.cnf
[mysqld]
datadir = /opt/mysql-data
basedir = /usr/local/mysql
datadir = /opt/mysql-data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/mysql-data/mysql.pid
user = mysql
skip-name-resolve
[root@host mysql]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@host mysql]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@host mysql]# sed -ri 's#^(datadir=).*#\1/opt/mysql-data#g' /etc/init.d/mysqld
[root@host mysql]# head -47 /etc/init.d/mysqld |tail -2
basedir=/usr/local/mysql
datadir=/opt/mysql-data
[root@host mysql]# service mysqld start
Starting MySQL. SUCCESS!
[root@host ~]# chkconfig mysqld on
[root@host ~]# chkconfig --list
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
mysqld 0:关 1:关 2:开 3:开 4:开 5:开 6:关
[root@host ~]# ln -s /usr/local/mysql/include /usr/include/mysql
[root@host ~]# vim /etc/ld.so.conf.d/mysql.conf
[root@host ~]# cat /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@host ~]# ldconfig 重新读取
[root@host ~]# 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.33
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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('xialuo123!'); 设置新密码
Query OK, 0 rows affected, 1 warning (0.00 sec)
php配置
[root@host ~]# yum -y install php*
启动服务
[root@host ~]# service php-fpm start
[root@host ~]# vim /etc/php-fpm.d/www.conf
......
;listen = /run/php-fpm/www.sock
listen = 0.0.0.0:9000
......
[root@host ~]# systemctl restart php-fpm.service
[root@host ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:9000 0.0.0.0:*
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
apache配置
[root@host ~]# vim /etc/httpd24/httpd.conf
将下列两条取消注释
......
LoadModule proxy_module modules/mod_proxy.so
......
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
......
在apache的主配置文件中找到以下代码,在index.html前面添加index.php使apaache服务能够第一个找到php的初始页面
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
[root@host ~]# mkdir /usr/local/apache/htdocs/xialuo
[root@host ~]# cd /usr/local/apache/htdocs/xialuo
[root@host xialuo]# ls
[root@host xialuo]# vim index.php
[root@host xialuo]# cat index.php
<?php
phpinfo();
?>
更改属主
[root@host ~]# chown -R apache.apache /usr/local/apache/htdocs/
[root@host ~]# ll /usr/local/apache/
总用量 36
drwxr-xr-x. 2 root root 262 5月 12 18:39 bin
drwxr-xr-x. 2 root root 167 5月 12 18:39 build
drwxr-xr-x. 2 root root 78 5月 12 18:39 cgi-bin
drwxr-xr-x. 3 root root 4096 5月 12 18:39 error
drwxr-sr-x. 3 apache apache 38 5月 12 22:23 htdocs
drwxr-xr-x. 3 root root 8192 5月 12 18:39 icons
drwxr-xr-x. 2 root root 4096 5月 12 18:39 include
drwxr-xr-x. 2 root root 58 5月 12 18:41 logs
drwxr-xr-x. 4 root root 30 5月 12 18:39 man
drwxr-sr-x. 14 root root 8192 8月 1 2020 manual
drwxr-xr-x. 2 root root 4096 5月 12 18:39 modules
[root@host ~]# cd /etc/httpd24/
[root@host httpd24]# ls
extra httpd.conf magic mime.types original
[root@host httpd24]# vim httpd.conf
......
#Include /etc/httpd24/extra/httpd-default.conf
Include /etc/httpd24/extra/vhosts.conf (将httpd-default改为vhosts.conf)
# Configure mod_proxy_html to understand HTML4/XHTML1
<IfModule proxy_html_module>
Include /etc/httpd24/extra/proxy-html.conf
......
[root@host extra]# vim vhosts.conf
<VirtualHost *:80>
DocumentRoot "/usr/local/apache/htdocs/xialuo"
ServerName www.memory.com
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/xialuo/$1
<Directory "/usr/local/apache/htdocs/xialuo">
Options none
AllowOverride none
Require all granted
</Directory>
</VirtualHost>
[root@host httpd24]# vim httpd.conf
......
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz(在下面添加以下两条代码)
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
......
重启apache
[root@host extra]# apachectl restart
[root@host extra]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:9000 0.0.0.0:*
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
有mysqlnd的页面就代表可以使用MySQL