如何在CentOS 7上搭建LAMP环境(使用YUM或编译)-LMLPHP

什么是LAMP?

LAMP是Linux,Apache,MySQL和PHP的缩写。 它是一堆应用程序的堆栈,它们在Web服务器上一起工作以托管网站。 话虽如此,每个程序都有不同的目的:

在LAMP中, Linux 用作处理计算机上所有命令的服务器操作系统。 在本文中,将使用CentOS 7发行版。

  • Apache 是一种Web服务器软件,可管理HTTP请求以传递您的网站内容。
  • MySQL 是关系数据库管理系统(RDBMS),其功能是在服务器上维护用户数据。
  • PHP 是用于服务器端通信的脚本语言。

如何在CentOS 7上搭建LAMP环境(使用YUM或编译)-LMLPHP

版本信息:

在示例步骤中使用了以下版本的软件。操作时,请您以实际软件版本为准。

  • CentOS7:7.5.1804(最小化安装)
  • Apache:2.4.6
  • MySQL:5.7.31
  • PHP:7.0.33

环境准备。

  • 查看系统版本。
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
  • 关闭防火墙及SELinux。
[root@wencheng ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Tue 2021-04-27 11:53:37 CST; 2s ago
     Docs: man:firewalld(1)
  Process: 5265 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
 Main PID: 5265 (code=exited, status=0/SUCCESS)

注:参数是inactive,则防火墙为关闭状态;参数是active,则防火墙为开启状态。
[root@wencheng ~]# systemctl disable firewalld    //永久关闭防火墙
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@wencheng ~]# systemctl is-enabled firewalld    //确认永久关闭成功 
disabled

[root@wencheng ~]# sed -i.bak '7s#enforcing#disabled#' /etc/selinux/config    //永久关闭SELinux
[root@wencheng ~]# cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted

[root@wencheng ~]# setenforce 0    //临时关闭SELinux
[root@wencheng ~]# getenforce
Permissive
  • 更新所有更新包,但不更新内核。
[root@wencheng ~]# yum --exclude=kernel* --exclude=centos-release* update -y

第一部分:YUM安装

步骤一:YUM安装Apache

[root@wencheng ~]# yum -y install httpd httpd-manual mod_ssl mod_perl mod_auth_mysql    //安装Apache服务及扩展包

[root@wencheng ~]# httpd -v    //查看版本信息
Server version: Apache/2.4.6 (CentOS)
Server built:   Nov 16 2020 16:18:20

[root@wencheng ~]# systemctl start httpd    //启动Apache服务器

[root@wencheng ~]# systemctl enable httpd    //设置Apache开机自启动
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

 查看安装结果。

  • 在本地机器浏览器地址中,输入http://IP (搭建Apacha服务机器的IP),若返回页面如下图,即Apache服务启动成功。

如何在CentOS 7上搭建LAMP环境(使用YUM或编译)-LMLPHP

 步骤二:YUM安装并配置MySQL

[root@wencheng ~]# rpm -Uvh  http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm    //更新YUM源

[root@wencheng ~]# yum -y install mysql-community-server    //安装MySQL

[root@wencheng ~]# mysql -V    //查看MySQL版本
mysql  Ver 14.14 Distrib 5.7.34, for Linux (x86_64) using  EditLine wrapper

[root@wencheng ~]# systemctl start mysqld    //启动MySQL

[root@wencheng ~]# systemctl enable mysqld    //设置开机自启动
[root@wencheng ~]# systemctl daemon-reload

[root@wencheng ~]# grep password /var/log/mysqld.log    //查看MySQL初始密码
2021-04-27T06:24:20.509347Z 1 [Note] A temporary password is generated for root@localhost: 8kfepaL?l,#x

[root@wencheng ~]# mysql_secure_installation    //配置MySQL的安全性

Securing the MySQL server deployment.

Enter password for user root:      //输入上一步获取的root用户初始密码(8kfepaL?l,#x)

The existing password for the user account root has expired. Please set a new password.

New password:    //输入新密码,长度为8至30个字符,必须同时包含大小写英文字母、数字和特殊符号Re-enter new password:

Re-enter new password:    //再次输入新密码
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.

Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y

New password:    //输入新密码,长度为8至30个字符,必须同时包含大小写英文字母、数字和特殊符号

Re-enter new password:    //再次输入新密码

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y    //是否继续使用所提供的密码,输入Y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y    //是否删除匿名用户,输入Y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y    //禁止root远程登录,输入Y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y    //是否删除test库和对它的访问权限,输入Y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y    //是否重新加载授权表,输入Y
Success.

All done

 步骤三:YUM安装PHP

[root@wencheng ~]# yum install -y https://repo.ius.io/ius-release-el7.rpm https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm    //添加epel源

[root@wencheng ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm    //更新YUM源

[root@wencheng ~]# yum -y install php70w-devel php70w.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 \
php70w-mcrypt.x86_64 php70w-pdo.x86_64   php70w-mysqlnd  php70w-fpm php70w-opcache php70w-pecl-redis php70w-pecl-mongodb    //安装PHP

[root@wencheng ~]# php -v    //查看PHP版本
PHP 7.0.33 (cli) (built: Dec  6 2018 22:30:44) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.33, Copyright (c) 1999-2017, by Zend Technologies

[root@wencheng ~]# echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php    //在Apache网站根目录创建测试文件

[root@wencheng ~]# systemctl restart httpd    //重启Apache服务器

 查看安装结果。

  • 在本地机器的浏览器地址中,输入http//IP/phpinfo.php  ,若返回页面如下,即PHP安装成功。

如何在CentOS 7上搭建LAMP环境(使用YUM或编译)-LMLPHP

 第二部分:编译安装

环境准备

[root@wencheng ~]# rpm -qa | grep httpd    //查看是否已经安装了相关rpm包
[root@wencheng ~]# rpm -qa | grep mysql
[root@wencheng ~]# rpm -qa | grep php

[root@wencheng ~]# ls
apr-1.6.2.tar.bz2 apr-util-1.6.0.tar.bz2 httpd-2.4.6.tar.bz2 mysql-5.7.31.tar.gz php-7.0.33.tar.gz

[root@wencheng ~]# yum -y install gcc gcc-c++ make pcre pcre-devel expat-devel perl bzip2   //安装依赖包
[root@wencheng ~]# mkdir -pv /usr/local/app

步骤一:编译安装Apache 

解压并编译安装。

[root@wencheng ~]# tar xf apr-1.6.2.tar.bz2 -C /usr/local/src/
[root@wencheng ~]# tar xf apr-util-1.6.0.tar.bz2 -C /usr/local/src/
[root@wencheng ~]# tar xf httpd-2.4.6.tar.bz2 -C /usr/local/src/
[root@wencheng ~]# ls /usr/local/src/
apr-1.6.2  apr-util-1.6.0  httpd-2.4.6

[root@wencheng ~]# cd /usr/local/src/apr-1.6.2
[root@wencheng apr-1.6.2]# ./configure --prefix=/usr/local/app/apr
[root@wencheng apr-1.6.2]# make -j 4 && make install    //make -j 4 表示开4核同时进行编译: cat /proc/cpuinfo| grep "processor"| wc -l

[root@wencheng ~]# cd /usr/local/src/apr-util-1.6.0
[root@wencheng apr-util-1.6.0]# ./configure --prefix=/usr/local/app/apr-util --with-apr=/usr/local/app/apr
[root@wencheng apr-util-1.6.0]# make -j 4 && make install

[root@wencheng ~]# cd /usr/local/src/httpd-2.4.6
[root@wencheng httpd-2.4.6]# ./configure --prefix=/usr/local/app/apache --sysconfdir=/etc/apache \
--with-apr=/usr/local/app/apr --with-apr-util=/usr/local/app/apr-util --with-z --with-pcre \
--with-mpm=event --enable-so --enable-rewrite --enable-mpms-shared=all
[root@wencheng httpd-2.4.6]# make -j 4 && make install

说明:
--prefix=/usr/local/app/apache  指定httpd服务程序的安装路径
--sysconfdir=/etc/apache  指定httpd服务程序配置文件路径
--with-apr=/usr/local/app/apr    指明apr路径
--with-apr-util=/usr/local/app/apr-util    指明apr-util路径
--with-z     支持数据压缩
--with-pcre     兼容正则表达式
--with-mpm=event   指定流模型为event
--enable-so    启用动态加载模块支持,使httpd具备进一步扩展功能的能力
--enable-rewrite    启用网页地址重写功能,用于网站优化、防盗链及目录迁移维护
--enable-mpms-shared=all    全部的工作模型

启动apache服务。

启动apache服务方法一:
[root@wencheng ~]# vim /etc/systemd/system/httpd.service    //将以下代码写在httpd.service文件中并存放在etc/systemd/system/目录下
Description=Start httpd
[Service]
Type=simple
EnvironmentFile=/etc/apache/httpd.conf
ExecStart=/usr/local/app/apache/bin/httpd -k start -DFOREGROUND
ExecReload=/usr/local/app/apache/bin/httpd -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
[Install]
WantedBy=multi-user.target
[root@wencheng ~]# systemctl daemon-reload    //重新加载
[root@wencheng ~]# systemctl start httpd    //启动apache服务
启动apache服务方法二:
[root@wencheng ~]# echo "MANPATH /usr/local/app/apache/man" >> /etc/man_db.conf    //设置man路径
[root@wencheng ~]# echo 'PATH=/usr/local/app/apache/bin:$PATH' > /etc/profile.d/apache.sh    //设置PATH环境变量
[root@wencheng ~]# source /etc/profile.d/apache.sh
[root@wencheng ~]# ln -s /usr/include /usr/local/app/apache/include    //输出头文件
[root@wencheng apache]# echo 'PidFile "/var/run/httpd.pid"' >> /etc/apache/httpd.conf

[root@wencheng ~]# apachectl start    //启动apache服务
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::20c:29ff:feb2:9730. Set the 'ServerName' directive globally to suppress this message
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[root@wencheng ~]# ps -ef | grep httpd    //查看进程
root      46451      1  0 17:43 ?        00:00:00 /usr/local/app/apache/bin/httpd -k start
daemon    46452  46451  0 17:43 ?        00:00:00 /usr/local/app/apache/bin/httpd -k start
daemon    46453  46451  0 17:43 ?        00:00:00 /usr/local/app/apache/bin/httpd -k start
daemon    46454  46451  0 17:43 ?        00:00:00 /usr/local/app/apache/bin/httpd -k start
root      46537   1560  0 17:43 pts/0    00:00:00 grep --color=auto httpd

[root@wencheng ~]# ss -nutl    //查看80端口
Netid  State      Recv-Q Send-Q                               Local Address:Port                                              Peer Address:Port
udp    UNCONN     0      0                                        127.0.0.1:323                                                          *:*
udp    UNCONN     0      0                                              ::1:323                                                         :::*
tcp    LISTEN     0      128                                              *:22                                                           *:*
tcp    LISTEN     0      100                                      127.0.0.1:25                                                           *:*
tcp    LISTEN     0      128                                             :::80                                                          :::*
tcp    LISTEN     0      128                                             :::8081                                                        :::*
tcp    LISTEN     0      128                                             :::22                                                          :::*
tcp    LISTEN     0      100                                            ::1:25                                                          :::*

[root@wencheng ~]# cat /usr/local/app/apache/htdocs/index.html
<html><body><h1>It works!</h1></body></html>
[root@wencheng ~]# curl 127.0.0.1    //测试curl返回
<html><body><h1>It works!</h1></body></html>

[root@wencheng ~]# useradd -r -s /sbin/nologin apache
[root@wencheng ~]# chown -R apache:apache /usr/local/app/apache
[root@wencheng ~]# ls -ld /usr/local/app/apache/
drwxr-xr-x. 13 apache apache 152 Apr 28 14:49 /usr/local/app/apache/

 配置参数说明(可选)

[root@wencheng ~]# vim /etc/apache/httpd.conf
#服务的路径
ServerRoot "/usr/local/app/apache"

#监听端口
Listen 80
Listen 8081

#管理员的邮件(若服务出现了问题,会给管理员发邮件,前提是服务器能发出邮件)
ServerAdmin you@example.com

#作为httpd运行的用户/组
User apache
Group apache

#域名信息
ServerName 127.0.0.1:80
ServerName 127.0.0.1:8081

#apache发布的目录
DocumentRoot "/usr/local/app/apache/htdocs"

<IfModule dir_module>
   #默认访问文件
    DirectoryIndex index.html
</IfModule>

#错误日志
ErrorLog "logs/error_log"
#警告级别:info debug error warn
LogLevel warn

#启用 rewrite 引擎
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example.com:8888 [NC]
RewriteRule ^/(.*)$ http://www.baidu.com/$1 [L]
LoadModule rewrite_module modules/mod_rewrite.so

#引用多域名的配置
#NameVirtualHost *:8081
Include conf/extra/httpd-vhosts.conf

#导入mpm详细配置
Include conf/extra/httpd-mpm.conf
[root@wencheng ~]# vim /etc/apache/extra/httpd-vhosts.conf
#虚拟主机1监听端口号80
#NameVirtualHost *:80
<VirtualHost *:80>
  ServerAdmin webmaster@dummy-host.example.com
  #虚拟主机2的发布目录
  DocumentRoot "/usr/local/app/apache/htdocs/host1"
    ServerName dummy-host.example.com
    ServerAlias www.dummy-host.example.com
    ErrorLog "logs/dummy-host1.example.com-error_log"
    CustomLog "logs/dummy-host1.example.com-access_log" common
</VirtualHost>

#虚拟主机1监听端口号8081
#NameVirtualHost *:8081
<VirtualHost *:8081>
  ServerAdmin webmaster@dummy-host2.example.com
  #虚拟主机2的发布目录
    DocumentRoot "/usr/local/app/apache/htdocs/host2"
    ServerName dummy-host2.example.com
    ErrorLog "logs/dummy-host2.example.com-error_log"
    CustomLog "logs/dummy-host2.example.com-access_log" common
</VirtualHost>
mpm的参数设置可能不对,可自行百度进行相应设置
[root@wencheng apache]# vim extra/httpd-mpm.conf
#prefork MPM
<IfModule mpm_prefork_module>
    #启动的工作进程数  企业中一般配置8/10/16 不会太(默认初始化生成数量)
    StartServers            8
    #最多同时xx进程,一般与MaxClients相对应或者大于MaxClients
    MinSpareServers         10000
    #最小空闲进程
    MaxSpareServers         25
    #最大空闲进程
    MaxRequestWorkers       75
    #最大支持多少个客户端访问(最大工作进程,每秒处理数量)
    MaxClients              1000
    #每个进程处理的最大请求数(当达到设置的数量后该线程会被kil掉,0代表永生)
    MaxConnectionsPerChild  10000
</IfModule>
#worker
<IfModule mpm_worker_module>
    StartServers             8
#    MaxClients               4000
    MinSpareThreads          25
    MaxSpareThreads          75
    #每个进程有多少线程数
    ThreadsPerChild          25
    MaxRequestWorkers      400
    MaxConnectionsPerChild   10000
#</IfModule>

 步骤二:编译安装MySQL

 解压并编译安装(注:内存>8G)。

[root@wencheng ~]# wget http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.bz2
[root@wencheng ~]# tar xf boost_1_59_0.tar.gz -C /usr/local/app/mysql/boost
[root@wencheng ~]# tar xf mysql-5.7.31.tar.gz -C /usr/local/src/
[root@wencheng ~]# cd /usr/local/src/mysql-5.7.31

[root@wencheng mysql-5.7.31]# yum install -y cmake openssl-devel bison*

[root@wencheng mysql-5.7.31]# cmake . \
> -DCMAKE_INSTALL_PREFIX=/usr/local/app/mysql \
> -DMYSQL_DATADIR=/usr/local/app/mysql/data \
> -DSYSCONFDIR=/etc \
> -DDEFAULT_CHARSET=utf8mb4 \
> -DENABLED_LOCAL_INFILE=1 \
> -DWITH_BOOST=/usr/local/app/mysql/boost \
> -DEXTRA_CHARSETS=all
说明:
-DCMAKE_INSTALL_PREFIX:mysql安装目录
-DMYSQL_DATADIR:数据存放目录
-DSYSCONFDIR:my.cnf配置文件目录
-DEFAULT_CHARSET:数据库默认字符编码
-DENABLED_LOCAL_INFILE:允许从本文件导入数据
-DWITH_BOOST=/usr/local/app/mysql/boost: boost源码路径
-DEXTRA_CHARSETS:安装所有字符集
# 若每次编译出错了,需根据下面操作,然后再重新编译安装。
[root@wencheng mysql-5.7.31]# find / -iname CMakeCache.txt
[root@wencheng mysql-5.7.31]# make clean
[root@wencheng mysql-5.7.31]# rm -f CMakeCache.txt

至此编译完成。

如何在CentOS 7上搭建LAMP环境(使用YUM或编译)-LMLPHP

[root@wencheng mysql-5.7.31]# make -j 4 && make install

内存不足导致,安装时报错。

如何在CentOS 7上搭建LAMP环境(使用YUM或编译)-LMLPHP

 临时新增4G交换分区,编译完成后删除。

[root@wencheng mysql-5.7.31]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1982          63        1769           0         149        1753
Swap:          2047          51        1996

[root@wencheng mysql-5.7.31]# dd if=/dev/zero of=/swapfile bs=1k count=4096000
4096000+0 records in
4096000+0 records out
4194304000 bytes (4.2 GB) copied, 26.196 s, 160 MB/s
[root@wencheng mysql-5.7.31]# mkswap /swapfile
Setting up swapspace version 1, size = 4095996 KiB
no label, UUID=ae18f018-86b3-4ef8-ada0-f54f64efc980

[root@wencheng mysql-5.7.31]# swapon /swapfile
[root@wencheng mysql-5.7.31]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1982          65          65           1        1851        1716
Swap:          6047          50        5997

# 删除/swapfile操作
[root@wencheng mysql-5.7.31]# swapoff /swapfile
[root@wencheng mysql-5.7.31]# rm -rf /swapfile

配置MySQL。

[root@wencheng app]# useradd -r -s /sbin/nologin mysql
[root@wencheng app]# chown -R mysql:mysql /usr/local/app/mysql
[root@wencheng mysql]# ls -ld /usr/local/app/mysql
drwxr-xr-x 11 mysql mysql 179 Apr 30 12:23 /usr/local/app/mysql
[root@wencheng mysql]# mkdir -p /usr/local/app/mysql/data

 添加配置文件

[root@wencheng mysql]# vim /etc/my.cnf
[client] port = 3306 default-character-set = utf8 socket = /usr/local/app/mysql/mysql.sock [mysql] port = 3306 default-character-set = utf8 socket = /usr/local/app/mysql/mysql.sock [mysqld] user = mysql basedir = /usr/local/app/mysql datadir = /usr/local/app/mysql/data port = 3306 pid-file = /usr/local/app/mysql/mysql.pid socket = /usr/local/app/mysql/mysql.sock server-id = 1 character_set_server=utf8

初始化Mysql

[root@wencheng mysql]# ./bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/app/mysql --datadir=/usr/local/app/mysql/data

 如何在CentOS 7上搭建LAMP环境(使用YUM或编译)-LMLPHP

设置环境变量

[root@wencheng mysql]# echo 'PATH=/usr/local/app/mysql/bin:/usr/local/app/mysql/lib:$PATH' >> /etc/profile.d/mysqld.sh
[root@wencheng mysql]# source /etc/profile.d/mysqld.sh

生成Mysql服务启动脚本

# 生成启动脚本
[root@wencheng mysql]# cp support-files/mysql.server /etc/init.d/mysqld

# 启动服务和设置开机自启
[root@wencheng mysql]# /etc/init.d/mysqld start
Starting MySQL..Logging to '/usr/local/app/mysql/data/wencheng.err'.
................................................................................................... SUCCESS!

[root@wencheng mysql]# service mysqld status
 SUCCESS! MySQL running (80286)

[root@wencheng mysql]# chkconfig --add mysqld
[root@wencheng mysql]# chkconfig mysqld on

[root@wencheng mysql]# ss -tnl | grep 3306    //查看mysql 3306端口是否存在
LISTEN     0      80          :::3306                    :::*

 登录Mysql数据库

[root@wencheng mysql]# 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.31 Source distribution

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>

# 查看Mysql版本
mysql> select version();    
+-----------+
| version() |
+-----------+
| 5.7.31    |
+-----------+
1 row in set (0.00 sec)

 步骤三:编译安装PHP

[root@wencheng ~]# tar -xf php-7.0.33.tar.gz -C /usr/local/src
[root@wencheng ~]# cd /usr/local/src/php-7.0.33/
[root@wencheng php-7.0.33]# yum install -y libxml2-devel libcurl-devel libjpeg-turbo-devel libpng-devel
[root@wencheng php-7.0.33]# ./configure --prefix=/usr/local/app/php --enable-ftp --enable-zip --enable-fpm \
--with-pdo-mysql --with-jpeg-dir --with-png-dir --with-gd --with-curl --enable-mbstring --enable-bcmath --enable-sockets
参数说明:
--prefix=/usr/local/app/php  php安装的根目录
--enable-ftp   开启ftp
--enable-zip   打开对zip的支持
--enable-fpm  开启fpm
--with-pdo-mysql   php依赖mysql库
--with-jpeg-dir   指定jpeg安装目录yum安装过后不用再次指定会自动找到
--with-png-dir   指定png安装目录yum安装过后不用再次指定会自动找到
--with-gd     gd库
--with-curl   允许curl扩展
--enable-mbstring   开启多字节字符串函数
--enable-bcmath   打开图片大小调整,用到zabbix监控的时候用到了这个模块
--enable-sockets  开启套节字

至此编译安装完成。

 如何在CentOS 7上搭建LAMP环境(使用YUM或编译)-LMLPHP

 配置PHP

[root@wencheng ~]# cp /usr/local/src/php-7.0.33/php.ini-production /etc/php7/php.ini
[root@wencheng ~]# vim /etc/php7/php.ini   //修改时区
date.timezone = PRC   

# 添加 php-fpm 管理相关的配置文件到系统配置目录 /etc/init.d
[root@wencheng php-7.0.33]# pwd
/usr/local/src/php-7.0.33
[root@wencheng php-7.0.33]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php7.0-fpm

# 添加 php-fpm 配置文件
[root@wencheng etc]# pwd
/usr/local/app/php/etc
[root@wencheng etc]# cp php-fpm.conf.default php-fpm.conf

# 添加 www.conf 配置文件
[root@wencheng php-fpm.d]# pwd
/usr/local/app/php/etc/php-fpm.d
[root@wencheng php-fpm.d]# cp www.conf.default www.conf

# 创建软连接
[root@wencheng ~]# ln -s /usr/local/app/php/bin/php /usr/local/bin/php
[root@wencheng ~]# ln -s /usr/local/app/php/sbin/php-fpm /usr/local/bin/php-fpm
[root@wencheng ~]# ls -l /usr/local/bin/
total 0
lrwxrwxrwx 1 root root 26 May  8 11:41 php -> /usr/local/app/php/bin/php
lrwxrwxrwx 1 root root 31 May  8 11:41 php-fpm -> /usr/local/app/php/sbin/php-fpm

# 测试PHP的配置文件是否无误
[root@wencheng ~]# php-fpm -t
[08-May-2021 11:56:55] NOTICE: configuration file /usr/local/app/php/etc/php-fpm.conf test is successful

# 查看php版本
[root@wencheng ~]# php -v
PHP 7.0.33 (cli) (built: May  8 2021 11:10:06) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies

# 设置php开机启动
[root@wencheng ~]# chmod +x /etc/init.d//php7.0-fpm
[root@wencheng ~]# chkconfig --add php7.0-fpm
[root@wencheng ~]# chkconfig php7.0-fpm on

[root@wencheng ~]# systemctl start php-fpm
[root@wencheng ~]# ps -ef | grep php
root      49126      1  0 12:02 ?        00:00:00 php-fpm: master process (/usr/local/app/php/etc/php-fpm.conf)
nobody    49127  49126  0 12:02 ?        00:00:00 php-fpm: pool www
nobody    49128  49126  0 12:02 ?        00:00:00 php-fpm: pool www
root      49155  76797  0 12:02 pts/1    00:00:00 grep --color=auto php
[root@wencheng ~]# ss -ntl | grep 9000
LISTEN     0      128    127.0.0.1:9000                     *:*

搭配其他网站。

例一:安装phpMyAdmin:4.0.10.20

[root@wencheng ~]# mkdir -p /var/www/html/phpmyadmin    //创建数据存放目录

[root@wencheng ~]# yum install -y wget unzip

[root@wencheng ~]# wget https://files.phpmyadmin.net/phpMyAdmin/4.0.10.20/phpMyAdmin-4.0.10.20-all-languages.zip
[root@wencheng ~]# ls -l phpMyAdmin-4.0.10.20-all-languages.zip
-rw-r--r--. 1 root root 7457007 Mar 29  2017 phpMyAdmin-4.0.10.20-all-languages.zip

[root@wencheng ~]# unzip phpMyAdmin-4.0.10.20-all-languages.zip    //解压
[root@wencheng ~]# mv phpMyAdmin-4.0.10.20-all-languages/* /var/www/html/phpmyadmin    //移动至phpMyAdmin数据存放目录  

 查看安装结果。

  • 在本地机器浏览器地址栏,输入hppt://IP/phpmyadmin ,若返回页面如下,即phpMyAdmin安装成功。

如何在CentOS 7上搭建LAMP环境(使用YUM或编译)-LMLPHP

  •  输入MySQL的用户名和密码。

如何在CentOS 7上搭建LAMP环境(使用YUM或编译)-LMLPHP

  •  若返回如下页面,即MySQL连接成功。

 如何在CentOS 7上搭建LAMP环境(使用YUM或编译)-LMLPHP

 
 
 
06-18 15:30