lamp独立部署

本文详细介绍如何从零开始在CentOS上部署LAMP环境,包括安装Apache、MySQL、PHP及其相关配置过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

lamp独立部署

安装httpd

安装开发工具包
[root@localhost ~]# ls /etc/yum.repos.d/   //虚拟机里面有centOS,这里就不再安装了
CentOS-Stream-AppStream.repo  CentOS-Stream-HighAvailability.repo
CentOS-Stream-BaseOS.repo     CentOS-Stream-Media.repo
CentOS-Stream-Debuginfo.repo  CentOS-Stream-PowerTools.repo
CentOS-Stream-Extras.repo     CentOS-Stream-RealTime.repo

[root@localhost ~]# dnf -y install epel-release

[root@localhost ~]# dnf -y groups mark install "Development Tools"

创建apache服务的用户
[root@localhost ~]# useradd -r -M -s /sbin/nologin apache

安装依赖包
[root@localhost ~]# dnf -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make
编译安装httpd

在官网下载apr-1.7.0.tar.bz2 apr-util-1.6.1.tar.bz2 httpd-2.4.43.tar.bz2这三个包并拖到/usr/src/下并解压到当前目录

[root@localhost ~]# cd /usr/src/
[root@localhost src]# ls
apr-1.7.0.tar.bz2  apr-util-1.6.1.tar.bz2  debug  httpd-2.4.43.tar.bz2  kernels

[root@localhost src]# tar xf apr-1.7.0.tar.bz2 
[root@localhost src]# tar xf apr-util-1.6.1.tar.bz2 
[root@localhost src]# tar xf httpd-2.4.43.tar.bz2 
[root@localhost src]# ls
apr-1.7.0          apr-util-1.6.1          debug         httpd-2.4.43.tar.bz2
apr-1.7.0.tar.bz2  apr-util-1.6.1.tar.bz2  httpd-2.4.43  kernels


[root@localhost src]# cd apr-1.7.0/
[root@localhost apr-1.7.0]# vim configure
    cfgfile="${ofile}T"
    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
    # $RM "$cfgfile"        //将此行加上注释,或者删除此行
    
//配置
[root@localhost apr-1.7.0]# ./configure --prefix=/usr/local/apr
//编译安装
[root@localhost apr-1.7.0]# make && make install

//配置
[root@localhost apr-1.7.0]# cd /usr/src/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


//配置
[root@localhost ~]# cd httpd-2.4.43
[root@localhost httpd-2.4.43]# ./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.43]# make && make install
安装后配置
[root@localhost ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[root@localhost ~]# source /etc/profile.d/httpd.sh
[root@localhost ~]# ln -s /usr/local/apache/include/ /usr/include/httpd
[root@localhost ~]# vim /etc/man_db.conf    //在这个文件里加入下列一行内容

MANDATORY_MANPATH                       /usr/local/apache/man

开启apache
[root@localhost ~]# apachectl start  
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message

[root@localhost ~]# systemctl disable firewalld  //永久性关闭防火墙
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

[root@localhost ~]# 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        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@localhost ~]# dnf -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
创建用户
[root@localhost ~]# useradd -r -M -s /sbin/nologin mysql
解压软件至/usr/local/

下载软件包去mysql官网下下载到本地后拖到虚拟机里

[root@localhost ~]# tar xf mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz -C /usr/local/

[root@localhost ~]# cd /usr/local/
[root@localhost local]# ls
apache    bin    include  libexec                              share
apr       etc    lib      mysql-5.7.33-linux-glibc2.12-x86_64  src
apr-util  games  lib64    sbin

[root@localhost local]# mv mysql-5.7.33-linux-glibc2.12-x86_64 mysql   //修改一下名字方便后续操作
[root@localhost local]# ls
apache  apr-util  etc    include  lib64    mysql  share
apr     bin       games  lib      libexec  sbin   src

修改目录/usr/local/mysql的属主属组
[root@localhost local]# ll /usr/local/mysql -d
drwxr-xr-x. 9 root root 129 512 19:18 /usr/local/mysql
[root@localhost local]# chown -R mysql.mysql /usr/local/mysql
[root@localhost local]# ll /usr/local/mysql -d
drwxr-xr-x. 9 mysql mysql 129 512 19:18 /usr/local/mysql

添加环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' >/etc/profile.d/mysql.sh
[root@localhost ~]# source /etc/profile.d/mysql.sh 
[root@localhost ~]# ln -s /usr/local/mysql/include /usr/include/mysql
[root@localhost ~]# vim /etc/ld.so.conf.d/mysql.conf  //添加下列一行内容

/usr/local/mysql/lib

[root@localhost ~]# ldconfig
[root@localhost ~]# vim /etc/man_db.conf   //添加下列一行内容
MANDATORY_MANPATH                       /usr/local/mysql/man

建立数据存放目录
[root@localhost ~]# mkdir /opt/data
[root@localhost ~]# chown -R mysql.mysql /opt/data
[root@localhost ~]# ll /opt/
总用量 0
drwxr-xr-x. 2 mysql mysql 6 512 21:46 data

初始化数据库
[root@localhost ~]# mysqld --initialize --user=mysql --datadir=/opt/data/
2021-05-12T13:49:08.214347Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-05-12T13:49:08.703901Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-05-12T13:49:08.782468Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-05-12T13:49:08.839617Z 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: d333fe0e-b328-11eb-8f31-000c29f893b1.
2021-05-12T13:49:08.840917Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-05-12T13:49:09.329992Z 0 [Warning] CA certificate ca.pem is self signed.
2021-05-12T13:49:09.468731Z 1 [Note] A temporary password is generated for root@localhost: ,WwgSw<M0?zw   //这个密码是随机生成的需要记住,所以给它放到一个文件夹里以防遗忘
[root@localhost ~]# echo ',WwgSw<M0?zw' > pass
[root@localhost ~]# cat pass
,WwgSw<M0?zw

生成配置文件
[root@localhost ~]# 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@localhost ~]# cat /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 ~]# ls /usr/local/mysql/
bin  docs  include  lib  LICENSE  man  README  share  support-files
[root@localhost ~]# ls /usr/local/mysql/support-files/
magic  mysqld_multi.server  mysql-log-rotate  mysql.server
[root@localhost ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

[root@localhost ~]# vim /etc/init.d/mysqld 
//补齐这两行=后面的路径
basedir=/usr/local/mysql
datadir=/opt/data

启动mysql
[root@localhost ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'.
 SUCCESS! 
[root@localhost ~]# 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              [::]:*              
LISTEN   0        80                     *:3306                *:*       

 [root@localhost ~]# mysql -uroot -p
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
[root@localhost ~]# dnf whatprovides libncurses.so.5
上次元数据过期检查:3:05:13 前,执行于 20210512日 星期三 185720秒。
ncurses-compat-libs-6.1-7.20180224.el8.i686 : Ncurses compatibility libraries
仓库        :baseos
匹配来源:
提供    : libncurses.so.5

[root@localhost ~]# dnf -y install ncurses-compat-libs
上次元数据过期检查:3:05:31 前,执行于 20210512日 星期三 185720秒。
依赖关系解决。
================================================================================
 软件包                   架构        版本                    仓库         大小
================================================================================
安装:
 ncurses-compat-libs      x86_64      6.1-7.20180224.el8      baseos      331 k

事务概要
================================================================================
安装  1 软件包

总下载:332 k
安装大小:1.2 M
下载软件包:
ncurses-compat-libs-6.1-7.20180224.el8.x86_64.r 413 kB/s | 331 kB     00:00    
--------------------------------------------------------------------------------
总计                                            228 kB/s | 331 kB     00:01     
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                                                                 1/1 
  安装    : ncurses-compat-libs-6.1-7.20180224.el8.x86_64                   1/1 
  运行脚本: ncurses-compat-libs-6.1-7.20180224.el8.x86_64                   1/1 
  验证    : ncurses-compat-libs-6.1-7.20180224.el8.x86_64                   1/1 
Installed products updated.

已安装:
  ncurses-compat-libs-6.1-7.20180224.el8.x86_64                                 

完毕!
  
mysql> set password = password('ZHANGde12+Jun')//修改密码
Query OK, 0 rows affected, 1 warning (0.00 sec)
    
设置开启自启
[root@localhost ~]# chkconfig --add mysqld
[root@localhost ~]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

mysqld          0:1:2:3:4:5:6:

安装php

安装依赖包
[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
[root@localhost ~]# dnf -y install php*
[root@localhost ~]# php -v
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


[root@localhost ~]# systemctl start php-fpm   //启动php-fpm
[root@localhost ~]# systemctl status php-fpm  //查看php-fpm的状态
● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; disabled; vendor pr>
   Active: active (running) since Wed 2021-05-12 22:21:25 CST; 28s ago
 Main PID: 563650 (php-fpm)
   Status: "Processes active: 0, idle: 5, Requests: 0, slow: 0, Traffic: 0req/s>
    Tasks: 6 (limit: 11070)
   Memory: 37.4M
   CGroup: /system.slice/php-fpm.service
           ├─563650 php-fpm: master process (/etc/php-fpm.conf)
           ├─563651 php-fpm: pool www
           ├─563652 php-fpm: pool www
           ├─563653 php-fpm: pool www
           ├─563654 php-fpm: pool www
           └─563656 php-fpm: pool www

512 22:21:25 localhost.localdomain systemd[1]: Starting The PHP FastCGI Proc>
512 22:21:25 localhost.localdomain systemd[1]: Started The PHP FastCGI Proce>

[root@localhost ~]# systemctl enable php-fpm    //设置开机自启
Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.

[root@localhost ~]# ss -antl    //没有9000端口
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              [::]:*              
LISTEN   0        80                     *:3306                *:*         
     
[root@localhost ~]# vim /etc/php-fpm.d/www.conf

;listen = /run/php-fpm/www.sock   //在行首加;
listen = 0.0.0.0:9000             //添加这一行

[root@localhost ~]# systemctl restart php-fpm  //重启php-fpm
[root@localhost ~]# ss -antl   //有9000端口
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              0.0.0.0:9000          0.0.0.0:*              
LISTEN   0        128                 [::]:111              [::]:*              
LISTEN   0        128                    *:80                  *:*              
LISTEN   0        128                 [::]:22               [::]:*              
LISTEN   0        5                  [::1]:631              [::]:*              
LISTEN   0        80                     *:3306                *:*              

配置apache

启用代理模块
[root@localhost ~]# vim /etc/httpd24/httpd.conf
//取消下列两行的注释
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
配置虚拟主机
//创建虚拟主机目录并生成php测试页面
[root@localhost ~]# mkdir /usr/local/apache/htdocs/yh
[root@localhost ~]# cd /usr/local/apache/htdocs
[root@localhost htdocs]# ls
index.html  yh
[root@localhost htdocs]# cd yh/
[root@localhost yh]# vim index.php
[root@localhost yh]# cat index.php
<?php
   phpinfo();
?>

[root@localhost yh]# chown -R apache.apache /usr/local/apache/htdocs/
[root@localhost yh]# ll /usr/local/apache
总用量 36
drwxr-xr-x.  2 root   root    262 512 19:06 bin
drwxr-xr-x.  2 root   root    167 512 19:06 build
drwxr-xr-x.  2 root   root     78 512 19:06 cgi-bin
drwxr-xr-x.  3 root   root   4096 512 19:06 error
drwxr-sr-x.  3 apache apache   34 512 22:42 htdocs
drwxr-xr-x.  3 root   root   8192 512 19:06 icons
drwxr-xr-x.  2 root   root   4096 512 19:06 include
drwxr-xr-x.  2 root   root     58 512 19:07 logs
drwxr-xr-x.  4 root   root     30 512 19:06 man
drwxr-sr-x. 14 root   root   8192 326 2020 manual
drwxr-xr-x.  2 root   root   4096 512 19:06 modules

[root@localhost httpd24]# vim httpd.conf

# Various default settings
#Include /etc/httpd24/extra/httpd-default.conf
Include /etc/httpd24/extra/vhosts.conf  //添加这一行

[root@localhost extra]# vim vhosts.conf
//在配置文件中加入以下内容
<VirtualHost *:80>
    DocumentRoot "/usr/local/apache/htdocs/yh"
    ServerName localhost:80
    ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/yh/$1
    <Directory "/usr/local/apache/htdocs/yh">
        Options none
        AllowOverride none
        Require all granted
    </Directory>
</VirtualHost>


[root@localhost httpd24]# vim 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  //添加此行

//在index.html前加上index.php 
<IfModule dir_module>index.php   
    DirectoryIndex index.php index.html
</IfModule>

开启apache
[root@localhost ~]# apachectl start

AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
httpd (pid 326884) already running
测试效果

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值