文章目录
源码编译安装部署lamp
环境
操作系统 | Apache版本 | MySQL版本 | PHP版本 |
---|---|---|---|
centos-8.2 | httpd-2.4.57 | mysql-5.7.39 | php-8.2.9 |
1.安装依赖包
[root@centos-8-3 ~]# yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make wget
2.下载和安装apr以及apr-util
//下载安装apr
[root@centos-8-3 ~]# wget https://downloads.apache.org/apr/apr-1.7.4.tar.gz
[root@centos-8-3 ~]# ls
anaconda-ks.cfg apr-1.7.4.tar.gz
[root@centos-8-3 ~]# tar -xf apr-1.7.4.tar.gz
[root@centos-8-3 ~]# ls
anaconda-ks.cfg apr-1.7.4 apr-1.7.4.tar.gz
//注释掉此行
[root@centos-8-3 ~]# cd apr-1.7.4/
[root@centos-8-3 apr-1.7.4]# vim configure
cfgfile=${ofile}T
trap "$RM \"$cfgfile\"; exit 1" 1 2 15
# $RM "$cfgfile" //注释此行
//配置apr
[root@centos-8-3 apr-1.7.4]# ./configure --prefix=/usr/local/apr
//编译和安装apr
[root@centos-8-3 apr-1.7.4]# make && make install
//下载安装apr-util
[root@centos-8-3 ~]# wget https://downloads.apache.org/apr/apr-util-1.6.3.tar.gz
[root@centos-8-3 ~]# tar -xf apr-util-1.6.3.tar.gz
[root@centos-8-3 ~]# ls
anaconda-ks.cfg apr-1.7.4.tar.gz apr-util-1.6.3.tar.gz
apr-1.7.4 apr-util-1.6.3
//配置apr-util
[root@centos-8-3 apr-util-1.6.3]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
//编译和安装
[root@centos-8-3 apr-util-1.6.3]# make && make install
3.编译安装httpd
//创建apache用户
[root@centos-8-3 ~]# groupadd -r apache
[root@centos-8-3 ~]# useradd -r -M -s /sbin/nologin -g apache apache
//下载最新版httpd源码包
[root@centos-8-3 ~]# wget https://downloads.apache.org/httpd/httpd-2.4.57.tar.gz
//配置httpd
[root@centos-8-3 ~]# tar -xf httpd-2.4.57.tar.gz
[root@centos-8-3 ~]# cd httpd-2.4.57/
[root@centos-8-3 httpd-2.4.57]# ./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@centos-8-3 httpd-2.4.57]# make && make install
(过程省略)
安装后配置
//永久配置环境变量
[root@centos-8-3 ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[root@centos-8-3 ~]# bash
//链接头文件
[root@centos-8-3 ~]# ln -s /usr/local/apache/include/ /usr/include/httpd
//指定man文件路径
[root@centos-8-3 ~]# echo 'MANPATH /usr/local/apache/man' >> /etc/man.config
//取消ServerName前面的注释
[root@centos-8-3 ~]# sed -i '/#ServerName/s/#//g' /etc/httpd24/httpd.conf
启动httpd
//80端口没有起来
[root@centos-8-3 ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
//启动
[root@centos-8-3 ~]# apachectl start
//80端口已经起来了
[root@centos-8-3 ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 511 *:80 *:*
4.二进制安装mysql
mysql的二进制包可以在https://pkgs.org上面去下载
创建mysql用户和组
[root@centos-8-3 ~]# groupadd -r -g 306 mysql
[root@centos-8-3 ~]# useradd -r -M -s /sbin/nologin -g 306 -u 306 mysql
//这里已经事先将mysql二进制包下载到了自己电脑上,然后传到Linux系统里面
//解压二进制包到/usr/local/
[root@centos-8-3 ~]# tar -xf mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@centos-8-3 ~]# ls /usr/local/
apache bin include libexec share
apr etc lib mysql-5.7.39-linux-glibc2.12-x86_64 src
apr-util games lib64 sbin
[root@centos-8-3 ~]#
//创建链接
[root@centos-8-3 ~]# cd /usr/local/
[root@centos-8-3 local]# ls
apache bin include libexec share
apr etc lib mysql-5.7.39-linux-glibc2.12-x86_64 src
apr-util games lib64 sbin
[root@centos-8-3 local]# ln -sv mysql-5.7.39-linux-glibc2.12-x86_64/ mysql
'mysql' -> 'mysql-5.7.39-linux-glibc2.12-x86_64/'
[root@centos-8-3 local]# ll
total 0
drwxr-xr-x. 13 root root 152 Aug 28 15:50 apache
drwxr-xr-x. 6 root root 58 Aug 28 15:23 apr
drwxr-xr-x. 5 root root 43 Aug 28 15:28 apr-util
drwxr-xr-x. 2 root root 6 Jun 22 2021 bin
drwxr-xr-x. 2 root root 6 Jun 22 2021 etc
drwxr-xr-x. 2 root root 6 Jun 22 2021 games
drwxr-xr-x. 2 root root 6 Jun 22 2021 include
drwxr-xr-x. 2 root root 6 Jun 22 2021 lib
drwxr-xr-x. 3 root root 17 Aug 28 09:46 lib64
drwxr-xr-x. 2 root root 6 Jun 22 2021 libexec
lrwxrwxrwx. 1 root root 36 Aug 28 16:31 mysql -> mysql-5.7.39-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root root 129 Aug 28 16:28 mysql-5.7.39-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 Jun 22 2021 sbin
drwxr-xr-x. 5 root root 49 Aug 28 09:46 share
drwxr-xr-x. 2 root root 6 Jun 22 2021 src
[root@centos-8-3 local]#
//修改目录/usr/local/mysql的属主属组
[root@centos-8-3 local]# chown -R mysql:mysql /usr/local/mysql
[root@centos-8-3 local]# ll /usr/local/mysql
lrwxrwxrwx. 1 mysql mysql 36 Aug 28 16:31 /usr/local/mysql -> mysql-5.7.39-linux-glibc2.12-x86_64/
[root@centos-8-3 local]#
//添加环境变量
[root@centos-8-3 local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@centos-8-3 local]# source /etc/profile.d/mysql.sh
[root@centos-8-3 local]# echo $PATH
/usr/local/mysql/bin:/usr/local/apache/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@centos-8-3 local]#
//建立数据存放目录
[root@centos-8-3 local]# mkdir /opt/data
[root@centos-8-3 local]# chown -R mysql.mysql /opt/data/
[root@centos-8-3 local]# ll /opt/
total 0
drwxr-xr-x. 2 root root 30 Aug 28 15:12 aliyun
drwxr-xr-x. 2 mysql mysql 6 Aug 28 16:50 data
drwxr-xr-x. 2 root root 6 Aug 28 15:05 yumbackup
[root@centos-8-3 local]#
//初始化数据库
[root@centos-8-3 ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2023-08-28T08:50:45.448280Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2023-08-28T08:50:45.587156Z 0 [Warning] InnoDB: New log files created, LSN=45790
2023-08-28T08:50:45.608931Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2023-08-28T08:50:45.663550Z 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: fa3ea031-457f-11ee-b2ed-000c29b9e9eb.
2023-08-28T08:50:45.664294Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2023-08-28T08:50:45.806139Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2023-08-28T08:50:45.806172Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2023-08-28T08:50:45.806466Z 0 [Warning] CA certificate ca.pem is self signed.
2023-08-28T08:50:45.815366Z 1 [Note] A temporary password is generated for root@localhost: hkP!oaHjQ5_/ //临时密码“hkP!oaHjQ5_/”
//配置mysql
[root@centos-8-3 ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
'/usr/local/include/mysql' -> '/usr/local/mysql/include/'
[root@centos-8-3 ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@centos-8-3 ~]# ldconfig
[root@centos-8-3 ~]#
//生成配置文件
[root@centos-8-3 ~]# vim /etc/my.cnf
[root@centos-8-3 ~]# cat /etc/my.cnf
[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
[root@centos-8-3 ~]#
//配置服务启动脚本
[root@centos-8-3 ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@centos-8-3 ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@centos-8-3 ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
//启动mysql
[root@centos-8-3 ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/centos-8-3.err'.
SUCCESS!
[root@centos-8-3 ~]# ps -ef|grep mysql
root 55927 1 0 16:58 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql 56115 55927 1 16:58 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=centos-8-3.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root 56145 54752 0 16:59 pts/0 00:00:00 grep --color=auto mysq
[root@centos-8-3 ~]#
[root@centos-8-3 ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 511 *:80 *:*
//链接库文件
[root@centos-8-3 ~]# ln -s /usr/lib64/libtinfo.so.6 /usr/lib64/libtinfo.so.5
[root@centos-8-3 ~]# ln -s /usr/lib64/libncurses.so.6 /usr/lib64/libncurses.so.5
//修改密码,先用临时命令登录
[root@centos-8-3 ~]# mysql -uroot -p
Enter password: //初始化数据库的时候会生成一个随机的临时密码,在上面可以看到
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.39
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> set password = password('12345678');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye
[root@centos-8-3 ~]#
5.编译安装php
安装PHP所需要的依赖
//先添加一个yum仓库
[root@centos-8-3 yum.repos.d]# vim CentOS-Stream-everything.repo
[Everything]
name=everything
baseurl=https://dl.fedoraproject.org/pub/epel/8/Everything/x86_64/
enabled=1
gpgcheck=0
[good]
name=good
baseurl=http://rpms.remirepo.net/enterprise/8/remi/x86_64/
enabled=1
gpgcheck=0
//安装依赖
[root@centos-8-3 ~]# 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 php72-php-mysqlnd sqlite-devel libzip libzip-devel
(省略)
[root@centos-8-3 ~]# cd
[root@centos-8-3 ~]# wget https://github.com/kkos/oniguruma/archive/v6.9.4.tar.gz -O oniguruma-6.9.4.tar.gz
--2023-08-28 20:51:06-- https://github.com/kkos/oniguruma/archive/v6.9.4.tar.gz
Resolving github.com (github.com)... 192.30.255.113
Connecting to github.com (github.com)|192.30.255.113|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://codeload.github.com/kkos/oniguruma/tar.gz/refs/tags/v6.9.4 [following]
--2023-08-28 20:51:07-- https://codeload.github.com/kkos/oniguruma/tar.gz/refs/tags/v6.9.4
Resolving codeload.github.com (codeload.github.com)... 192.30.255.120
Connecting to codeload.github.com (codeload.github.com)|192.30.255.120|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/x-gzip]
Saving to: ‘oniguruma-6.9.4.tar.gz’
oniguruma-6.9.4.ta [ <=> ] 568.94K 14.3KB/s in 24s
2023-08-28 20:51:32 (24.1 KB/s) - ‘oniguruma-6.9.4.tar.gz’ saved [582597]
[root@centos-8-3 ~]# tar -xf oniguruma-6.9.4.tar.gz
[root@centos-8-3 ~]# cd oniguruma-6.9.4/
[root@centos-8-3 oniguruma-6.9.4]# ./autogen.sh
(省略)
[root@centos-8-3 oniguruma-6.9.4]# ./configure --prefix=/usr --libdir=/lib64
(省略)
[root@centos-8-3 oniguruma-6.9.4]# make && make install
(省略)
下载php的二进制包,解压
[root@centos-8-3 ~]# wget https://www.php.net/distributions/php-8.2.9.tar.gz
[root@centos-8-3 ~]# tar -xf php-8.2.9.tar.gz
[root@centos-8-3 ~]# cd php-8.2.9/
[root@centos-8-3 php-8.2.9]#
配置
[root@centos-8-3 php-8.2.9]# ./configure --prefix=/usr/local/php8 \
--with-config-file-path=/etc \
--enable-fpm \
--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-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
(配置过程省略)
编译和安装
[root@centos-8-3 php-8.2.9]# make -j4 && make install
(过程省略)
安装后配置
//配置环境变量
[root@centos-8-3 php-8.2.9]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php8.sh
[root@centos-8-3 php-8.2.9]# bash
[root@centos-8-3 php-8.2.9]# which php
/usr/local/php8/bin/php
[root@centos-8-3 php-8.2.9]# php -v
PHP 8.2.9 (cli) (built: Aug 28 2023 21:05:28) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.9, Copyright (c) Zend Technologies
[root@centos-8-3 php-8.2.9]#
配置php-fpm
[root@centos-8-3 php-8.2.9]# cp php.ini-production /etc/php.ini
[root@centos-8-3 php-8.2.9]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@centos-8-3 php-8.2.9]# chmod +x /etc/rc.d/init.d/php-fpm
[root@centos-8-3 php-8.2.9]# cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
[root@centos-8-3 php-8.2.9]# cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf
编辑php-fpm的配置文件
手动添加
[root@centos-8-3 php-8.2.9]# vim /usr/local/php8/etc/php-fpm.conf
[root@centos-8-3 php-8.2.9]# tail -4 /usr/local/php8/etc/php-fpm.conf
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
启动php-fpm
[root@centos-8-3 php-8.2.9]# service php-fpm start
Starting php-fpm done
[root@centos-8-3 php-8.2.9]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 2048 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 511 *:80 *:*
[root@centos-8-3 php-8.2.9]# ps -ef | grep php
root 203658 1 0 21:17 ? 00:00:00 php-fpm: master process (/usr/local/php8/etc/php-fpm.conf)
nobody 203659 203658 0 21:17 ? 00:00:00 php-fpm: pool www
nobody 203660 203658 0 21:17 ? 00:00:00 php-fpm: pool www
nobody 203661 203658 0 21:17 ? 00:00:00 php-fpm: pool www
nobody 203662 203658 0 21:17 ? 00:00:00 php-fpm: pool www
nobody 203663 203658 0 21:17 ? 00:00:00 php-fpm: pool www
root 203666 203588 0 21:18 pts/1 00:00:00 grep --color=auto php
[root@centos-8-3 php-8.2.9]#
加入systemctl管理
[root@centos-8-3 ~]# vim /usr/lib/systemd/system/php-fpm.service
[root@centos-8-3 ~]# cat /usr/lib/systemd/system/php-fpm.service
[Unit]
Description=php-fpm server daemon
After=network.targe
[Service]
Type=forking
ExecStart=/etc/rc.d/init.d/php-fpm start
ExecStop=/etc/rc.d/init.d/php-fpm stop
ExecReload=/bin/kill -HUP \$MAINPID
[Install]
WantedBy=multi-user.target" > /usr/lib/systemd/system/php-fpm.service
[root@centos-8-3 ~]# systemctl daemon-reload
6.配置apache
启用httpd的相关模块
[root@centos-8-3 php-8.2.9]# sed -i '/proxy_module/s/#//g' /etc/httpd24/httpd.conf
[root@centos-8-3 php-8.2.9]# sed -i '/proxy_fcgi_module/s/#//g' /etc/httpd24/httpd.conf
创建虚拟主机目录,写一个测试页面
[root@centos-8-3 ~]# mkdir /usr/local/apache/htdocs/cl.com
[root@centos-8-3 ~]# vim /usr/local/apache/htdocs/cl.com/index.php
[root@centos-8-3 ~]# cat /usr/local/apache/htdocs/cl.com/index.php
<?php
phpinfo();
?>
[root@centos-8-3 ~]# chown -R apache.apache /usr/local/apache/htdocs/
[root@centos-8-3 ~]# ll /usr/local/apache/htdocs/ -d
drwxr-xr-x. 3 apache apache 38 Aug 28 21:37 /usr/local/apache/htdocs/
创建主机
//在最后添加以下内容
[root@centos-8-3 ~]# vim /etc/httpd24/httpd.conf
<VirtualHost *:80>
DocumentRoot "/usr/local/apache/htdocs/cl.com"
ServerName www.cl.com
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/cl.com/$1
<Directory "/usr/local/apache/htdocs/cl.com">
Options none
AllowOverride none
Require all granted
</Directory>
</VirtualHost>
//添加两行
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php //此行
AddType application/x-httpd-php-source .phps //此行
//添加index.php优先
<IfModule dir_module>
DirectoryIndex index.php index.html //添加一个index.php
</IfModule>
重启httpd服务
[root@centos-8-3 ~]# apachectl stop
[root@centos-8-3 ~]# apachectl start
[root@centos-8-3 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 2048 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 511 *:80 *:*
[root@centos-8-3 ~]#
关闭防火墙和selinux
[root@centos-8-3 ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@centos-8-3 ~]# setenforce 0
在真机的C:\Windows\System32\drivers\etc\hosts里写入ip和域名的绑定