LAMP 平台软件安装次序:
httpd --> mysql --> php
注意:php 要求 httpd 使用 prefork MPM。
安装 httpd
YUM 源配置
[root@localhost ~]# dnf -y install wget vim epel-release
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ls
CentOS-Stream-AppStream.repo CentOS-Stream-RealTime.repo
CentOS-Stream-BaseOS.repo CentOS-Stream-ResilientStorage.repo
CentOS-Stream-Debuginfo.repo CentOS-Stream-Sources.repo
CentOS-Stream-Extras-common.repo epel-modular.repo
CentOS-Stream-Extras.repo epel-playground.repo
CentOS-Stream-HighAvailability.repo epel.repo
CentOS-Stream-Media.repo epel-testing-modular.repo
CentOS-Stream-NFV.repo epel-testing.repo
CentOS-Stream-PowerTools.repo
[root@localhost yum.repos.d]# rm -f *
[root@localhost yum.repos.d]# ls
[root@localhost yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[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]# cd
[root@localhost ~]#
安装开发工具包
[root@localhost ~]# yum groups mark install 'Development Tools'
CentOS-8.5.2111 - Base - mirrors.aliyun.com 11 kB/s | 3.9 kB 00:00
CentOS-8.5.2111 - Extras - mirrors.aliyun.com 7.4 kB/s | 1.5 kB 00:00
CentOS-8.5.2111 - AppStream - mirrors.aliyun.com 26 kB/s | 4.3 kB 00:00
Dependencies resolved.
===================================================================================
Package Architecture Version Repository Size
===================================================================================
Installing Groups:
Development Tools
Transaction Summary
===================================================================================
Is this ok [y/N]: y
Complete!
创建 apache 服务的用户和组
[root@localhost ~]# useradd -r -M -s /sbin/nologin apache
[root@localhost ~]# id apache
uid=994(apache) gid=991(apache) groups=991(apache)
安装依赖包
[root@localhost apr-1.6.5]# yum -y install make openssl-devel pcre-devel expat-devel libtool gcc gcc-c++
下载和安装 apr 以及 apr-util
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://downloads.apache.org/httpd/httpd-2.4.54.tar.bz2
[root@localhost src]# wget https://downloads.apache.org/apr/apr-1.6.5.tar.bz2
[root@localhost src]# wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.bz2
[root@localhost src]# ls
apr-1.6.5.tar.bz2 apr-util-1.6.1.tar.bz2 debug httpd-2.4.54.tar.bz2 kernels
[root@localhost src]# tar xf apr-1.6.5.tar.bz2
[root@localhost src]# cd apr-1.6.5
[root@localhost apr-1.6.5]# vim configure
......
# $RM "$cfgfile" //将此行加上注释,或者删除此行
......
[root@localhost apr-1.6.5]# ./configure --prefix=/usr/local/apr
[root@localhost apr-1.6.5]# make && make install
[root@localhost apr-1.6.5]# cd ..
[root@localhost src]# tar xf apr-util-1.6.1.tar.bz2
[root@localhost src]# cd apr-util-1.6.1
[root@localhost apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@localhost apr-util-1.6.1]# make && make install
编译安装 httpd
[root@localhost apr-util-1.6.1]# cd ..
[root@localhost src]# tar xf httpd-2.4.54.tar.bz2
[root@localhost src]# cd httpd-2.4.54
[root@localhost httpd-2.4.54]# ./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@localhost httpd-2.4.54]# make && make install
安装后配置
[root@localhost httpd-2.4.54]# cd /usr/local/apache/
[root@localhost apache]# ls
bin build cgi-bin error htdocs icons include logs man manual modules
[root@localhost apache]# echo 'export PATH=$PATH:/usr/local/apache/bin' > /etc/profile.d/httpd.sh
[root@localhost apache]# source /etc/profile.d/httpd.sh
[root@localhost apache]# ln -s /usr/local/apache/include/ /usr/include/httpd
[root@localhost apache]# vim /etc/man_db.conf
......
MANDATORY_MANPATH /usr/local/apache/man // 添加此行
......
取消 ServerName 前面的注释
[root@localhost apache]# vim /etc/httpd24/httpd.conf
......
ServerName www.example.com:80
......
启动 apache
[root@localhost apache]# which apachectl
/usr/local/apache/bin/apachectl
[root@localhost apache]# cd /usr/lib/systemd/system
[root@localhost system]# cp sshd.service httpd.service
[root@localhost system]# vim httpd.service
[Unit]
Description=httpd server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start
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]# cd
[root@localhost ~]# systemctl status httpd
● httpd.service - httpd server daemon
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset:>
Active: inactive (dead)
Aug 02 16:51:46 localhost.localdomain systemd[1]: httpd.service: Service has more >
[root@localhost ~]# systemctl start httpd
[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 [::]:*
[root@localhost ~]# systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
关闭防火墙
[root@localhost ~]# systemctl stop firewalld
[root@localhost system]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# vim /etc/selinux/config
......
SELINUX=disabled //将enforcing模式换成disabled
......
[root@localhost ~]# setenforce 0
安装 mysql
安装依赖包
[root@localhost ~]# yum -y install ncurses-compat-libs ncurses-devel openssl-devel openssl cmake
创建用户和组
[root@localhost ~]# useradd -r -M -s /sbin/nologin mysql
[root@localhost ~]# id mysql
uid=993(mysql) gid=990(mysql) groups=990(mysql)
下载二进制格式的 mysql 软件包
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# ls
apr-1.6.5 httpd-2.4.54
apr-1.6.5.tar.bz2 httpd-2.4.54.tar.bz2
apr-util-1.6.1 kernels
apr-util-1.6.1.tar.bz2 mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
debug
解压软件至 /usr/local/
[root@localhost src]# tar xf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost src]# cd /usr/local/
[root@localhost local]# ls
apache bin include libexec share
apr etc lib mysql-5.7.37-linux-glibc2.12-x86_64 src
apr-util games lib64 sbin
[root@localhost local]# ln -s mysql-5.7.37-linux-glibc2.12-x86_64/ mysql
修改属主属组
[root@localhost local]# chown -R mysql.mysql mysql*
[root@localhost local]# ll
total 0
drwxr-xr-x. 13 root root 152 Aug 2 16:38 apache
drwxr-xr-x. 6 root root 58 Aug 2 16:30 apr
drwxr-xr-x. 5 root root 43 Aug 2 16:32 apr-util
drwxr-xr-x. 2 root root 6 Jun 22 2021 bin
drwxr-xr-x. 2 root root 6 Jun 22 2021 etc
drwxr-xr-x. 2 root root 6 Jun 22 2021 games
drwxr-xr-x. 2 root root 6 Jun 22 2021 include
drwxr-xr-x. 2 root root 6 Jun 22 2021 lib
drwxr-xr-x. 3 root root 17 Jul 13 13:19 lib64
drwxr-xr-x. 2 root root 6 Jun 22 2021 libexec
lrwxrwxrwx. 1 mysql mysql 36 Aug 2 17:14 mysql -> mysql-5.7.37-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 mysql mysql 129 Aug 2 17:13 mysql-5.7.37-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 Jun 22 2021 sbin
drwxr-xr-x. 5 root root 49 Jul 13 13:19 share
drwxr-xr-x. 2 root root 6 Jun 22 2021 src
bin、include、lib、man
[root@localhost local]# cd mysql
[root@localhost mysql]# ls
bin docs include lib LICENSE man README share support-files
[root@localhost mysql]# echo 'export PATH=$PATH:/usr/local/mysql/bin' > /etc/profile.d/mysql.sh
[root@localhost mysql]# source /etc/profile.d/mysql.sh
[root@localhost mysql]# ln -s /usr/local/mysql/include/ /usr/include/mysql
[root@localhost mysql]# echo '/usr/local/mysql/lib/' > /etc/ld.so.conf.d/mysql.conf
[root@localhost mysql]# ldconfig
[root@localhost mysql]# vim /etc/man_db.conf
......
MANDATORY_MANPATH /usr/local/mysql/man //添加此行
......
建立数据存放目录
[root@localhost mysql]# cd
[root@localhost ~]# mkdir /opt/data
[root@localhost ~]# chown -R mysql.mysql /opt/data/
初始化数据库
[root@localhost ~]# mysqld --initialize --user mysql --datadir /opt/data/
[root@localhost ~]# echo 'htXtF?ZZ8e6;' > password //此处是临时密码储存文件,临时密码随机生成
生成配置文件
[root@localhost ~]# 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
配置服务启动脚本
[root@localhost ~]# cd /usr/local/mysql/support-files/
[root@localhost support-files]# ls
magic mysqld_multi.server mysql-log-rotate mysql.server
[root@localhost support-files]# file mysql.server
mysql.server: POSIX shell script, ASCII text executable
[root@localhost support-files]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost support-files]# vim /etc/init.d/mysqld
......
basedir=/usr/local/mysql/
datadir=/opt/data/
......
[root@localhost support-files]# cd /usr/lib/systemd/system
[root@localhost system]# cp sshd.service mysqld.service
[root@localhost system]# vim mysqld.service
[Unit]
Description=mysqld server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/etc/init.d/mysqld start
ExecStop=/etc/init.d/mysqld stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@localhost system]# systemctl daemon-reload
[root@localhost system]# systemctl status mysqld
● mysqld.service - mysqld server daemon
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset>
Active: inactive (dead)
启动 mysql
[root@localhost system]# systemctl enable --now mysqld
Synchronizing state of mysqld.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install enable mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
[root@localhost system]# 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 80 *:3306 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
修改密码
[root@localhost system]# cd
[root@localhost ~]# cat password
htXtF?ZZ8e6;
[root@localhost ~]# mysql -p'htXtF?ZZ8e6;'
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 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> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> exit
Bye
[root@localhost ~]# mysql -p123456
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.37 MySQL Community Server (GPL)
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>
安装 php
安装依赖包
[root@localhost ~]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel libzip-devel openldap-devel pcre-devel freetype freetype-devel gmp gmp-devel readline readline-devel libxslt libxslt-devel sqlite-devel php-mysqlnd
[root@localhost ~]# yum -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
下载 php
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://www.php.net/distributions/php-7.4.30.tar.xz
[root@localhost src]# tar xf php-7.4.30.tar.xz
编译安装 php
[root@localhost src]# cd php-7.4.30
[root@localhost php-7.4.30]# ./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 \
--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
[root@localhost php-7.4.30]# make && make install
安装后配置
[root@localhost php-7.4.30]# cd /usr/local/php7/
[root@localhost php7]# ls
bin etc include lib php sbin var
[root@localhost php7]# echo 'export PATH=$PATH:/usr/local/php7/bin' > /etc/profile.d/php7.sh
[root@localhost php7]# source /etc/profile.d/php7.sh
[root@localhost php7]# ln -s /usr/local/php7/include/ /usr/include/php7
[root@localhost php7]# echo '/usr/local/php7/lib/' > /etc/ld.so.conf.d/php7.conf
[root@localhost php7]# ldconfig
配置 php-fpm
[root@localhost php7]# cd /usr/src/php-7.4.30
[root@localhost php-7.4.30]# cp php.ini-production /etc/php.ini
cp: overwrite '/etc/php.ini'? y
[root@localhost php-7.4.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-7.4.30]# ll -d /etc/init.d/php-fpm
-rw-r--r--. 1 root root 2402 Aug 2 23:38 /etc/init.d/php-fpm
[root@localhost php-7.4.30]# chmod +x /etc/init.d/php-fpm
[root@localhost php-7.4.30]# cd /usr/local/php7/etc/
[root@localhost etc]# ls
pear.conf php-fpm.conf.default php-fpm.d
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# cd php-fpm.d/
[root@localhost php-fpm.d]# ls
www.conf.default
[root@localhost php-fpm.d]# cp www.conf.default www.conf
//编辑php-fpm的配置文件(/usr/local/php7/etc/php-fpm.conf):
//配置fpm的相关选项为你所需要的值:
[root@localhost ~]# vim /usr/local/php7/etc/php-fpm.conf
.....
.....
pm.max_children = 50 ;最多同时提供50个进程提供50个并发服务
pm.start_servers = 5 ;启动时启动5个进程
pm.min_spare_servers = 2 ;最小空闲进程数
pm.max_spare_servers = 8 ;最大空闲进程数
启动 php
[root@localhost php-fpm.d]# cd
[root@localhost ~]# cd /usr/lib/systemd/system
[root@localhost system]# cp sshd.service php-fpm.service
[root@localhost system]# vim php-fpm.service
[Unit]
Description=php-fpm server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/etc/init.d/php-fpm start
ExecStop=/etc/init.d/php-fpm stop
ExecReload=/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; disabled; vendo>
Active: inactive (dead)
[root@localhost system]# systemctl start php-fpm
[root@localhost system]# systemctl enable php-fpm
Synchronizing state of php-fpm.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install enable php-fpm
Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.
[root@localhost system]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
配置 apache
启用代理模块
在 apache httpd 2.4 以后已经专门有一个模块针对 FastCGI 的实现,此模块为 mod_proxy_fcgi.so,它其实是作为 mod_proxy.so 模块的扩展,因此,这两个模块都要加载,编辑 httpd.conf 文件,取消以下两行内容的注释:
- LoadModule proxy_module modules/mod_proxy.so
- LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
[root@localhost ~]# vim /etc/httpd24/httpd.conf
[root@localhost ~]# httpd -t
Syntax OK
配置虚拟主机
创建虚拟主机目录并生成 php 测试页面
[root@localhost ~]# cd /usr/local/apache/htdocs/
[root@localhost htdocs]# ls
index.html
[root@localhost htdocs]# rm -f *
[root@localhost htdocs]# ls
[root@localhost htdocs]# mkdir runtime
[root@localhost htdocs]# cd runtime/
[root@localhost runtime]# vim index.php
?php
phpinfo();
?>
[root@localhost runtime]# chown -R apache.apache /usr/local/apache/
[root@localhost runtime]# vim /etc/httpd24/httpd.conf
......
# Virtual hosts
Include /etc/httpd24/extra/httpd-vhosts.conf //此行取消注释
......
[root@localhost htdocs]# vim /etc/httpd24/extra/httpd-vhosts.conf
# Virtual Hosts
#
# Required modules: mod_log_config
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
# <URL:http://httpd.apache.org/docs/2.4/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost *:80>
DocumentRoot "/usr/local/apache/htdocs/runtime"
ServerName runtime.example.com
ErrorLog "logs/runtime.example.com-error_log"
CustomLog "logs/runtime.example.com-access_log" common
ProxyRequests Off //关闭正向代理
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/runtime/$1
<Directory "/usr/local/apache/htdocs/wangqing.com">
Options none
AllowOverride none
Require all granted
</Directory>
</VirtualHost>
[root@localhost runtime]# vim /etc/httpd24/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 //添加此行
......
<IfModule dir_module>
DirectoryIndex index.php index.html //添加index.php
</IfModule>
......
[root@localhost runtime]# cd
[root@localhost ~]# httpd -t
Syntax OK
[root@localhost ~]# systemctl restart httpd
[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 80 *:3306 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
验证
-
修改
/etc/hosts
文件,添加域名与 IP 的映射。
将 Windows 系统下的C:\Windows\System32\drivers\etc\hosts
文件移至桌面,使用记事本打开,添加192.168.91.128 runtime.example.com
,再移回。 -
在浏览器上使用域名访问,若看到以下界面则表示 lamp 架构搭建成功,否则请检查你的操作。