lamp平台搭建
- 实验环境
主机名 | IP地址 | 服务 | 系统 |
---|---|---|---|
haha.com | 192.168.118.100 | lamp | rhel7 |
安装apache
- 安装开发工具
[root@haha ~]# yum groups mark install 'Development Tools'
- 创建apache服务的用户和组
[root@haha ~]# groupadd -r apache
[root@haha ~]# useradd -r -M -s /sbin/nologin -g apache apache
- 安装依赖包
[root@haha ~]# yum groups mark install 'Development Tools'
- 下载和安装arp和arp-util
[root@haha ~]# cd /usr/src/
[root@haha src]# wget http://mirrors.shu.edu.cn/apache/apr/apr-1.6.5.tar.bz2
[root@haha src]# wget http://mirrors.shu.edu.cn/apache//apr/apr-util-1.6.1.tar.bz2
[root@haha src]# tar xf apr-1.6.5.tar.bz2
[root@haha src]# tar xf apr-util-1.6.1.tar.bz2
[root@haha src]# cd apr-1.6.5/
[root@haha apr-1.6.5]# vim configure
cfgfile=${ofile}T
trap "$RM \"$cfgfile\"; exit 1" 1 2 15
# $RM "$cfgfile" //此行加注释
[root@haha apr-1.6.5]# ./configure --prefix=/usr/local/apr
[root@haha apr-1.6.5]# make && make install
[root@haha apr-1.6.5]# cd /usr/src/apr-util-1.6.1/
[root@haha apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util -with-apr=/usr/local/apr
[root@haha apr-util-1.6.1]# make && make install
- 编译安装apache
[root@haha src]# wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.4.34.tar.bz2
[root@haha src]# tar xf httpd-2.4.34.tar.bz2
[root@haha src]# cd httpd-2.4.34/
[root@haha 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@haha httpd-2.4.34]# make && make install
- 添加path路径
[root@haha bin]# echo 'PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[root@haha bin]# source /etc/profile.d/httpd.sh
[root@haha bin]# which apachectl
/usr/local/apache/bin/apachectl
- 在主配置文件取消ServerName的注释
[root@haha bin]# sed -i '/#ServerName/s/#//g' /etc/httpd24/httpd.conf
- 关闭防火墙和selinux,启动apache
[root@haha bin]# systemctl stop firewalld
[root@haha bin]# setenforce 0
[root@haha bin]# apachectl start
安装mysql
- 安装依赖包
[root@haha bin]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
- 创建mysql用户和组
[root@haha ~]# groupadd -r -g 306 mysql
[root@haha ~]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql
- 下载二进制mysql包
[root@haha ~]# cd /usr/src/
[root@haha src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
[root@haha src]# tar xf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
- 做软连接和添加至环境变量
[root@haha local]# ln -s mysql-5.7.23-linux-glibc2.12-x86_64/ mysql
[root@haha local]# chown -R mysql.mysql /usr/local/mysql
[root@haha local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@haha local]# source /etc/profile.d/mysql.sh
[root@haha local]# which mysqld
/usr/local/mysql/bin/mysqld
- 建立数据存放目录
[root@haha bin]# mkdir /opt/data -p
[root@haha bin]# chown -R mysql.mysql /opt/data/
[root@haha bin]# ll /opt/data/ -d
drwxr-xr-x 2 mysql mysql 6 Sep 26 08:34 /opt/data/
- 初始化数据库
[root@haha local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
- 将随机密码保存下来
[root@haha local]# echo '&gH?,y2Ma3hV' > /root/a
- 配置mysql
[root@haha local]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@haha local]# ldconfig -v
- 生成配置文件
[root@haha 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
user = mysql
skip-name-resolve
EOF
- 配置服务启动脚本
[root@haha local]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@haha local]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@haha local]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
- 启动Mysql
[root@haha local]# service mysqld start
Starting MySQL.Logging to '/opt/data/haha.com.err'.
SUCCESS!
[root@haha 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@haha local]# mysql -uroot -p'&gH?,y2Ma3hV'
mysql> set password=password('123.com');
Query OK, 0 rows affected, 1 warning (0.00 sec)
安装php
- 安装依赖包
[root@haha 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@haha local]# cd /usr/src/
[root@haha src]# wget http://cn.php.net/distributions/php-7.2.8.tar.xz
- 编译安装
[root@haha src]# tar xf php-7.2.8.tar.xz
[root@haha src]# cd php-7.2.8/
[root@haha 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@haha php-7.2.8]# make && make install
- 安装完成后配置
[root@haha bin]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@haha bin]# source /etc/profile.d/php7.sh
[root@haha bin]# which php
/usr/local/php7/bin/php
[root@haha 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@haha php-7.2.8]# cp php.ini-production /etc/php.ini
[root@haha php-7.2.8]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@haha php-7.2.8]# chmod +x /etc/init.d/php-fpm
[root@haha php-7.2.8]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[root@haha 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@haha 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@haha 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@haha php-7.2.8]# service php-fpm start
Starting php-fpm done
\\默认情况下,fpm监听在127.0.0.1的9000端口
[root@haha 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@haha 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@haha ~]# sed -i '/proxy_module/s/#//g' /etc/httpd24/httpd.conf
[root@haha ~]# 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/haha/$1
- 创建虚拟主机目录并生成php测试页面
[root@haha ~]# sed -i '/proxy_module/s/#//g' /etc/httpd24/httpd.conf
[root@haha ~]# sed -i '/proxy_fcgi_module/s/#//g' /etc/httpd24/httpd.conf
[root@haha ~]# mkdir /usr/local/apache/htdocs/haha
[root@haha ~]# cat > /usr/local/apache/htdocs/haha/index.php << EOF
> <?php
> phpinfo();
> ?>
> EOF
[root@haha ~]# chown -R apache.apache /usr/local/apache/htdocs/
- 在配置文件最后加入以下内容
[root@haha ~]# vim /etc/httpd24/httpd.conf
<VirtualHost *:80>
DocumentRoot "/usr/local/apache/htdocs/haha"
ServerName www.haha.com
ProxyRequests OFF
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/haha/$1
<Directory "/usr/local/apache/htdocs/haha">
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@haha ~]# sed -i '/ DirectoryIndex/s/index.html/index.php index.html/g' /etc/httpd24/httpd.conf
- 重启apache服务
[root@haha ~]# apachectl restart
[root@haha ~]# 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的映射
- 在windows客户机上增加环境变量,让虚拟域名和虚拟主机ip对照
在hosts文件里面底行增加ip与域名的映射
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
192.168.118.100 www.jerry.com #增加此行
在浏览器上使用域名访问,若看到以下界面则表示lamp架构搭建成功