lamp平台构建

#lamp平台构建

文章目录


##lamp平台构建
环境说明:
系统平台
centos7redhat7
ip172.16.12.128
需要安装的服务
httpd-2.4
mysql-5.7
php
php-mysql
lamp平台软件安装次序:
httpd --> mysql --> php
注意:php要求httpd使用prefork MPM
##安装httpd

yum源配置
[root@localhost yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo https:mirrors.aliyun.com/repo
[root@localhost ~]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS-Base.repo 
[root@localhost ~]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS-Base.repo 

安装 epel 配置包
[root@localhost ~]# yum install -y https:mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm

将 repo 配置中的地址替换为阿里云镜像站地址
[root@localhost ~]# sed -i 's|^#baseurl=https:download.example/pub|baseurl=https:mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@localhost ~]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*

安装开发工具包
[root@localhost ~]# yum groups mark install 'Development Tools'

创建apache服务的用户和组
[root@localhost ~]# useradd -M -r -s /sbin/nologin apache
[root@localhost ~]# id apache 
uid=975(apache) gid=974(apache) 组=974(apache)

安装依赖包
[root@localhost ~]# yum -y install --allowerasing openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ 

下载和安装apr以及apr-util
[root@localhost src]# wget https:downloads.apache.org/apr/apr-util-1.6.1.tar.bz2
[root@localhost src]# wget https:downloads.apache.org/apr/apr-1.6.5.tar.bz2
[root@localhost src]# tar xf apr-util-1.6.1.tar.bz2 
[root@localhost src]# tar xf apr-1.6.5.tar.bz2 
[root@localhost 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
[root@localhost apr-1.6.5]# vim configure
    cfgfile="${ofile}T"
    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
    # $RM "$cfgfile"        将此行加上注释,或者删除此行
    
[root@localhost apr-1.6.5]# ./configure --prefix=/usr/local/apr
配置过程略...
[root@localhost apr-1.6.5]# make && make install
编译安装过程略...

root@localhost apr-1.6.5]# 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
编译安装过程略...

[root@localhost httpd-2.4.54]# tar xf httpd-2.4.54.tar.bz2
[root@localhost httpd-2.4.38]# ./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
--with-apr-util=/usr/local/apr-util
[root@localhost bin]# make && make install
[root@localhost bin]# /usr/local/apache/bin/apachectl
[root@localhost ~]# ss -antl|grep 80
LISTEN 0      128                *:80              *:*   

设置环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[root@localhost ~]# source /etc/profile.d/httpd.sh 
[root@localhost ~]# which httpd
/usr/local/apache/bin/httpd

设置include
[root@localhost apache]# ln -s /usr/local/apache/include/ /usr/include/apach
e

设置man
[root@localhost apache]# vim /etc/man_db.conf 
添加一行 MANDATORY_MANPATH                       /usr/local/apache/man

取消ServerName前面的注释
[root@localhost ~]# sed -i '/#ServerName/s/#g' /etc/httpd24/httpd.conf

设置启动方式为systemctl
[root@localhost system]# cp sshd.service httpd.service
[root@localhost system]# vim httpd.service 

[Unit]
Description=httpd server daemon
After=network.target sshd-keygen.target

[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start
ExecStop=/usr/local/apache/bin/apachectl stop
ExecReload=/bin/kill -HUP $MAINPID

[root@localhost system]# httpd -t
Syntax OK
[root@localhost system]# systemctl daemon-reload 
[root@localhost system]# systemctl status httpd
● httpd.service - httpd server daemon
   Loaded: loaded (/usr/lib/systemd/system/http>
   Active: inactive (dead)
[root@localhost system]# systemctl start httpd
[root@localhost system]# ss -antl |grep 80
LISTEN 0      128                *:80              *:*  

###安装mysql

安装依赖包
[root@localhost ~]# yum -y install ncurses-devel openssl-devel openssl cmake

创建用户和组
[root@localhost ~]# useradd -M -r -s  /sbin/nologin  mysql
[root@localhost ~]# id mysql
uid=974(mysql) gid=973(mysql) 组=973(mysql)

下载二进制格式的mysql软件包
mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz

解压软件至/usr/local/
[root@localhost src]# tar xf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz  -C /usr/local/

设置软链接
[root@localhost local]# ln -s mysql-5.7.37-linux-glibc2.12-x86_64/ mysql
[root@localhost local]# chown -R mysql.mysql mysql
[root@localhost local]# chown -R mysql.mysql mysql-5.7.37-linux-glibc2.12-x86_64/

修改目录/usr/local/mysql的属主属组
[root@localhost local]# chown -R mysql.mysql mysql
[root@localhost local]# chown -R mysql.mysql mysql-5.7.37-linux-glibc2.12-x86_64/

添加环境变量
[root@localhost mysql]# echo 'export PATH=$PATH:/usr/local/mysql/bin/' > /etc/profile.d/mysql.sh

设置lib文件
[root@localhost mysql]# vim /etc/ld.so.conf.d/mysql.conf
[root@localhost mysql]# cat /etc/ld.so.conf.d/mysql.conf 
/usr/local/mysql/lib
[root@localhost mysql]# ldconfig 

配置include头文件
[root@localhost mysql]# ln -s /usr/local/mysql/include/ /usr/include/mysql

建立数据存放目录
[root@localhost mysql]# mkdir /opt/data
[root@localhost mysql]# chown -R mysql.mysql /opt/data/

初始化数据库
[root@localhost ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
 root@localhost: y?Jt*9rE:gC1

生成配置文件
[root@localhost mysql]# 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
[root@localhost ~]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'.
 SUCCESS!
 [root@localhost ~]# ss -antl|grep 3306
LISTEN 0      80                 *:3306            *:*          

设置systemctl启动
[root@localhost ~]# cd /usr/lib/systemd/system
[root@localhost system]# cp sshd.service mysql.service
[root@localhost system]# vim mysql.service 
[root@localhost system]# cat mysql.service 
[Unit]
Description=mysql server daemon
After=network.target sshd-keygen.target

[Service]
Type=forking
ExecStart=/etc/init.d/mysqld start
ExecStop=/etc/init.d/mysqld stop
ExecReload=/bin/kill -HUP $MAINPID

[root@localhost system]# systemctl daemon-reload 
[root@localhost system]# systemctl start mysql
[root@localhost system]# ss -antl |grep 3306
LISTEN 0      80                 *:3306            *:*          

加入开机自启
[root@localhost ~]# systemctl enable mysqld
mysqld.service is not a native service, redirecting to systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install enable mysqld

修改密码
[root@localhost ~]# yum install libncurses*
[root@localhost ~]# mysql -u root -p'y?Jt*9rE:gC1'

mysql> set password = password('1234');
Query OK, 0 rows affected, 1 warning (0.00 sec)

[root@localhost ~]# mysql -u root -p1234
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.37 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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>

##安装php

安装依赖包
[root@localhost ~]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel  pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel --allowerasing --nobest

下载php
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https:www.php.net/distributions/php-7.4.30.tar.xz

编译安装php
[root@localhost src]# tar xf php-7.4.30.tar.xz 
./configure --prefix=/usr/local/php7  \
--with-config-file-path=/etc \
--enable-fpm \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-openssl \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--enable-exif  \
--enable-ftp \
--enable-gd \
--with-jpeg \
--with-zlib-dir \
--with-freetype \
--with-gettext \
--enable-json \
--enable-mbstring \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-readline \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--with-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-pcntl \
--enable-posix

+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE. By continuing this installation  |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.


安装后配置
[root@localhost php7]# echo 'export PATH=$PATH:/usr/local/php7/bin' > /etc/profile.d/php7.sh
[root@localhost php7]# source /etc/profile.d/php7.sh 

[root@localhost php7]# ln -s /usr/local/php7/include/ /usr/include/php7
[root@localhost php7]# echo '/usr/local/php7/lib/' > /etc/ld.so.conf.d/php7.conf
[root@localhost php7]# ldconfig 


配置php-fpm
[root@localhost php7]# cd /usr/src/php-7.4.30/
[root@localhost php-7.4.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost usr]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[root@localhost usr]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf

[root@localhost system]# vim php.service 
[Unit]
Description=php server daemon
After=network.target sshd-keygen.target

[Service]
Type=forking
ExecStart=/etc/init.d/php-fpm start
ExecStart=/etc/init.d/php-fpm stop
ExecReload=/bin/kill -HUP $MAINPID


启动php-fpm
[root@localhost system]# systemctl start php
[root@localhost system]# systemctl enable php
Created symlink /etc/systemd/system/multi-user.target.wants/php.service → /usr/lib/systemd/system/php.service.
[root@localhost system]# ss -antl |grep 9000
LISTEN 0      128        127.0.0.1:9000      0.0.0.0:*  

##配置apache
3.4.1 启用代理模块
在apache httpd 2.4以后已经专门有一个模块针对FastCGI的实现,此模块为mod_proxy_fcgi.so,它其实是作为mod_proxy.so模块的扩展,因此,这两个模块都要加载,编辑httpd.conf文件,取消以下两行内容的注释:

启用httpd的相关模块

[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
创建虚拟主机目录并生成php测试页面
[root@localhost ~]# mkdir /usr/local/apache/htdocs/dog/index.php
[root@localhost ~]# cat > /usr/local/apache/htdocs/dog/index.php <<EOF
<?php
   phpinfo();
?>
EOF
[root@localhost htdocs]# chown -R apache.apache /usr/local/apache/
<VirtualHost *:80>
    DocumentRoot "/usr/local/apache/htdocs/dog
    ServerName dog.example.com
    ErrorLog "logs/dog.example.com-error_log"
    CustomLog "logs/dod$dummy-host.example.com"g.example.com-access_log" common
ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$ fcgi:127.0.0.1:9000/usr/local/apache/htdocs/dog/$1
   <Directory "/usr/local/apache/htdocs/wangqing.com">
        Options none
        AllowOverride none
        Require all granted
    </Directory>
</VirtualHost>

[root@localhost ~]# vim /etc/httpd24/httpd.conf
搜索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@localhost ~]# sed -i '/    DirectoryIndex/s/index.html/index.php index.html/g' /etc/httpd24/httpd.conf

[root@localhost ~]# systemctl restart httpd
[root@localhost ~]# ss -antl |grep 80
LISTEN 0      80                 *:3306            *:*          
LISTEN 0      128                *:80              *:*          


设置systemctl启动
[root@localhost ~]# /etc/init.d/php-fpm start
Starting php-fpm  done
[root@localhost ~]# ss -antl
State  Recv-Q Send-Q Local Address:Port Peer Address:PortProcess                                                  
LISTEN 0      128        127.0.0.1:9000      0.0.0.0:*

关闭防火墙
[root@localhost dog]# systemctl stop firewalld.service 
[root@localhost dog]# vim /etc/selinux/config 
[root@localhost dog]# setenforce 0
[root@localhost dog]# systemctl disable firewalld.service 
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

验证
1.修改/etc/hosts文件,添加域名与IP的映射
2.在浏览器上使用域名访问,若看到以下界面则表示lamp架构搭建成功,否则请检查你的操作

LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*

关闭防火墙
[root@localhost dog]# systemctl stop firewalld.service
[root@localhost dog]# vim /etc/selinux/config
[root@localhost dog]# setenforce 0
[root@localhost dog]# systemctl disable firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值