如何在CentOS 7上搭建LAMP環境(使用YUM或編譯)

上古南城發表於2021-06-18

什麼是LAMP?

LAMP是Linux,Apache,MySQL和PHP的縮寫。 它是一堆應用程式的堆疊,它們在Web伺服器上一起工作以託管網站。 話雖如此,每個程式都有不同的目的:

在LAMP中, Linux 用作處理計算機上所有命令的伺服器作業系統。 在本文中,將使用CentOS 7發行版。

  • Apache 是一種Web伺服器軟體,可管理HTTP請求以傳遞您的網站內容。
  • MySQL 是關聯式資料庫管理系統(RDBMS),其功能是在伺服器上維護使用者資料。
  • PHP 是用於伺服器端通訊的指令碼語言。

版本資訊:

在示例步驟中使用了以下版本的軟體。操作時,請您以實際軟體版本為準。

  • 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服務啟動成功。

 步驟二: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安裝成功。

 第二部分:編譯安裝

環境準備

[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:安裝所有字符集

 注:更多預編譯配置引數請參考mysql官方文件說明:https://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html#cmake-general-options

# 若每次編譯出錯了,需根據下面操作,然後再重新編譯安裝。
[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

至此編譯完成。

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

記憶體不足導致,安裝時報錯。

 臨時新增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

 

設定環境變數

[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  開啟套節字

至此編譯安裝完成。

 

 配置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安裝成功。

  •  輸入MySQL的使用者名稱和密碼。

  •  若返回如下頁面,即MySQL連線成功。

 

 
 
 

相關文章