lamp平台搭建:
环境说明:
系统平台 | ip地址 | 需要安装的服务 |
---|---|---|
centos7 | 192.168.110.128 | httpd-2.4 mysql-5.7 php php-mysql |
安装httpd
- 安装开发工具:
[root@localhost ~]# yum groupinstall "Development Tools" -y
安装过程已省略
- 创建apache用户和组:
[root@localhost ~]# groupadd -r apache
[root@localhost ~]# useradd -r -M -s /sbin/nologin -g apache apache
- 安装依赖包:
[root@localhost ~]# yum -y install openssl-devel pcre-devel expat-devel lib tool
安装过程已省略。
- 下载和安装arp和arp-util:
[root@localhost ~]# cd /usr/src/
[root@localhost src]wget http://mirrors.shu.edu.cn/apache//apr/apr-1.6.3.
tar.bz2
下载过程已省略
[root@localhost src]#wget http://mirrors.shu.edu.cn/apache//apr/apr-util-1
.6.1.tar.bz2
下载过程已省略
- 解压apr和apr-util:
[root@localhost src]# ls
apr-1.6.3.tar.bz2 apr-util-1.6.1.tar.bz2 debug kernels
[root@localhost src]# tar xf apr-1.6.3.tar.bz2
[root@localhost src]# tar xf apr-util-1.6.1.tar.bz2
[root@localhost src]# ls
apr-1.6.3 apr-1.6.3.tar.bz2 apr-util-1.6.1 apr-util-1.6.1.tar.bz2 debug
kernels
[root@localhost src]# cd apr-1.6.3
[root@localhost apr-1.6.3]# vim configure
cfgfile="${ofile}T"
trap "$RM \"$cfgfile\"; exit 1" 1 2 15
#$RM "$cfgfile" #将此行加上注释或者删除此行
- 配置编译apr和apr-util:
[root@localhost apr-1.6.3]# ./configure --prefix=/usr/local/apr
配置过程省略
[root@localhost apr-1.6.3]# make && make install
编译过程省略
[root@localhost apr-1.6.3]# cd /usr/src/apr-util-1.6.1
[root@localhost apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util -
-with-apr=/usr/local/apr
配置过程略
[root@localhost apr-util-1.6.1]# make && make install
编译过程略
- 下载编译安装httpd:
[root@localhost ~]# wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.4.3
4.tar.bz2
下载过程略
[root@localhost ~]# ls
httpd-2.4.34.tar.bz2
[root@localhost ~]# tar xf httpd-2.4.34.tar.bz2
[root@localhost ~]# cd httpd-2.4.34
[root@localhost 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@localhost httpd-2.4.34]# make && make install
编译过程略
- 安装后配置:
[root@localhost ~]# echo 'PATH=/usr/local/apache/bin:$PATH' > /etc/profile.
d/httpd.sh
[root@localhost ~]# source /etc/profile.d/httpd.sh
[root@localhost ~]# ln -s /usr/local/apache/include/ /usr/include/httpd
[root@localhost ~]# echo 'MANPATH /usr/local/apache/man' >> /etc/man.config
- 在主配置文件取消ServerName的注释:
[root@localhost ~]#sed -i '/#ServerName/s/#//g' /etc/httpd24/httpd.conf
- 关闭防火墙,启动apache:
[root@localhost ~]#systemctl stop firewalld
[root@localhost ~]#setenforce 0
[root@localhost ~]# apachectl start
安装mysql
- 安装依赖包:
[root@localhost ~]# yum -y install ncurses-devel openssl-devel openssl cmak
e mariadb-devel
- 创建用户和组:
[root@localhost src]# groupadd -r -g 306 mysql
[root@localhost src]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql
- 下载二进制格式的mysql软件包:
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://downloads.mysql.com/archives/get/file/my
sql-5.7.22-linux-glibc2.12-x86_64.tar.gz 下载过程略
- 解压至/usr/local/
[root@localhost src]# ls
debug kernels mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C
/usr/local/
[root@localhost ~]# ls /usr/local/
bin games lib libexec sbin src
- 做一个软连接:
[root@localhost local]# ln -sv mysql-5.7.23-linux-glibc2.12-x86_64/ mysql
‘mysql’ -> ‘mysql-5.7.23-linux-glibc2.12-x86_64/’
- 修改/usr/local/my属主属组:
[root@localhost ~]# chown -R mysql.mysql /usr/local/mysql
[root@localhost ~]# ll /usr/local/mysql -d
lrwxrwxrwx 1 mysql mysql 36 Aug 14 16:00 /usr/local/mysql -> mysql-5.7.22-l
inux-glibc2.12-x86_64/
- 添加环境变量:
[root@localhost ~]# ls /usr/local/mysql
bin COPYING docs include lib man README share support-files
[root@localhost ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/pr
ofile.d/mysql.sh
[root@localhost ~]# . /etc/profile.d/mysql.sh
[root@localhost ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/roo
t/bin
- 建立数据库存放目录:
[root@localhost local]# chown -R mysql.mysql /opt/data/
[root@localhost local]# ll /opt/
总用量 4
-rw-r--r--. 1 root root 45 2月 24 06:57 cc
drwxr-xr-x. 2 mysql mysql 6 2月 24 11:08 data
- 初始化数据库:
[root@localhost ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
[root@localhost src]# echo 'ri9woXLadi,G' >/root/a
- 配置mysql并生成配置文件:
[root@localhost ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
‘/usr/local/include/mysql’ -> ‘/usr/local/mysql/include/’
[root@localhost ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@localhost ~]# ldconfig -v
- 生成配置文件:
[root@localhost ~]# 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@localhost ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@localhost ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
- 启动mysql服务:
[root@localhost ~]# service mysqld start
Starting MySQL.. SUCCESS!
[root@localhost ~]# 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 :::*
- 登入mysql修改密码:
[root@haha local]# mysql -uroot -p'ri9woXLadi,G'
mysql> set password=password('123.com');
Query OK, 0 rows affected, 1 warning (0.00 sec)
安装php
- 安装依赖包
[root@localhost ~]# yum -y install libxml2 libxml2-devel openssl openssl-de
vel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng li
bpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel
readline readline-devel libxslt libxslt-devel mhash mhash-devel
安装过程略
- 下载php:
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget http://cn.php.net/distributions/php-7.2.8.tar.xz
- 编译安装php:
[root@localhost src]# tar xf php-7.2.8.tar.xz
[root@localhost src]# cd php-7.2.8
[root@localhost 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@localhost php-7.2.8]# make -j $(cat /proc/cpuinfo |grep processor|wc
-l)
编译过程略
[root@localhost php-7.2.8]# make install
安装过程略
- 安装后配置:
[root@localhost ~]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@localhost ~]# source /etc/profile.d/php7.sh
[root@localhost php-7.2.8]# which php
/usr/local/php7/bin/php
[root@localhost php-7.2.8]# php -v
PHP 7.2.8 (cli) (built: Aug 16 2018 13:27:30) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
- 配置php-fpm:
[root@localhost php-7.2.8]# cp php.ini-production /etc/php.ini
[root@localhost php-7.2.8]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-7.2.8]# chmod +x /etc/rc.d/init.d/php-fpm
[root@localhost php-7.2.8]# cp /usr/local/php7/etc/php-fpm.conf.default /us
r/local/php7/etc/php-fpm.conf
[root@localhost php-7.2.8]# cp /usr/local/php7/etc/php-fpm.d/www.conf.defau
lt /usr/local/php7/etc/php-fpm.d/www.conf
- 编辑php-fpm的配置文件(/usr/local/php7/etc/php-fpm.conf)
配置fpm的相关选项为你所需要的值:
[root@localhost ~]# 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@localhost ~]# 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@localhost ~]# service php-fpm start
Starting php-fpm done
root@localhost src]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:9000 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 80 :::3306 :::*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
[root@localhost ~]# 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
配置虚拟主机:
- 启动ttpd相关的模块:
[root@localhost ~]# sed -i '/proxy_module/s/#//g' /etc/httpd24/httpd.conf
[root@localhost ~]# sed -i '/proxy_fcgi_module/s/#//g' /etc/httpd24/httpd.conf
- 在需要使用的cfgi的虚拟主机中添加如下两行:
ProxyRequests Off //关闭正向代理
ProxyPassMatch ^/(.*\.php)$fcgi://127.0.0.1:9000/var/www/html/idfsoft.com/
$1
- 创建虚拟主机目录并生成php测试页面:
[root@localhost ~]# mkdir /usr/local/apache/htdocs/wuzhiyong.com
localhost ~]# cat > /usr/local/apache/htdocs/wangqing.com/index.php <<EOF
<?php
phpinfo();
?>
EOF
[root@localhost ~]# chown -R apache.apache /usr/local/apache/htdocs/
[root@localhost ~]# ll /usr/local/apache/htdocs/ -d
drwxr-xr-x 3 apache apache 44 Aug 16 14:50 /usr/local/apache/htdocs/
- 在配置文件最后加入以下内容:
[root@localhost ~]# vim /etc/httpd24/httpd.conf
<VirtualHost *:80>
DocumentRoot "/usr/local/apache/htdocs/wuzhiyong.com"
ServerName www.wuzhiyong.com
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/wuzhiyong.com/$1
<Directory "/usr/local/apache/htdocs/wuzhiyong.com">
Options none
AllowOverride none
Require all granted
</Directory>
</VirtualHost>
- 搜索AddType,添加以下内容:
[root@localhost ~]# vim /etc/httpd24/httpd.conf
#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@localhost ~]# sed -i '/ DirectoryIndex/s/index.html/index.php inde
x.html/g' /etc/httpd24/httpd.conf
- 重启apache服务:
[root@localhost ~]# apachectl stop
[root@localhost ~]# 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 :::*
修改 host 文件,添加域名与IP的映射
在windows客户机上增加环境变量,让虚拟域名和虚拟主机ip对照
在浏览器上使用域名访问,若看到以下界面则表示lamp架构搭建成功