LAMP架构实验

    科技2026-08-23  4

    LAMP架构实验

    1.关闭防火墙及内核防护 systemctl stop firewalld systemctl disable firewalld setenforce 0 2.搭建YUM仓库 mount /dev/cdrom /mnt cd /etc/yum.repos.d/ mkdir backup mv C* backup/ cp backup/CentOS-Base.repo local.repo vi local.repo 插入一下内容 [centos] name=centos baseurl=file:///mnt gpgcheck=0 enabled=1 #gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

    清除缓存: yum clean all yum makecache

    3.安装Apache所需软件 apr-1.6.2.tar.gz apr-util-1.6.0.tar.gz httpd-2.4.29.tar.gz

    tar zxvf apr-1.6.2.tar.gz tar zxvf apr-util-1.6.0.tar.gz tar jxvf httpd-2.4.29.tar.bz2

    mv apr-1.6.2 httpd-2.4.29/srclib/apr mv apr-util-1.6.0 httpd-2.4.29/srclib/apr-util

    4.安装环境依赖包 yum -y install gcc gcc-c++ make pcre-devel expat-devel perl

    5.配置模块 ./configure –prefix=/usr/local/httpd –enable-so –enable-rewrite –enable-charset-lite –enable-cgi

    各配置模块解释 –prefix:指定将 httpd 服务程序安装到哪个目录下,如/usr/local/httpd –enable-so:启用动态加载模块支持,使 httpd 具备进一步扩展功能的能力。 –enable-rewrite:启用网页地址重写功能,用于网站优化及目录迁移维护。 –enable-charset-lite:启动字符集支持,以便支持使用各种字符集编码的网 –enable-cgi:启用 CGI 脚本程序支持,便于扩展网站的应用访问能力。

    5.编译及安装 make && make install

    6.优化执行路径 ln -s /usr/local/httpd/conf/httpd.conf /etc/ ln -s /usr/local/httpd/bin/* /usr/local/bin/ httpd -v ###查看下HTTP版本

    7.添加 httpd 系统服务 cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd vi /etc/init.d/httpd chkconfig: 35 85 21 description: Apache is a World Wide Web server

    chkconfig --add httpd chkconfig --list httpd

    cd /lib/systemd/system/ vim httpd.service 插入以下内容 [Unit] Description=The Apache HTTP Server After=network.target [Service] Type=forking PIDFile=/usr/local/httpd/logs/httpd.pid ExecStart= /usr/local/bin/apachectl $OPTIONS ExecrReload= /bin/kill -HUP $MAINPID [Install] WantedBy=multi-user.target

    并保存退出

    systemctl start httpd.service systemctl enable httpd.service systemctl is-enabled httpd.service

    8.修改配置文件

    vim /usr/local/httpd/conf/httpd.conf 修改Listen 虚拟机IP地址:80 去掉ServerName前面的#号 ServerName www.51xit.top:80 9.浏览器验证 netstat -anpt | grep 80 验证成功 网页演示:在浏览器输入虚拟机IP,出现“It works!”表示已在运行。

    mysql 1.安装依赖包 yum -y install ncurses-devel autoconf cmake

    2.编译安装 cd /opt tar zxvf mysql-5.6.26.tar.gz cd mysql-5.6.26/

    cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DSYSCONFDIR=/etc -DMYSQL_DATADIR=/home/mysql/ -DMYSQL_UNIX_ADDR=/home/mysql/mysql.sock

    make && make install

    3.添加mysqld服务 cp support-files/my-default.cnf /etc/my.cnf yes (提示询问是否覆盖,输入yes回车即可) cp support-files/mysql.server /etc/init.d/mysqld

    cd /etc/init.d/ ls -l (可以看到mysqld是没有执行权限的) chmod +x mysqld (赋予mysqld可执行权限) ls -l (现在mysqld有可执行的权限了)

    chkconfig --add /etc/init.d/mysqld (添加/etc/init.d目录下的mysqld服务) chkconfig --level 35 mysqld on (开启3、5级别)

    4.修改环境变量 echo “PATH=$PATH:/usr/local/mysql/bin” >> /etc/profile (追加到/etc/profile中。该文件是每个用户登录时都会运行的环境变量设置,属于系统级别的环境变量,设置在里面的东西对所有用户适用。) source /etc/profile (生效) echo $PATH (看环境变量会多出刚刚追加的路径)

    5.添加mysql程序用户 useradd -s /sbin/nologin mysql (添加程序用户) chown -R mysql.mysql /usr/local/mysql/ (赋予程序用户权限)

    6.初始化数据库 /usr/local/mysql/scripts/mysql_install_db –user=mysql –ldata=/var/lib/mysql –basedir=/usr/local/mysql/ –datadir=/home/mysql/

    vim /etc/init.d/mysqld shift+: /basedir回车 修改: basedir=/usr/local/mysql datadir=/home/mysql

    7.启动数据库 service mysqld start

    netstat -antp | grep 3306

    8.修改数据库root密码 mysqladmin -u root -p password “abc123” (修改root密码,这里密码设置为abc123,执行该命令后会让你输入一个密码,但第一次登陆没有密码,直接按回车即可) 此时会有以下提示在命令行输入密码是不安全的,因为history会记录下来设置的密码。 Warning: Using a password on the command line interface can be insecure. 此时密码已经修改完成。

    9.登陆数据库 mysql -u root -p 然后输入密码,密码输入时是不显示的,所以直接输入完密码按回车即可

    到此数据库搭建完成,输入show databases;可以查看现有的库

    php 1.安装环境依赖包 yum -y install gd \ libpng libpng-devel pcre pcre-devel libxml2-devel libjpeg-devel 解释: gd (gd库,php处理图片的处理库,专门用来处理图片) libpng (支持png格式) libpng-devel pcre (支持正则表达式) pcre-devel libxml2-devel (支持xml格式的2版本图片) libjpeg-devel (支持jpg/jgep格式图片)

    2.解压php软件包 cd /opt tar jxvf php-5.6.11.tar.bz2 cd php-5.6.11

    3.配置模块 ./configure –prefix=/usr/local/php5 \ –with-gd –with-zlib –with-apxs2=/usr/local/httpd/bin/apxs –with-mysql=/usr/local/mysql –with-config-file-path=/usr/local/php5 –enable-mbstring

    4.编译安装 make &&make install

    5.配置php cp php.ini-development /usr/local/php5/php.ini (将配置文件复制到php.ini) ln -s /usr/local/php5/bin/* /usr/local/bin/ (对php/bin下的可执行文件建立软连接,便于系统识别)

    6.让apache识别,加载php功能模块 vim /etc/httpd.conf DirectoryIndex index.html index.php (在这个模块添加index.php)

    在图中位置添加这两行: AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps

    修改过配置文件,先停止在启动 service httpd stop service httpd start cd /usr/local/httpd/htdocs/ (进入首页目录) ls (可以看到一个首页) vim index.html 清空里面的内容,加入下面内容:

    <?php phpinfo(); ?>

    保存退出

    mv index.html index.php (将index.html改为index.php)

    到此,LAMP架构搭建完成。

    Processed: 0.012, SQL: 9