简介
LAMP是指一组通常一起使用来运行动态网站或者服务器的自由软件名称首字母缩写:
LAMP平台构建
LAMP平台软件安装次序
httpd -----> MySQL ----> PHP
注意:PHP要求httpd使用prefork MPM
安装httpd
~准备工作
//yum源配置(配置阿里源)
[root@localhost ~]# cd /etc/yum.repos.d/ //进入到yum.repos.d目录
//下载新的 CentOS-Base.repo 到 /etc/yum.repos.d/
[root@localhost yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
// 非阿里云ECS用户执行下面的命令
[root@localhost yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@localhost yum.repos.d]# dnf clean all //清除缓存
//配置epel(RHEL 8)
1)安装 epel 配置包
[root@localhost yum.repos.d]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
2)将 repo 配置中的地址替换为阿里云镜像站地址
[root@localhost yum.repos.d]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@localhost yum.repos.d]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
[root@localhost yum.repos.d]# dnf clean all //清除缓存
[root@localhost yum.repos.d]# dnf makecache //建立缓存
//安装vim和wget
[root@localhost ~]# dnf -y install vim wget
//安装开发工具包
dnf -y groups mark install 'Development Tools' //里面包括gcc c++ make等工具包(它是标志安装不是真的安装) //真的安装命令 dnf -y groupinstall 'Development Tools'
[root@localhost ~]# dnf -y groups mark install 'Development Tools'
//需要创建一个apache的系统用户
[root@localhost ~]# useradd -r -M -s /sbin/nologin apache // -M没有家目录,-s不允许登录
//安装依赖包
[root@localhost ~]# dnf -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make
// 下载和安装Apr和apr-util
[root@localhost ~]# wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz //下载Apr
[root@localhost ~]# wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz //下载apr-util
//下载httpd
[root@localhost ~]# wget https://downloads.apache.org/httpd/httpd-2.4.53.tar.gz
[root@localhost ~]# tar xf apr-1.7.0.tar.gz //解压Apr
[root@localhost ~]# tar xf apr-util-1.6.1.tar.gz //解压apr-util
[root@localhost ~]# tar xf httpd-2.4.53.tar.gz //解压httpd
[root@localhost apr-1.7.0]# vim configure
cfgfile="${ofile}T"
trap "$RM \"$cfgfile\"; exit 1" 1 2 15
# $RM "$cfgfile" //将此行加上注释,或者删除此行
//安装apr源码包
//第一步 ./configure
[root@localhost ~]# cd apr-1.7.0/
[root@localhost apr-1.7.0]# ./configure --prefix=/usr/local/apr
//第二步 make
[root@localhost apr-1.7.0]# make -j 3
//第三步 make install
[root@localhost apr-1.7.0]# make install
// 安装apr-util源码包
//第一步 ./configure
[root@localhost ~]# cd apr-util-1.6.1
[root@localhost apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
//第二步 make
[root@localhost apr-util-1.6.1]# make -j 3
//第三步 make install
[root@localhost apr-util-1.6.1]# make install
//安装httpd源码包
//第一步 ./configure
[root@localhost ~]# cd httpd-2.4.53
[root@localhost httpd-2.4.53]# ./configure --prefix=/usr/local/apache \
--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
//第二步 make
[root@localhost httpd-2.4.53]# make -j 3
//第三步 make install
[root@localhost httpd-2.4.53]# make install
//设置环境变量
[root@localhost ~]# cd /usr/local/apache
[root@localhost apache]# ls
bin cgi-bin error icons logs manual
build conf htdocs include man modules
//创建环境变量后httpd和apachectl命令可以使用了
[root@localhost ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/apache.sh
[root@localhost ~]# source /etc/profile.d/apache.sh
[root@localhost ~]# which httpd
/usr/local/apache/bin/httpd
[root@localhost ~]# which apachectl
/usr/local/apache/bin/apachectl
// /usr/local/apache/ 目录下常用目录解释
bin 放命令
conf 放配置文件
htdocs 放网站
logs 放日志
include 头文件
man 帮助文档
// 设置头文件映射关系
[root@localhost ~]# ls /usr/local/apache/ //有头文件 include 所以需要做链接关系
bin cgi-bin error icons logs manual
build conf htdocs include man modules
[root@localhost ~]# ln -s /usr/local/apache/include/ /usr/include/apache
// 设置man文档
[root@localhost ~]# vim /etc/man_db.conf
MANDATORY_MANPATH /usr/local/share/man //在这个的下面添加下面一条
MANDATORY_MANPATH /usr/local/apache/man //添加
使用systemctl命令设置httpd、让它开机自启
使用源码包安装apache服务 默认是不能用systemctl的
任何源码安装的服务都适用
[root@localhost ~]# cd /usr/lib/systemd/system
[root@localhost system]# ls sshd.service
sshd.service
[root@localhost system]# cp sshd.service httpd.service //复制一份这个文件改名为httpd.service
[root@localhost system]# vim httpd.service //编辑这个文件
[root@localhost system]# cat httpd.service
[Unit]
Description=httpd server daemon //修改为httpd
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start //更改为apachectl的路径 开启
ExecStop=/usr/local/apache/bin/apachectl stop //关闭
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@localhost system]# systemctl daemon-reload //重新加载服务 让其生效
[root@localhost system]# systemctl status httpd //此时就可以使用systemcl 查看httpd状态了
● httpd.service - httpd server daemon
Loaded: loaded (/usr/lib/systemd/system/httpd.service; >
Active: inactive (dead)
lines 1-3/3 (END)
[root@localhost system]# systemctl start httpd //开启httpd服务
[root@localhost system]# systemctl enable --now httpd //设置为开机自启
[root@localhost system]# systemctl status httpd //查看状态
● httpd.service - httpd server daemon
Loaded: loaded (/usr/lib/systemd/system/httpd.service; >
Active: active (running) since Sat 2022-04-16 21:00:21 >
Main PID: 57208 (httpd)
//启动服务前把防火墙关了
// 设置防火墙 Selinux httpd
[root@localhost ~]# systemctl disable --now firewalld //开机不自启,并且立马关闭
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# systemctl status firewalld //查看防火墙状态,开机不自启
● firewalld.service - firewalld - dynamic firewall da>
Loaded: loaded (/usr/lib/systemd/system/firewalld.>
Active: inactive (dead)
Docs: man:firewalld(1)
[root@localhost ~]# getenforce
Enforcing
[root@localhost ~]# setenforce 0 关闭selinux 当前生效
[root@localhost ~]# getenforce
Permissive
[root@localhost ~]# vim /etc/selinux/config
SELINUX=disabled //第一个修改为disabled,永久关闭
访问虚拟机的ip地址
//解决启动服务和关闭服务的警告信息
[root@localhost ~]# cd /usr/local/apache/conf/ //进到配置文件目录
[root@localhost conf]# ls
extra httpd.conf magic mime.types original
[root@localhost conf]# vim httpd.conf
ServerName www.example.com:80 //将这一行前面的注释取消掉
//安装MySQL
//安装MySQL5.7
[root@localhost ~]# wget https://repo.mysql.com//yum/mysql-5.7-community/el/7/x86_64/mysql-community-server-5.7.37-1.el7.x86_64.rpm //下载安装包server
[root@localhost ~]# wget https://repo.mysql.com//yum/mysql-5.7-community/el/7/x86_64/mysql-community-common-5.7.37-1.el7.x86_64.rpm //下载安装包common
[root@localhost ~]# wget https://repo.mysql.com//yum/mysql-5.7-community/el/7/x86_64/mysql-community-client-5.7.37-1.el7.x86_64.rpm //下载安装包client
[root@localhost ~]# wget https://repo.mysql.com//yum/mysql-5.7-community/el/7/x86_64/mysql-community-devel-5.7.37-1.el7.x86_64.rpm //下载安装包devel
[root@localhost ~]# wget https://repo.mysql.com//yum/mysql-5.7-community/el/7/x86_64/mysql-community-libs-5.7.37-1.el7.x86_64.rpm //下载安装包libs
[root@localhost ~]# ls
anaconda-ks.cfg
mysql-community-client-5.7.37-1.el7.x86_64.rpm
mysql-community-common-5.7.37-1.el7.x86_64.rpm
mysql-community-devel-5.7.37-1.el7.x86_64.rpm
mysql-community-libs-5.7.37-1.el7.x86_64.rpm
mysql-community-server-5.7.37-1.el7.x86_64.rpm
[root@localhost ~]# dnf -y install *.rpm //安装 .rpm 后缀的所有包
mysql配置
#启动mysql并设置开机自动启动
systemctl enable --now mysqld
[root@localhost ~]# systemctl enable --now mysqld //启动MySQL服务
systemctl status mysqld
[root@localhost ~]# systemctl status mysqld //查看MySQL状态,并且开机自启
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enable>
Active: active (running) since Mon 2022-04-18 15:37:15 CST; 14>
Docs: man:mysqld(8)
#确保3306端口已经监听起来
ss -antl
[root@localhost ~]# ss -antl //查看MySQL端口号,默认是3306
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
#在日志文件中找出临时密码
grep "password" /var/log/mysqld.log
[root@localhost ~]# grep "password" /var/log/mysqld.log
2022-04-21T12:57:47.909339Z 1 [Note] A temporary password is generated for root@localhost: mt!FK=Wc?9lq //安装的时候所设置的密码,临时的
#使用获取到的临时密码登录mysql
[root@localhost ~]# 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.37
Copyright (c) 2000, 2022, 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> //看到有这样的标识符则表示成功登录了
//也可以这样做登录
[root@localhost ~]# mysql -uroot -p'mt!FK=Wc?9lq'
//设置密码
mysql> set password = password('RunTime123!')
//为避免mysql自动升级,这里需要卸载最开始安装的yum源
rpm -e mysql57-community-release
//安装PHP
PHP官网 https://www.php.net/
//下载PHP
[root@localhost ~]# wget https://www.php.net/distributions/php-7.4.29.tar.xz
[root@localhost ~]# tar xf php-7.4.29.tar.xz //解压
//安装依赖包
[root@localhost ~]# dnf -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
//第一步 ./configure
[root@localhost ~]# cd php-7.4.29
[root@localhost php-7.4.29]# ./configure --prefix=/usr/local/php7 \
--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 \
--enable-gd \
--with-jpeg \
--with-zlib-dir \
--with-freetype \
--with-gettext \
--enable-json \
--enable-mbstring \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-readline \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--with-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-pcntl \
--enable-posix
// make编译
[root@localhost php-7.4.29]# make -j 3
//make install
[root@localhost php-7.4.29]# make install
//安装PHP报错信息和解决
//报错
configure: error: Package requirements (sqlite3 > 3.7.4) were not met:
//解决
[root@localhost php-7.4.29]# dnf -y install sqlite-devel
//报错
configure: error: Package requirements (oniguruma) were not met:
//解决
[root@localhost ~]# dnf -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
//报错
configure: error: Package requirements (libzip >= 0.11 libzip != 1.3.1 libzip != 1.7.0) were not met:
//解决
[root@localhost ~]# dnf -y install libzip-devel
//安装后配置
[root@localhost ~]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@localhost ~]# source /etc/profile.d/php7.sh
[root@localhost ~]# which php
/usr/local/php7/bin/php
[root@localhost ~]# php -v
PHP 7.4.29 (cli) (built: Apr 21 2022 22:27:56) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
[root@localhost ~]#
//配置php-fpm
[root@localhost php-7.4.29]# \cp php.ini-production /etc/php.ini //这个文件已存在所以要加上\表示覆盖
[root@localhost php-7.4.29]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-7.4.29]# chmod +x /etc/rc.d/init.d/php-fpm
[root@localhost php-7.4.29]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[root@localhost php-7.4.29]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
//启动php-fpm
[root@localhost]# service php-fpm start
Starting php-fpm done
//默认情况下,fpm监听在127.0.0.1的9000端口,也可以使用如下命令验证其是否已经监听在相应的套接字
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
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 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
[root@localhost ~]# pkill php-fpm //杀掉进程
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
**使用systemctl命令设置php、让它开机自启
[root@localhost ~]# cd /usr/lib/systemd/system
[root@localhost system]# cp sshd.service php-fpm.service
[root@localhost system]# cat php-fpm.service
[Unit]
Description=php-fpm server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/etc/init.d/php-fpm
ExecStop=/usr/bin/kill -9 $MAINPID
ExecReload=/usr/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@localhost system]# systemctl daemon-reload //让文件生效
[root@localhost system]# systemctl status php-fpm
● php-fpm.service - php-fpm server daemon
Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; disable>
Active: inactive (dead)
//启动php
[root@localhost ~]# /etc/init.d/php-fpm start
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
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 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
Windows 10操作
为真机的host文件添加ip对应域名做映射
虚拟机ip :192.168.229.130
域名: test.example.com
配置apache
启用代理模块
在apache httpd 2.4以后已经专门有一个模块针对FastCGI的实现,此模块为mod_proxy_fcgi.so,它其实是作为mod_proxy.so模块的扩展,因此,这两个模块都要加载,编辑httpd.conf文件,取消以下两行内容的注释:
[root@localhost ~]# cd /usr/local/apache/conf/
[root@localhost conf]# ls
extra httpd.conf magic mime.types original
[root@localhost conf]# vim httpd.conf //把下面两条代码的注释取消掉
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
配置虚拟主机
在需要使用fcgi的虚拟主机中添加类似如下两行:
//创建虚拟主机目录并生成php测试页面
[root@localhost ~]# cd /usr/local/apache/
[root@localhost apache]# ls
bin cgi-bin error icons logs manual
build conf htdocs include man modules
[root@localhost apache]# cd htdocs/
[root@localhost htdocs]# ls
index.html
[root@localhost htdocs]# mkdir test.com
[root@localhost htdocs]# cd test.com/
[root@localhost test.com]# ls
[root@localhost test.com]# vim index.php
[root@localhost test.com]# cat index.php
<?php
phpinfo();
?>
//在配置文件的最后加入以下内容
<VirtualHost *:80>
DocumentRoot "/usr/local/apache/htdocs/test.com"
ServerName test.example.com
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/test.com/$1
<Directory "/usr/local/apache/htdocs/test.com">
Options none
AllowOverride none
Require all granted
</Directory>
</VirtualHost>
[root@localhost ~]# vim /usr/local/apache/conf/httpd.conf
[root@localhost ~]# tail -11 /usr/local/apache/conf/httpd.conf
<VirtualHost *:80>
DocumentRoot "/usr/local/apache/htdocs/test.com"
ServerName test.example.com
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/test.com/$1
<Directory "/usr/local/apache/htdocs/test.com">
Options none
AllowOverride none
Require all granted
</Directory>
</VirtualHost>
//搜索AddType,添加以下内容
[root@localhost ~]# vim /usr/local/apache/conf/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 #添加此行
//搜索index.html,添加以下内容
<IfModule dir_module>
DirectoryIndex index.php index.html //添加 index.php
</IfModule>
浏览器测试
域名访问
ip访问