LAMP技术

lamp lamp平台搭建 实验环境 主机名 IP地址 服务 系统 xiefei.com 192.168.100.60 lamp reh-hat7 安装apache 安装开发工具 [root@xiefei ~]# yum groups mark install 'Development Tools' 创建apache服务的用户和组 [root@xiefei

lamp

lamp平台搭建

  • 实验环境

主机名IP地址服务系统
xiefei.com192.168.100.60lampreh-hat7

安装apache

  • 安装开发工具

[root@xiefei ~]#  yum groups mark install 'Development Tools'

  • 创建apache服务的用户和组

[root@xiefei ~]# groupadd -r apache
[root@xiefei ~]# useradd -r -M -s /sbin/nologin -g apache apache

  • 安装依赖包

yum -y install openssl-devel pcre-devel expat-devel libtool

  • 下载和安装arp和arp-util

[root@xiefei ~]# cd /usr/src/
[root@xiefei src]# wget http://mirrors.shu.edu.cn/apache/apr/apr-1.6.5.tar.bz2
[root@xiefei src]# wget http://mirrors.shu.edu.cn/apache//apr/apr-util-1.6.1.tar.bz2
[root@xiefei src]# tar xf apr-1.6.5.tar.bz2
[root@xiefei src]# tar xf apr-util-1.6.1.tar.bz2
[root@xiefei src]# cd apr-1.6.5/
[root@xiefei apr-1.6.5]# vim configure
    cfgfile=${ofile}T
    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
#    $RM "$cfgfile"    //此行加注释
[root@xiefei apr-1.6.5]# ./configure --prefix=/usr/local/apr
[root@xiefei apr-1.6.5]# make && make install
[root@xiefei apr-1.6.5]# cd /usr/src/apr-util-1.6.1/
[root@xiefei apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util -with-apr=/usr/local/apr
[root@xiefei apr-util-1.6.1]# make && make install

  • 编译安装apache

[root@xiefei src]# wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.4.34.tar.bz2
[root@xiefei src]# tar xf httpd-2.4.34.tar.bz2
[root@xiefei src]# cd httpd-2.4.34/
[root@xiefei 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
 [root@xiefei httpd-2.4.34]# make && make install

  • 添加path路径

[root@xiefei bin]# echo 'PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[root@xiefei bin]# source /etc/profile.d/httpd.sh
[root@xiefei bin]# which apachectl 
/usr/local/apache/bin/apachectl

  • 在主配置文件取消ServerName的注释

[root@xiefei bin]# sed -i '/#ServerName/s/#//g' /etc/httpd24/httpd.conf

  • 关闭防火墙和selinux,启动apache

[root@xiefei bin]# systemctl stop firewalld
[root@xiefei bin]# setenforce 0
[root@xiefei bin]# apachectl start

  • 检测apache是否启动在这里插入图片描述

安装mysql

  • 安装依赖包

[root@xiefei bin]# yum -y install ncurses-devel openssl-devel openssl  cmake mariadb-devel

  • 创建mysql用户和组

[root@xiefei ~]# groupadd -r -g 306 mysql
[root@xiefei ~]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql

  • 下载二进制mysql包

[root@xiefei ~]# cd /usr/src/
[root@xiefei src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
[root@xiefei src]# tar xf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz -C /usr/local/

  • 做软连接和添加至环境变量

[root@xiefei local]# ln -s mysql-5.7.23-linux-glibc2.12-x86_64/ mysql
[root@xiefei local]# chown -R mysql.mysql /usr/local/mysql
[root@xiefei local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@xiefei local]# source /etc/profile.d/mysql.sh
[root@xiefei local]# which mysqld
/usr/local/mysql/bin/mysqld

  • 建立数据存放目录

[root@xiefei bin]# mkdir /opt/data -p
[root@xiefei bin]# chown -R mysql.mysql /opt/data/
[root@xiefei bin]# ll /opt/data/ -d
drwxr-xr-x 2 mysql mysql 6 Sep 26 08:34 /opt/data/

  • 初始化数据库

[root@xiefei local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/

//初始化数据库出现如下失误

[root@xiefei local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
/usr/local/mysql/bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

解决方法

[root@xiefei local]# yum -y install libaio  //安装即可

  • 将随机密码保存下来

[root@xiefei local]# echo '&gH?,y2Ma3hV' > /root/a

  • 配置mysql

[root@xiefei local]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@xiefei local]# ldconfig -v

  • 生成配置文件

[root@xiefei local]#  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
ser = mysql
skip-name-resolve
EOF

  • 配置服务启动脚本

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

  • 启动Mysql

[root@xiefei local]# service mysqld start
Starting MySQL.Logging to '/opt/data/xiefei.com.err'.
 SUCCESS! 
[root@xiefei local]# ss -anlt
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                    :::*  

  • 登陆mysql修改密码

[root@xiefei local]# mysql -uroot -p'&gH?,y2Ma3hV'
mysql> set password=password('123.com');
Query OK, 0 rows affected, 1 warning (0.00 sec)

安装php

  • 安装依赖包

[root@xiefei local]# 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-develreadline readline-devel libxslt libxslt-devel mhash mhash-deve

  • 下载php

[root@xiefei local]# cd /usr/src/
[root@xiefei src]#  wget http://cn.php.net/distributions/php-7.2.8.tar.xz

  • 编译安装

[root@xiefei src]# tar xf php-7.2.8.tar.xz
[root@xiefei src]# cd php-7.2.8/
[root@xiefei 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
[root@xiefei php-7.2.8]# make  && make install

  • 安装完成后配置

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

  • 配置php-fam

[root@xiefei php-7.2.8]# cp php.ini-production /etc/php.ini
[root@xiefei php-7.2.8]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@xiefei php-7.2.8]# chmod +x /etc/init.d/php-fpm
[root@xiefei php-7.2.8]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf 
[root@xiefei 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的配置文件(/usr/local/php7/etc/php-fpm.conf)
    配置fpm相关选项为你所需要的值

[root@xiefei php-7.2.8]# 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   //最大空闲进程数
[root@xiefei php-7.2.8]# tail  /usr/local/php7/etc/php-fpm.conf
; file.
; Relative path can also be used. They will be prefixed by:
;  - the global prefix if it's been set (-p argument)
;  - /usr/local/php7 otherwise
include=/usr/local/php7/etc/php-fpm.d/*.conf

pm.max_children = 50
pm.start_servers = 5 
pm.min_spare_servers = 2 
pm.max_spare_servers = 8

  • 启动php服务

[root@xiefei php-7.2.8]# service php-fpm start
Starting php-fpm  done
\\默认情况下,fpm监听在127.0.0.1的9000端口
[root@xiefei php-7.2.8]# ss -anlt
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                    :::* 
[root@xiefei php-7.2.8]# ps -ef | grep php
root      67777      1  0 09:32 ?        00:00:00 php-fpm: master process (/usr/local/php7/etc/php-fpm.conf)
nobody    67778  67777  0 09:32 ?        00:00:00 php-fpm: pool www
nobody    67779  67777  0 09:32 ?        00:00:00 php-fpm: pool www
nobody    67780  67777  0 09:32 ?        00:00:00 php-fpm: pool www
nobody    67781  67777  0 09:32 ?        00:00:00 php-fpm: pool www
nobody    67782  67777  0 09:32 ?        00:00:00 php-fpm: pool www
root      67785   2020  0 09:32 pts/2    00:00:00 grep --color=auto php

配置虚拟主机

  • 启动代理模块

[root@xiefei ~]# sed -i '/proxy_module/s/#//g' /etc/httpd24/httpd.conf
[root@xiefei ~]# sed -i '/proxy_fcgi_module/s/#//g' /etc/httpd24/httpd.conf

在需要使用fcgi的虚拟主机中添加类似如下两行

    ProxyRequests OFF
    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/xiefei/$1

  • 创建虚拟主机目录并生成php测试页面

[root@xiefei ~]# sed -i '/proxy_module/s/#//g' /etc/httpd24/httpd.conf 
[root@xiefei ~]# sed -i '/proxy_fcgi_module/s/#//g' /etc/httpd24/httpd.conf
[root@xiefei ~]# mkdir /usr/local/apache/htdocs/xiefei
[root@xiefei ~]# cat > /usr/local/apache/htdocs/xiefei/index.php << EOF
> <?php
>     phpinfo();
> ?>
> EOF
[root@xiefei ~]# chown -R apache.apache /usr/local/apache/htdocs/

  • 在配置文件最后加入以下内容

[root@xiefei ~]# vim /etc/httpd24/httpd.conf
<VirtualHost *:80>
    DocumentRoot "/usr/local/apache/htdocs/xiefei"
    ServerName www.xiefei.com
    ProxyRequests OFF
    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/xiefei/$1
    <Directory "/usr/local/apache/htdocs/xiefei">
        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    //添加此行
    [root@xiefei ~]# sed -i '/    DirectoryIndex/s/index.html/index.php index.html/g' /etc/httpd24/httpd.conf

  • 重启apache服务

[root@xiefei ~]# apachectl restart
[root@xiefei ~]# ss -anlt
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                    :::*

  • 修改 host 文件,添加域名与IP的映射
    在浏览器上使用域名访问,若看到以下界面则表示lamp架构搭建成功

192.168.100.60 www.xiefei.com   //修改host文件

在这里插入图片描述

知秋君
上一篇 2024-08-29 11:48
下一篇 2024-08-29 11:12

相关推荐