lamp介绍和部署流程
lamp简介
-
lamp其实就是由Linux+Apache+Mysql/MariaDB+Php/Perl/Python的一组动态网站或者服务器的开源软件,除Linux外其它各部件本身都是各自独立的程序,但是因为经常被放在一起使用,拥有了越来越高的兼容度,共同组成了一个强大的Web应用程序平台。
-
LAMP指的是Linux(操作系统)、Apache(HTTP服务器)、MySQL(也指MariaDB,数据库软件)和PHP(有时也是指Perl或Python)的第一个字母,一般用来建立web应用平台。
web服务器工作流程
web服务器
web服务器的资源分为两种,静态资源和动态资源
- 静态资源就是指静态内容,客户端从服务器获得的资源的表现形式与原文件相同。可以简单的理解为就是直接存储于文件系统中的资源
- 动态资源则通常是程序文件,需要在服务器执行之后,将执行的结果返回给客户端
web工作流程
- 客户端通过http协议请求web服务器资源
- web服务器收到请求后判断客户端请求的资源是静态资源或是动态资源
- 若是静态资源则直接从本地文件系统取之返回给客户端。
- 否则若为动态资源则通过FastCGI协议与php服务器联系,通过CGI程序的master进程调度worker进程来执行程序以获得客户端请求的动态资源,并将执行的结果通过FastCGI协议返回给httpd服务器,httpd服务器收到php的执行结果后将其封装为http响应报文响应给客户端。在执行程序获取动态资源时若需要获得数据库中的资源时,由Php服务器通过mysql协议与MySQL/MariaDB服务器交互,取之而后返回给httpd,httpd将从php服务器收到的执行结果封装成http响应报文响应给客户端。
lamp平台构建
环境说明:
系统平台 | IP | 需要安装的服务 |
---|---|---|
redhat8 | 192.168.110.133 | httpd-2.4 mysql-5.7 php php-mysql |
lamp平台软件安装次序:
httpd --> mysql --> php
配置yum网络源(这里用的是阿里云的网络源)
centos源: https://developer.aliyun.com/mirror/centos?spm=a2c6h.13651102.0.0.3e221b116dseUd
epel源:https://developer.aliyun.com/mirror/epel?spm=a2c6h.13651102.0.0.3e221b116ldhDa
[root@192 ~]# cd /etc/yum.repos.d/
[root@192 yum.repos.d]# ls
redhat.repo
[root@192 yum.repos.d]# rm -f redhat.repo
//配置centos源
[root@192 yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2595 100 2595 0 0 4451 0 --:--:-- --:--:-- --:--:-- 4443
[root@192 yum.repos.d]# ls
CentOS-Base.repo
[root@192 yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@192 yum.repos.d]# sed -i 's|$releasever|8|' /etc/yum.repos.d/CentOS-Base.repo //把centos文件里的$releasever替换成8
//配置epel源
[root@192 yum.repos.d]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
CentOS-8 - Base - mirrors.aliyun.com 957 kB/s | 2.3 MB 00:02
Installed:
epel-release-8-10.el8.noarch
Complete!
[root@192 yum.repos.d]# ls
CentOS-Base.repo epel-playground.repo epel-testing-modular.repo redhat.repo
epel-modular.repo epel.repo epel-testing.repo
[root@192 yum.repos.d]# sed -i 's|^#baseurl=https://download.fedoraproject.org/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@192 yum.repos.d]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
[root@192 yum.repos.d]# sed -i 's|$releasever|8|' /etc/yum.repos.d/epel* //把epel文件中所有的$releasever替换成8
//清理yum缓存,建立缓存
[root@192 ~]# yum clean all
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
17 files removed
[root@192 ~]# yum makecache
安装http
- 安装开发工具包
[root@192 ~]# yum -y groups mark install 'Development Tools'
- 创建apache服务和用户和组
[root@192 ~]# useradd -r -M -s /sbin/nologin apache
- 安装依赖包
[root@192 ~]# yum -y install bzip2 make openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ libxml2-devel
- 下载源码包
[root@192 ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.46.tar.bz2
[root@192 ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-1.7.0.tar.gz
[root@192 ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-util-1.6.1.tar.gz
[root@192 ~]# ls
anaconda-ks.cfg apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.46.tar.bz2
- 安装apr
[root@192 ~]# tar xf apr-1.7.0.tar.gz
[root@192 ~]# cd apr-1.7.0
[root@192 apr-1.7.0]# vim configure
cfgfile=${ofile}T
trap "$RM "$cfgfile"; exit 1" 1 2 15
# $RM "$cfgfile" //删除这一行,或者#注释
[root@192 apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@192 apr-1.7.0]# make
[root@192 apr-1.7.0]# make install
- 安装apr-util
[root@192 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@192 apr-util-1.6.1]# make
[root@192 apr-util-1.6.1]# make install
- 安装httpd
[root@192 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@192 httpd-2.4.46]# make
[root@192 httpd-2.4.46]# make install
- 配置apache
[root@192 httpd-2.4.46]# cd
[root@192 ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh //配置环境变量
[root@192 ~]# source /etc/profile.d/httpd.sh
[root@192 ~]# which apachectl //查看apachectl命令
/usr/local/apache/bin/apachectl
[root@192 ~]# ln -s /usr/local/apache/include/ /usr/include/httpd
[root@192 ~]# vim /etc/man_db.conf
MANDATORY_MANPATH /usr/man
MANDATORY_MANPATH /usr/share/man
MANDATORY_MANPATH /usr/local/share/man
MANDATORY_MANPATH /usr/local/apache/man //加上这行配置
[root@192 ~]# sed -i '/#ServerName/s/#//g' /etc/httpd24/httpd.conf //取消ServerName前面的注释
[root@192 ~]# apachectl start //启动apahce
[root@192 ~]# ss -antl //80端口已经启动了
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
安装mysql
- 安装依赖包
[root@192 ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs
- 创建mysql的用户和组
[root@192 ~]# useradd -r -M -s /sbin/nologin mysql
- 下载源码包
[root@192 ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
[root@192 ~]# ls
anaconda-ks.cfg apr-util-1.6.1 httpd-2.4.46.tar.bz2
apr-1.7.0 apr-util-1.6.1.tar.gz mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
apr-1.7.0.tar.gz httpd-2.4.46
[root@192 ~]#
- 解压mysql
[root@192 ~]# tar xf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
- 设置软链接并修改属主和属组
[root@192 ~]# tar xf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@192 ~]# ln -s /usr/local/mysql-5.7.31-linux-glibc2.12-x86_64 /usr/local/mysql
[root@192 ~]# chown -R mysql.mysql /usr/local/mysql*
[root@192 ~]# ll /usr/local/
total 0
drwxr-xr-x. 13 root root 152 Jan 2 16:43 apache
drwxr-xr-x. 6 root root 58 Jan 2 16:30 apr
drwxr-xr-x. 5 root root 43 Jan 2 16:33 apr-util
drwxr-xr-x. 2 root root 6 Aug 12 2018 bin
drwxr-xr-x. 2 root root 6 Aug 12 2018 etc
drwxr-xr-x. 2 root root 6 Aug 12 2018 games
drwxr-xr-x. 2 root root 6 Aug 12 2018 include
drwxr-xr-x. 2 root root 6 Aug 12 2018 lib
drwxr-xr-x. 2 root root 6 Aug 12 2018 lib64
drwxr-xr-x. 2 root root 6 Aug 12 2018 libexec
lrwxrwxrwx. 1 mysql mysql 46 Jan 2 17:06 mysql -> /usr/local/mysql-5.7.31-linux-glibc2.12-x86_64
drwxr-xr-x. 9 mysql mysql 129 Jun 2 2020 mysql-5.7.31-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 Aug 12 2018 sbin
drwxr-xr-x. 5 root root 49 Jan 2 15:06 share
drwxr-xr-x. 2 root root 6 Aug 12 2018 src
- 添加环境变量
[root@192 ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@192 ~]# source /etc/profile.d/mysql.sh
[root@192 ~]# which mysql //可以看到有mysql命令
/usr/local/mysql/bin/mysql
- 建立数据存放目录
[root@192 ~]# mkdir /opt/data
[root@192 ~]# chown -R mysql.mysql /opt/data
[root@192 ~]# ll /opt/
total 0
drwxr-xr-x. 2 mysql mysql 6 Jan 2 17:07 data
- 初始化数据库
[root@192 ~]# mysqld --initialize --user=mysql --datadir=/opt/data
2021-01-02T09:08:06.425204Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-01-02T09:08:06.574243Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-01-02T09:08:06.603296Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-01-02T09:08:06.663300Z 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: 06dc94bf-4cda-11eb-9cfb-000c294ad3f8.
2021-01-02T09:08:06.664151Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-01-02T09:08:07.785877Z 0 [Warning] CA certificate ca.pem is self signed.
2021-01-02T09:08:07.866512Z 1 [Note] A temporary password is generated for root@localhost: T%&qOEqnN1UZ
[root@192 ~]# echo 'T%&qOEqnN1UZ' > a //把密码保存到a等下方便查看
- 配置mysql
[root@192 ~]# vim /etc/my.cnf
[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
//配置以上8行配置
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
- 配置服务启动脚本
[root@192 ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@192 ~]# vim /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/opt/data
- 启动mysql
[root@192 ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/192.168.110.133.err'.
SUCCESS!
[root@192 ~]# ss -antl //可以看到3306端口
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@192 ~]#
- 用刚刚的临时密码登录mysql,并设置新密码
[root@192 ~]# cat a
T%&qOEqnN1UZ
[root@192 ~]# mysql -uroot -p'T%&qOEqnN1UZ'
mysql: [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 3
Server version: 5.7.31
Copyright (c) 2000, 2020, 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('123456');
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> quit
Bye
- 修改完后继续配置文件
[root@192 ~]# vim /etc/man_db.conf
#MANDATORY_MANPATH /usr/src/pvm3/man
#
MANDATORY_MANPATH /usr/man
MANDATORY_MANPATH /usr/share/man
MANDATORY_MANPATH /usr/local/share/man
MANDATORY_MANPATH /usr/local/apache/man
MANDATORY_MANPATH /usr/local/mysql/man //加上这一行
[root@192 ~]# vim /etc/ld.so.conf.d/mysql.conf
usr/local/mysql/lib //添加这个配置
[root@192 ~]# ldconfig
安装php
- 安装所需要的依赖包
[root@192 ~]# 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 pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd
- 安装php
[root@192 ~]# yum -y install php-*
[root@192 ~]# which php
/usr/bin/php
[root@192 ~]# php -v //查看php版本号
PHP 7.2.24 (cli) (built: Oct 22 2019 08:28:36) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.24, Copyright (c) 1999-2018, by Zend Technologies
- 启动php
[root@192 ~]# systemctl start php-fpm
配置apache
- 启用代理模块
[root@192 ~]# vim /etc/httpd24/httpd.conf
LoadModule proxy_module modules/mod_proxy.so //取消注释
#LoadModule proxy_connect_module modules/mod_proxy_connect.so
#LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
#LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so //取消注释
- 编辑www.conf
[root@192 ~]# vim /etc/php-fpm.d/www.conf
; Note: This value is mandatory.
;listen = /run/php-fpm/www.sock //前面加上;
listen = 127.0.0.1:9000
[root@192 ~]# systemctl restart php-fpm //重启服务
[root@192 ~]# ss -antl //本机ip端口起来了
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@192 ~]#
- 创建虚机主机目录并生成php测试页面
[root@192 htdocs]# mkdir /usr/local/apache/htdocs/html
[root@192 htdocs]# ls
html index.html
[root@192 ~]# vim /usr/local/apache/htdocs/html/index.php
<?php
phpinfo();
?>
[root@192 ~]# chown -R apache.apache /usr/local/apache/htdocs/ //修改属主和属组
[root@192 ~]# ll /usr/local/apache/htdocs/
total 4
drwxr-xr-x. 2 apache apache 23 Jan 2 17:56 html
-rw-r--r--. 1 apache apache 45 Jun 12 2007 index.html
- 配置虚拟主机
[root@192 ~]# vim /etc/httpd24/httpd.conf //在最后一行加上配置
<VirtualHost *:80>
DocumentRoot "/usr/local/apache/htdocs/html"
ServerName www.leichen.com
ProxyRequests Off ProxyPassMatch ^/(.*.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/html/$1
<Directory "/usr/local/apache/htdocs/html">
Options none
AllowOverride none
Require all granted
</Directory>
</VirtualHost>
[root@192 ~]# apachectl restart //重启
[root@192 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@192 ~]# 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@192 ~]# vim /etc/httpd24/httpd.conf
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
DirectoryIndex index.php index.html //添加index.php 优先访问前面的
</IfModule>
- 重启apache服务,关闭防火墙
[root@192 ~]# apachectl restart
[root@192 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@192 ~]# systemctl stop firewalld
[root@192 ~]# setenforce 0