1. 程式人生 > >lamp平臺構建(Apache、MySQL、PHP)

lamp平臺構建(Apache、MySQL、PHP)

環境說明:

系統平臺 IP 需要安裝的服務
redhat 192.168.102.128 httpd-2.4
mysql-5.7
php
php-mysql

lamp平臺軟體安裝次序:http–>mysql–>php
注意:PHP要求httpd使用prefork MPM

1. 安裝httpd

注意:先將網路源配好。

1.1 安裝開發工具包

[[email protected] ~]# yum groups mark install 'Development Tools'
已載入外掛:product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
沒有安裝組資訊檔案
Maybe run: yum groups mark convert (see man yum)
base                                             | 3.6 kB     00:00     
centosplus                                       | 3.4 kB     00:00     
extras                                           | 3.4 kB     00:00     
updates                                          | 3.4 kB     00:00     
(1/5): centosplus/x86_64/primary_db                | 2.9 MB   00:04     
(2/5): base/x86_64/primary_db                      | 5.9 MB   00:05     
(3/5): base/x86_64/group_gz                        | 166 kB   00:05     
(4/5): extras/x86_64/primary_db                    | 187 kB   00:05     
(5/5): updates/x86_64/primary_db                   | 5.2 MB   00:07     
Marked install: Development Tools

1.2 建立apache服務的使用者和組

[[email protected] ~]# groupadd -r apache
[[email protected] ~]# useradd -r -M -s /sbin/nologin -g apache apache

1.3安裝依賴包

[[email protected] ~]# yum -y install openssl-devel pcre-devel expat-devel libtool
#安裝過程略···

1.4下載和安裝apr以及apr-util

[[email protected]
~]# cd /usr/src/ [[email protected] src]# wget http://mirrors.shu.edu.cn/apache//apr/apr-1.6.5.tar.bz2 #過程略··· [[email protected] src]# wget http://mirrors.shu.edu.cn/apache//apr/apr-util-1.6.1.tar.bz2 #過程略··· [[email protected] src]# ls apr-1.6.5.tar.bz2 apr-util-1.6.1.tar.bz2 debug kernels [[email protected]
src]# yum -y install bzip2 //安裝bzip2 #解壓兩個包 [[email protected] src]# tar xf apr-1.6.5.tar.bz2 [[email protected] src]# tar xf apr-util-1.6.1.tar.bz2 [[email protected] src]# ls apr-1.6.5 apr-util-1.6.1 debug apr-1.6.5.tar.bz2 apr-util-1.6.1.tar.bz2 kernels [[email protected] src]# cd apr-1.6.5/ [[email protected] apr-1.6.5]# vim configure cfgfile=${ofile}T trap "$RM \"$cfgfile\"; exit 1" 1 2 15 # $RM "$cfgfile" //加上註釋或者刪掉 [[email protected] apr-1.6.5]# ./configure --prefix=/usr/local/apr #配置過程略··· [[email protected] apr-1.6.5]# make && make install #編譯安裝過程略··· [[email protected] apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr #配置過程略··· [[email protected] apr-util-1.6.1]# make && make install 編譯安裝過程略···

1.5編譯安裝httpd

[[email protected] ~]#  wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.4.34.tar.bz2                //下載軟體包
[[email protected] ~]# ls
anaconda-ks.cfg  httpd-2.4.34.tar.bz2
[[email protected] ~]# tar xf httpd-2.4.34.tar.bz2 
[[email protected] ~]# ls
anaconda-ks.cfg  httpd-2.4.34  httpd-2.4.34.tar.bz2
[[email protected] ~]# cd httpd-2.4.34/
[[email protected] httpd-2.4.34]# ./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
#配置過程略···
[[email protected] httpd-2.4.34]# make && make install
#編譯安裝過程略···
//安裝後配置
[[email protected] httpd-2.4.34]# vim /etc/profile.d/httpd.sh
export PATH=/usr/local/apache/bin:$PATH
[[email protected] httpd-2.4.34]# source /etc/profile.d/httpd.sh 
[[email protected] httpd-2.4.34]# ln -s /usr/local/apache/include/ /usr/include/httpd
[[email protected] httpd-2.4.34]# echo 'MANPATH /usr/local/apache/man' >> /etc/man.config
[[email protected] httpd-2.4.34]# vim /etc/httpd24/httpd.conf
# If your host doesn't have a registered DNS name, enter its IP address here.
#
ServerName www.example.com:80              //去掉註釋

#
# Deny access to the entirety of your server's filesystem. You must
//啟動apache
[[email protected] httpd-2.4.34]# apachectl start
[[email protected] httpd-2.4.34]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128      *:22                   *:*                  
LISTEN     0      100    127.0.0.1:25                   *:*                  
LISTEN     0      128     :::80                  :::*                  
LISTEN     0      128     :::22                  :::*                  
LISTEN     0      100    ::1:25                  :::*

2.安裝MySQL

2.1 安裝依賴包

[[email protected] httpd-2.4.34]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel

2.2建立使用者和組

[[email protected] ~]# groupadd -r -g 306 mysql
[[email protected] ~]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql

2.3下載二級制格式的mysql軟體包並解壓

[[email protected] ~]# cd /usr/src/
[[email protected] src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz                //下載二進位制包
[[email protected] src]# ls
apr-1.6.5               debug
apr-1.6.5.tar.bz2       kernels
apr-util-1.6.1          mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
apr-util-1.6.1.tar.bz2
//解壓軟體到/usr/local/
[[email protected] src]# tar xf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[[email protected] src]# ls /usr/local/
apache    bin    include  libexec                              share
apr       etc    lib      mysql-5.7.23-linux-glibc2.12-x86_64  src
apr-util  games  lib64    sbin

2.4修改目錄/usr/local/mysql的屬主屬組

[[email protected] local]# ln -sv mysql-5.7.23-linux-glibc2.12-x86_64/ mysql
[[email protected] local]# chown -R mysql.mysql /usr/local/mysql
[[email protected] local]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 36 9月  17 12:31 /usr/local/mysql -> mysql-5.7.23-linux-glibc2.12-x86_64/

2.5新增環境變數

[[email protected] ~]# ls /usr/local/mysql
bin  COPYING  docs  include  lib  man  README  share  support-files
[[email protected] ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[[email protected] ~]# . /etc/pro
profile    profile.d/ protocols  
[[email protected] ~]# . /etc/profile.d/mysql.sh 
[[email protected] ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/apache/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

2.6建立資料存放目錄

[[email protected] ~]# mkdir /opt/data
[[email protected] ~]# chown -R mysql.mysql /opt/data/
[[email protected] ~]# ll /opt/
總用量 0
drwxr-xr-x. 2 mysql mysql 6 9月  17 13:50 data

2.7初始化資料庫

[[email protected] ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2018-09-17T05:52:56.050088Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-09-17T05:52:58.186950Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-09-17T05:52:58.375927Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-09-17T05:52:58.948910Z 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: ee5aa321-ba3d-11e8-92d4-000c2938489e.
2018-09-17T05:52:58.956148Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-09-17T05:52:58.958181Z 1 [Note] A temporary password is generated for [email protected]: 2r7pygJH+sOp
[[email protected] ~]# echo '2r7pygJH+sOp' > pass

2.8配置MySQL

[[email protected] ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
"/usr/local/include/mysql" -> "/usr/local/mysql/include/"
[[email protected] ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[[email protected] ~]# ldconfig -v
略···

2.9生成配置檔案

[[email protected] ~]# 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

2.10配置服務

[[email protected] ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[[email protected] ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld 
[[email protected] ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld

2.11啟動MySQL

[[email protected] local]# service mysqld start
Starting MySQL.Logging to '/opt/data/server.err'.
 SUCCESS!
 [[email protected] local]# ps -ef|grep mysql
root      44994      1  0 14:39 pts/0    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql     45172  44994  0 14:39 pts/0    00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=server.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root      45203  44547  0 14:41 pts/0    00:00:00 grep --color=auto mysql
[[email protected] local]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128         *:22                      *:*                  
LISTEN     0      100    127.0.0.1:25                      *:*                  
LISTEN     0      128        :::80                     :::*                  
LISTEN     0      128        :::22                     :::*                  
LISTEN     0      100       ::1:25                     :::*                  
LISTEN     0      80         :::3306                   :::*

2.12修改密碼

[[email protected] ~]# cat pass
2r7pygJH+sOp
[[email protected] ~]# 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.23

Copyright (c) 2000, 2018, 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> set password = password('12345678');
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> quit
Bye

3安裝PHP

要有網路源,前面已經提過。

3.1安裝依賴包

[[email protected] ~]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel
#安裝過程略···

3.2下載安裝PHP

[[email protected] ~]# cd /usr/src/
[[email protected] src]# wget http://cn.php.net/distributions/php-7.2.8.tar.xz
下載過程略···
//編譯安裝PHP
[[email protected] src]# tar xf php-7.2.8.tar.xz
[[email protected] src]# cd php-7.2.8/
[[email protected] php-7.2.8]# ./configure --prefix=/usr/local/php7 \
> --with-curl \
> --with-freetype-dir \
> --with-gd \
> --with-gettext \
> --with-iconv-dir \
> --with-kerberos \
> --with-libdir=lib64 \
> --with-libxml-dir=/usr \
> --with-mysqli=/usr/local/mysql/bin/mysql_config \
> --with-openssl \
> --with-pcre-regex \
> --with-pdo-mysql \
> --with-pdo-sqlite \
> --with-pear \
> --with-jpeg-dir \
> --with-png-dir \
> --with-xmlrpc \
> --with-xsl \
> --with-zlib \
> --with-config-file-path=/etc \
> --with-config-file-scan-dir=/etc/php.d \
> --with-bz2 \
> --enable-fpm \
> --enable-bcmath \
> --enable-libxml \
> --enable-inline-optimization \
> --enable-mbregex \
> --enable-mbstring \
> --enable-opcache \
> --enable-pcntl \
> --enable-shmop \
> --enable-soap \
> --enable-sockets \
> --enable-sysvsem \
> --enable-xml \
> --enable-zip
[[email protected] php-7.2.8]# make -j $(cat /proc/cpuinfo |grep processor|wc -l)
編譯過程略···
[[email protected] php-7.2.8]# make install
安裝過程略···

3.3安裝後配置

[[email protected] php-7.2.8]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[[email protected] php-7.2.8]# source /etc/profile.d/php7.sh
[[email protected] php-7.2.8]# which php
/usr/local/php7/bin/php
[r[email protected] php-7.2.8]# php -v
PHP 7.2.8 (cli) (built: Sep 17 2018 15:02:30) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

3.4配置php-fpm

[[email protected] php-7.2.8]# cp php.ini-production /etc/php.ini
[[email protected] php-7.2.8]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[[email protected] php-7.2.8]# chmod +x /etc/rc.d/init.d/php-fpm
[[email protected] php-7.2.8]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[[email protected] php-7.2.8]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
//編輯php-fpm的配置檔案、新增如下內容
[[email protected] php-7.2.8]# vim /usr/local/php7/etc/php-fpm.conf
·······
·······
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8

3.5php-fpm

[[email protected] php-7.2.8]# service php-fpm start
Starting php-fpm  done
[[email protected] php-7.2.8]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128         *:22                      *:*                  
LISTEN     0      100    127.0.0.1:25                      *:*                  
LISTEN     0      128    127.0.0.1:9000                    *:*                  
LISTEN     0      128        :::80                     :::*                  
LISTEN     0      128        :::22                     :::*                  
LISTEN     0      100       ::1:25                     :::*                  
LISTEN     0      80         :::3306                   :::*                  
[[email protected] php-7.2.8]# ps -ef | grep php
root      66511      1  0 15:23 ?        00:00:00 php-fpm: master process (/usr/local/php7/etc/php-fpm.conf)
nobody    66512  66511  0 15:23 ?        00:00:00 php-fpm: pool www
nobody    66513  66511  0 15:23 ?        00:00:00 php-fpm: pool www
nobody    66514  66511  0 15:23 ?        00:00:00 php-fpm: pool www
nobody    66515  66511  0 15:23 ?        00:00:00 php-fpm: pool www
nobody    66516  66511  0 15:23 ?        00:00:00 php-fpm: pool www
root      66519  44547  0 15:24 pts/0    00:00:00 grep --color=auto php

4配置apache

4.1啟動代理模組

[[email protected] php-7.2.8]# sed -i '/proxy_module/s/#//g' /etc/httpd24/httpd.conf
[[email protected] php-7.2.8]# sed -i '/proxy_fcgi_module/s/#//g' /etc/httpd24/httpd.conf

4.2配置虛擬機器

[[email protected] php-7.2.8]# mkdir /usr/local/apache/htdocs/lslsls.com
[[email protected] php-7.2.8]# cat > /usr/local/apache/htdocs/lslsls.com/index.php <<EOF
> <?php
>  phpinfo();
> ?>
> EOF
[[email protected] php-7.2.8]# chown -R apache.apache /usr/local/apache/htdocs/
[[email protected] php-7.2.8]# ll /usr/local/apache/htdocs/ -d
drwxr-xr-x. 3 apache apache 42 9月  17 18:55 /usr/local/apache/htdocs/
[email protected] php-7.2.8]# vim /etc/httpd24/httpd.conf
//在最後新增以下內容
<VirtualHost *:80>
 DocumentRoot "/usr/local/apache/htdocs/lslsls.com"
 ServerName www.lslsls.com
 ProxyRequests Off
 ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/lslsls.com/$1
 <Directory "/usr/local/apache/htdocs/lslsls.com">
 Options none
 AllowOverride none
 Require all granted
 </Directory>
</VirtualHost>
//搜尋AddType,新增以下內容
# 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        //新增此行
    [[email protected] php-7.2.8]# sed -i '/ DirectoryIndex/s/index.html/index.php index.html/g' /etc/httpd24/httpd.con

4.3重啟Apache服務

[[email protected] ~]# apachectl stop
[[email protected] ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128         *:22                      *:*                  
LISTEN     0      100    127.0.0.1:25                      *:*                  
LISTEN     0      128    127.0.0.1:9000                    *:*                  
LISTEN     0      128        :::22                     :::*                  
LISTEN     0      100       ::1:25                     :::*                  
LISTEN     0      80         :::3306                   :::*                  
[[email protected] ~]# apachectl start
[[email protected] ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128         *:22                      *:*                  
LISTEN     0      100    127.0.0.1:25                      *:*                  
LISTEN     0      128    127.0.0.1:9000                    *:*                  
LISTEN     0      128        :::80                     :::*                  
LISTEN     0      128        :::22                     :::*                  
LISTEN     0      100       ::1:25                     :::*                  
LISTEN     0      80         :::3306                   :::*

5驗證

5.1修改hosts檔案,新增域名和IP的對映

在這裡插入圖片描述

5.2用遊覽器登入

在這裡插入圖片描述