文章目录 LAMP简介web服务器工作流程CGI和FastCGIhttpd与php结合方式web工作流程 LAMP平台构建安装httpd安装mysql安装php配置apache启用代理模块
所谓lamp,其实就是由linux+Apache+Mysql/MariaDB+PHP/Perl/python的一组动态网站或者服务器的开源软件,除Linux外其它各部件本身都是各自独立的程序,但是因为经常被放在一起使用,拥有了越来越高的兼容度,共同组成了一个强大的WEB应用程序平台。
LAMP指的是Linux(操作系统)、Apache(Http服务器)、mysql(也指MariaDB,数据库软件)和php(有时也是指Perl或Python)的第一个字母,一般用来建立web应用平台。
web服务器的资源分为两种,静态资源和动态资源
如图所示
阶段①显示的是httpd服务器(即apache)和php服务器通过FastCGI协议进行通信,且php作为独立的服务进程运行
阶段②显示的是php程序和mysql数据库间通过mysql协议进行通信。php与mysql本没有什么联系,但是由Php语言写成的程序可以与mysql进行数据交互。同理perl和python写的程序也可以与mysql数据库进行交互
CGI(Common Gateway Interface,通用网关接口),CGI是外部应用程序(CGI程序)与WEB服务器之间的接口标准,是在CGI程序和Web服务器之间传递信息的过程。CGI规范允许Web服务器执行外部程序,并将它们的输出发送给Web浏览器,CGI将web的一组简单的静态超媒体文档变成一个完整的新的交互式媒体。
FastCGI(Fast Common Gateway Interface)是CGI的改良版,CGI是通过启用一个解释器进程来处理每个请求,耗时且耗资源,而FastCGI则是通过master-worker形式来处理每个请求,即启动一个master主进程,然后根据配置启动几个worker进程,当请求进来时,master会从worker进程中选择一个去处理请求,这样就避免了重复的生成和杀死进程带来的频繁cpu上下文切换而导致耗时
httpd与php结合的方式有以下三种:
当有访问请求通过http协议到达httpd服务器时,httpd进行解析
lamp平台软件安装次序
httpd --> mysql --> php
yum源配置
[root@100 ~]# wget -O /etc/yum.repos.d/Centos-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo[root@100 ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo[root@100 ~]# yum makecacheWaiting for process with pid 2260 to finish.CentOS-8.5.2111 - Base - mirrors.aliyun.com 30 kB/s | 3.9 kB 00:00CentOS-8.5.2111 - Extras - mirrors.aliyun.com 105 kB/s | 1.5 kB 00:00CentOS-8.5.2111 - AppStream - mirrors.aliyun.com 37 kB/s | 4.3 kB 00:00Metadata cache created.[root@100 ~]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm[root@100 ~]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*[root@100 ~]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*[root@100 ~]# ls /etc/yum.repos.d/CentOS-Base.repo epel-modular.repo epel.repo epel-testing-modular.repo epel-testing.repo
安装工具包
[root@100 ~]# yum groups mark install 'Development Tools'
创建apache用户
[root@100 ~]# useradd -r -M -s /sbin/nologin apache[root@100 ~]# id apacheuid=975(apache) gid=974(apache) groups=974(apache)
安装依赖包
[root@100 ~]# yum -y install openssl-devel pcre-devel expat-devel libtool GCc gcc-c++
下载安装apr与apr-util与httpd
//安装apr[root@100 ~]# cd /usr/src/[root@100 src]# wget https://downloads.apache.org/apr/apr-1.6.5.tar.bz2[root@100 src]# wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.bz2[root@100 src]# wget https://downloads.apache.org/httpd/httpd-2.4.54.tar.bz2[root@100 src]# tar xf apr-1.6.5.tar.bz2[root@100 src]# tar xf apr-util-1.6.1.tar.bz2[root@100 src]# tar xf httpd-2.4.54.tar.bz2[root@100 src]# lsapr-1.6.5 apr-1.6.5.tar.bz2 apr-util-1.6.1 apr-util-1.6.1.tar.bz2 debug httpd-2.4.54 httpd-2.4.54.tar.bz2 kernels[root@100 src]# cd apr-1.6.5/[root@100 apr-1.6.5]# vim configure cfgfile=${ofile}T trap "$RM \"$cfgfile\"; exit 1" 1 2 15# $RM "$cfgfile"//将此行加上注释或者删除[root@100 apr-1.6.5]# ./configure --prefix=/usr/local/apr[root@100 apr-1.6.5]# make && make install//安装apr-util[root@100 apr-1.6.5]# cd ../apr-util-1.6.1/[root@100 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr[root@100 apr-util-1.6.1]# make && make install//安装httpd[root@100 apr-util-1.6.1]# cd ../httpd-2.4.54/[root@100 httpd-2.4.54]# ./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@100 httpd-2.4.54]# make && make install//配置环境变量[root@100 httpd-2.4.54]# echo "export PATH=$PATH:/usr/local/apache/bin" > /etc/profile.d/httpd.sh[root@100 httpd-2.4.54]# ln -s /usr/local/apache/include/ /usr/include/httpd[root@100 httpd-2.4.54]# vim /etc/man_db.confMANDATORY_MANPATH /usr/local/apache/man[root@100 httpd-2.4.54]# source /etc/profile.d/httpd.sh[root@100 ~]# cd /usr/lib/systemd/system[root@100 system]# vim httpd.service[Unit]Description=apache server daemonAfter=network.target sshd-keygen.target[Service]Type=forkingExecStart=/usr/local/apache/bin/apachectl startExecStop=/usr/local/apache/bin/apachectl stopExecReload=/bin/kill -HUP $MAINPID[Install]WantedBy=multi-user.target[root@100 ~]# systemctl start httpd[root@100 ~]# systemctl enable httpdCreated symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.[root@100 ~]# ss -antl |grep 80LISTEN 0 128 *:80 *:*
配置防火墙规则
[root@100 ~]# firewall-cmd --permanent --zone=public --add-service=httpsuccess[root@100 ~]# firewall-cmd --permanent --zone=public --add-service=httpssuccess[root@100 ~]# firewall-cmd --reloadsuccess
浏览器访问测试
安装依赖包
[root@100 ~]# yum -y install ncurses-devel openssl-devel openssl cmake
创建mysql用户
[root@100 ~]# useradd -r -M -s /sbin/nologin mysql[root@100 ~]# id mysqluid=974(mysql) gid=973(mysql) groups=973(mysql)
下载mysql二进制安装包并解压
[root@100 src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz[root@100 src]# tar xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/[root@100 src]# cd /usr/local/[root@100 local]# ln -s mysql-5.7.38-linux-glibc2.12-x86_64/ mysql[root@100 local]# chown -R mysql.mysql mysql*[root@100 local]# ll -d mysql*lrwxrwxrwx. 1 mysql mysql 36 Aug 2 16:28 mysql -> mysql-5.7.38-linux-glibc2.12-x86_64/drwxr-xr-x. 9 mysql mysql 129 Aug 2 16:27 mysql-5.7.38-linux-glibc2.12-x86_64
创建数据存放目录
[root@100 mysql]# mkdir /opt/data[root@100 mysql]# chown -R mysql.mysql /opt/data
初始化数据库
[root@100 mysql]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/2022-08-02T08:38:35.411890Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).2022-08-02T08:38:35.565117Z 0 [Warning] InnoDB: New log files created, LSN=457902022-08-02T08:38:35.587059Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.2022-08-02T08:38:35.641848Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 7f99e6ef-123e-11ed-87d4-000c29fb2070.2022-08-02T08:38:35.642615Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.2022-08-02T08:38:35.799655Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.2022-08-02T08:38:35.799685Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.2022-08-02T08:38:35.800059Z 0 [Warning] CA certificate ca.pem is self signed.2022-08-02T08:38:35.858711Z 1 [Note] A temporary password is generated for root@localhost: B+#LeN5Yx,Hy[root@100 mysql]# echo "B+#LeN5Yx,Hy" > /root/sql_passwd
添加环境变量
[root@100 mysql]# echo "export PATH=$PATH:/usr/local/mysql/bin" > /etc/profile.d/mysql.sh[root@100 mysql]# source /etc/profile.d/mysql.sh[root@100 mysql]# ln -s /usr/local/mysql/include/ /usr/include/mysql[root@100 mysql]# echo "/usr/local/mysql/lib" > /etc/ld.so.conf.d/mysql.conf[root@100 mysql]# vim /etc/man_db.confMANDATORY_MANPATH /usr/local/mysql/man[root@100 mysql]# which mysqld/usr/local/mysql/bin/mysqld
生成配置文件
[root@100 mysql]# vim /etc/my.cnf[mysqld]basedir = /usr/local/mysqldatadir = /opt/datasocket = /tmp/mysql.sockport = 3306pid-file = /opt/data/mysql.piduser = mysqlskip-name-resolve
配置服务启动脚本
[root@100 mysql]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld[root@100 mysql]# vim /etc/init.d/mysqld 46 basedir=/usr/local/mysql 47 datadir=/opt/data[root@100 mysql]# cd /usr/lib/systemd/system[root@100 system]# vim mysqld.service[Unit]Description=mysqld server daemonAfter=network.target sshd-keygen.target[Service]Type=forkingExecStart=/etc/init.d/mysqld startExecStop=/etc/init.d/mysqld stopExecReload=/bin/kill -HUP $MAINPID[Install]WantedBy=multi-user.target
启动mysql
[root@100 system]# systemctl start mysqld[root@100 system]# systemctl enable mysqldSynchronizing state of mysqld.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.Executing: /usr/lib/systemd/systemd-sysv-install enable mysqldCreated symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.[root@100 system]# ss -antl |grep 3306LISTEN 0 80 *:3306 *:*
进入mysql修改密码
[root@100 ~]# mysql -uroot -p'B+#LeN5Yx,Hy'mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory//解决方法[root@100 ~]# yum provides libncurses.so.5Last metadata expiration check: 0:59:59 ago on Tue 02 Aug 2022 15:48:59 CST.ncurses-compat-libs-6.1-9.20180224.el8.i686 : Ncurses compatibility librariesRepo : baseMatched from:Provide : libncurses.so.5[root@100 ~]# yum -y install ncurses-compat-libs//再次进入数据库修改密码[root@100 ~]# mysql -uroot -p'B+#LeN5Yx,Hy'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 2Server version: 5.7.38Copyright (c) 2000, 2022, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> set password = password('123456');Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> quitBye//使用修改后的密码登录测试[root@100 ~]# mysql -uroot -p123456mysql: [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 3Server version: 5.7.38 MySQL Community Server (GPL)Copyright (c) 2000, 2022, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>
安装依赖包
[root@100 ~]# 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 php-mysqlnd
下载php并解压
[root@100 ~]# cd /usr/src/[root@100 src]# wget https://www.php.net/distributions/php-7.4.30.tar.xz[root@100 src]# tar xf php-7.4.30.tar.xz[root@100 src]# cd php-7.4.30/
编译安装php
[root@100 php-7.4.30]# ./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
遇到以下问题的解决方法
configure: error: Package requirements (libxml-2.0 >= 2.7.6) were not met:
Package ‘libxml-2.0’, required by ‘virtual:world’, not found
[root@100 php-7.4.30]# yum install libxml2-devel -y
configure: error: Package requirements (sqlite3 > 3.7.4) were not met:
Package ‘sqlite3’, required by ‘virtual:world’, not found
[root@100 php-7.4.30]# yum -y install sqlite-devel
configure: error: Package requirements (oniguruma) were not met:
Package ‘oniguruma’, required by ‘virtual:world’, not found
[root@100 php-7.4.30]# yum -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
configure: error: Package requirements (libzip >= 0.11 libzip != 1.3.1 libzip != 1.7.0) were not met:
Package ‘libzip’, required by ‘virtual:world’, not found
Package ‘libzip’, required by ‘virtual:world’, not found
Package ‘libzip’, required by ‘virtual:world’, not found
[root@100 php-7.4.30]# yum -y install libzip-devel
继续编译安装
[root@100 php-7.4.30]# make && make install
配置环境变量
[root@100 php7]# echo "export PATH=$PATH:/usr/local/php7/bin" > /etc/profile.d/php.sh[root@100 php7]# source /etc/profile.d/php.sh[root@100 php7]# ln -s /usr/local/php7/include/ /usr/include/php[root@100 php7]# echo "/usr/local/php7/lib" > /etc/ld.so.conf.d/php.conf[root@100 php7]# which php/usr/local/php7/bin/php[root@100 php7]# php -vPHP 7.4.30 (cli) (built: Aug 2 2022 17:22:46) ( NTS )Copyright (c) The PHP GroupZend Engine v3.4.0, Copyright (c) Zend Technologies
配置php-fpm
[root@100 php-7.4.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm[root@100 php-7.4.30]# chmod +x /etc/init.d/php-fpm[root@100 php-7.4.30]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf[root@100 php-7.4.30]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf[root@100 ~]# cd /usr/lib/systemd/system[root@100 system]# vim php-fpm.service[Unit]Description=php-fpm server daemonAfter=network.target sshd-keygen.target[Service]Type=forkingExecStart=/etc/init.d/php-fpm startExecStop=/etc/init.d/php-fpm stopExecReload=/bin/kill -HUP $MAINPID[Install]WantedBy=multi-user.target
启动php-fpm
[root@100 system]# systemctl enable --now php-fpm.serviceSynchronizing state of php-fpm.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.Executing: /usr/lib/systemd/systemd-sysv-install enable php-fpmCreated symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.[root@100 ~]# ss -antl |grep 9000LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
在apache httpd 2.4以后已经专门有一个模块针对FastCGI的实现,此模块为mod_proxy_fcgi.so,它其实是作为mod_proxy.so模块的扩展,因此,这两个模块都要加载,编辑httpd.conf文件,取消以下两行内容的注释:
[root@100 ~]# vim /etc/httpd24/httpd.confLoadModule proxy_module modules/mod_proxy.soLoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
在需要使用fcgi的虚拟主机中添加类似如下两行:
ProxyRequests Off //关闭正向代理ProxyPaSSMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/PATH/TO/DOCUMENT_ROOT/$1
例如
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/var/www/html/idfsoft.com/$1
以上设置表示把以.php结尾的文件请求发送到php-fpm进程,php-fpm至少需要知道运行的目录和URI,所以这里直接在fcgi://127.0.0.1:9000后指明了这两个参数,其它参数的传递已经被mod_proxy_fcgi.so进行了封装,不需要手动指定。
注意:
这里写的/var/www/html/是yum源安装方式生成的网页存放目录,这里必须改成你编译安装指定的网页存放路径
这里的idfsoft.com是域名,你必须改成你所使用的域名
这里的$1表示匹配所有以.php结尾的http请求
创建虚拟主机目录并生成php测试页面
[root@100 ~]# mkdir /usr/local/apache/htdocs/keven.com[root@100 ~]# vim /usr/local/apache/htdocs/keven.com/index.php[root@100 ~]# chown -R apache.apache /usr/local/apache/htdocs/[root@100 ~]# ll -d /usr/local/apache/htdocs/drwxr-xr-x. 3 apache apache 41 Aug 2 17:58 /usr/local/apache/htdocs/[root@100 ~]# vim /etc/httpd24/extra/httpd-vhosts.conf DocumentRoot "/usr/local/apache/htdocs/keven.com" ServerName www.keven233.com ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/keven.com/$1 Options none AllowOverride none Require all granted [root@100 ~]# vim /etc/httpd24/httpd.conf399 AddType application/x-httpd-php .php//添加这两行400 AddType application/x-httpd-php-source .phps261 DirectoryIndex index.php index.html//在这行添加index.php488 Include /etc/httpd24/extra/httpd-vhosts.conf//将这行注释取消[root@100 ~]# systemctl restart httpd
在/etc/hosts文件里添加IP和域名
[root@100 ~]# vim /etc/hosts192.168.159.100 www.keven233.com
在浏览器使用域名访问
将C:\Windows\System32\drivers\etc\hosts拖到桌面用记事本打开添加IP和域名
保存后再将hosts文件拖回C:\Windows\System32\drivers\etc\下
--结束END--
本文标题: LAMP平台搭建
本文链接: https://www.lsjlt.com/news/385603.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0