lamp部署(源码安装httpd+二进制安装MySQL+源码安装php)
目录
编译安装httpd
准备工作
[root@localhost ~]# yum groups mark install "Development Tools" //安装开发工具包
[root@localhost ~]# useradd -r -M -s /sbin/nologin apache
//创建apache服务的用户和组
[root@localhost ~]# id apache
uid=995(apache) gid=992(apache) groups=992(apache)
[root@localhost ~]# yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make
[root@localhost ~]# yum - y install make
源码包下载和解压
[root@localhost ~]# wget https://downloads.apache.org/httpd/httpd-2.4.53.tar.gz
[root@localhost ~]# wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz
[root@localhost ~]# wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz
[root@localhost ~]# tar -xf apr-1.7.0.tar.gz
[root@localhost ~]# tar -xf apr-util-1.6.1.tar.gz
[root@localhost ~]# tar -xf httpd-2.4.53.tar.gz
编译安装
//安装apr
[root@rookie ~]# cd apr-1.7.0
[root@rookie apr-1.7.0]# vim configure
# $RM "$cfgfile" //将此行加上注释,或者删除此行
[root@localhost ~]# cd apr-1.7.0/
[root@localhost apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@localhost apr-1.7.0]# make && make install
//安装apr-util
[root@localhost apr-1.7.0]# cd ../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 apr-util-1.6.1]# cd ../httpd-2.4.53/
[root@localhost httpd-2.4.53]# cd httpd-2.4.53/
[root@localhost httpd-2.4.53]# ./configure --prefix=/usr/local/apache --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.53]# make && make install
设置环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/apache.sh
[root@localhost ~]# source /etc/profile.d/apache.sh
[root@localhost ~]# which httpd
/usr/local/apache/bin/httpd
[root@localhost ~]# which httpd
/usr/local/apache/bin/httpd
[root@localhost ~]# ln -s /usr/local/apache/include/ /usr/include/apache
[root@localhost ~]# ll /usr/include/|grep apache
lrwxrwxrwx. 1 root root 26 7月 5 20:48 apache -> /usr/local/apache/include/
[root@localhost ~]# vim /etc/man_db.conf
#MANDATORY_MANPATH /usr/src/pvm3/man
#
MANDATORY_MANPATH /usr/man
MANDATORY_MANPATH /usr/share/man
MANDATORY_MANPATH /usr/local/share/man
MANDATORY_MANPATH /usr/local/apache/man //添加这一行
设置防火墙
[root@localhost ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
Permissive
[root@localhost ~]# vim /etc/selinux/config
[root@localhost ~]# cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
[root@localhost ~]# apachectl start
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*

使用systemctl命令设置httpd
使用源码包安装apache服务 默认是不能用systemctl的
任何源码安装的服务都适用
[root@localhost ~]# cd /usr/lib/systemd/system
[root@localhost system]# cp sshd.service httpd.service //复制一份此文件改名为httpd.service
[root@localhost system]# cat httpd.service //编辑此文件
[Unit]
Description=httpd server daemon //将Openssh修改为httpd
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start //更改为apachectl的路径 开启
ExecStop=/usr/local/apache/bin/apachectl stop //关闭
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@localhost ~]# systemctl daemon-reload //重启让其生效
二进制安装mysql
创建mysql用户和组
[root@localhost ~]# useradd -r -M -s /sbin/nologin mysql
解压mysql安装包
[root@localhost ~]# cd /usr/src/
[root@localhost src]# ls
debug kernels
[root@localhost src]# ls
debug kernels mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# tar -xf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost src]# cd
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ls
apache apr-util etc include lib64 mysql-5.7.37-linux-glibc2.12-x86_64 share
apr bin games lib libexec sbin src
[root@localhost local]# mv mysql-5.7.37-linux-glibc2.12-x86_64/ mysql
[root@localhost local]# ls
apache apr-util etc include lib64 mysql share
apr bin games lib libexec sbin src
[root@localhost local]# chown -R mysql.mysql mysql
[root@localhost local]# ll -ld mysql/
drwxr-xr-x. 9 mysql mysql 129 7月 5 21:13 mysql/
设置环境变量
[root@localhost local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost local]# source /etc/profile.d/mysql.sh
[root@localhost local]# which mysql
/usr/local/mysql/bin/mysql
[root@localhost local]# ln -s /usr/local/mysql/include/ /usr/include/mysql //设置软连接
[root@localhost local]# cat /etc/ld.so.conf.d/mysql.conf //配置lib库
/usr/local/mysql/lib
[root@localhost mysql]# vim /etc/man_db.conf //添加man文档
MANDATORY_MANPATH /usr/man
MANDATORY_MANPATH /usr/share/man
MANDATORY_MANPATH /usr/local/share/man
MANDATORY_MANPATH /usr/local/apache/man
MANDATORY_MANPATH /usr/local/mysql/man //添加此行
建立数据存放目录
[root@localhost ~]# mkdir -p /opt/data
[root@localhost ~]# cd /opt/
[root@localhost opt]# chown -R mysql.mysql data/
[root@localhost opt]# ll
总用量 0
drwxr-xr-x. 2 mysql mysql 6 7月 5 21:27 data
[root@localhost opt]# cd
[root@localhost ~]# mysqld --initialize --user mysql --datadir /opt/data/
2022-07-05T13:28:09.153428Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-05T13:28:09.302781Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-05T13:28:09.325863Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-05T13:28:09.385639Z 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: 4f97d12b-fc66-11ec-ade9-000c29f59753.
2022-07-05T13:28:09.388131Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-05T13:28:10.575980Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-05T13:28:10.575996Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-05T13:28:10.576487Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-05T13:28:10.702209Z 1 [Note] A temporary password is generated for root@localhost: nb/+a*7x;Hcm
生成配置文件
[root@localhost ~]# 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
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
配置服务启动脚本
[root@localhost ~]# cd /usr/local/mysql/
[root@localhost mysql]# ls
bin docs include lib LICENSE man README share support-files
[root@localhost mysql]# cd support-files/
[root@localhost support-files]# ls
magic mysqld_multi.server mysql-log-rotate mysql.server
[root@localhost support-files]# cp mysql.server mysqld
[root@localhost support-files]# vim mysqld //搜索datadir,在两行后添加引号内的内容
basedir="/usr/local/mysql"
datadir="/usr/local/data"
启动mysql
[root@localhost support-files]# /usr/local/mysql/support-files/mysqld start
Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'.
SUCCESS!
登录mysql时会报错
[root@localhost ~]# grep "password" /var/log/mysqld.log //查看临时密码
[root@localhost ~]# mysql -uroot -p'nb/+a*7x;Hcm'
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared ob
ject file: No such file or directory
翻译:
mysql: 加载共享库时出错: libncurses.so.5: 无法打开共享 ob
弹出文件:没有这样的文件或目录
解决方法
[root@localhost ~]# dnf provides libncurses.so.5
上次元数据过期检查:1:20:27 前,执行于 2022年07月05日 星期二 20时29分39秒。
ncurses-compat-libs-6.1-7.20180224.el8.i686 : Ncurses compatibility libraries
仓库 :baseos
匹配来源:
提供 : libncurses.so.5
ncurses-compat-libs-6.1-9.20180224.el8.i686 : Ncurses compatibility libraries
仓库 :baseos
匹配来源:
提供 : libncurses.so.5
[root@localhost ~]# install -y ncurses-compat-libs
再次登录mysql并修改密码
[root@localhost ~]# mysql -uroot -p'nb/+a*7x;Hcm'
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 5
Server version: 5.7.37
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('runtime.123!'); //修改密码
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye
[root@localhost ~]# mysql -uroot -p'runtime.123!' //新密码登录
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 6
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> quit //成功
Bye
[root@localhost ~]# cat pass //新建一个文件用于保存密码
runtime.123!
设置开机自启
[root@localhost support-files]# /usr/local/mysql/support-files/mysqld start
Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'.
SUCCESS!
[root@localhost ~]# cd /usr/lib/systemd/system
[root@localhost system]# cp sshd.service mysqld.service
[root@localhost system]# cat mysqld.service
[Unit]
Description=mysql server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysqld start
ExecStop=/usr/local/mysql/support-files/mysqld stop
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
[root@localhost system]# cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
[root@localhost system]# systemctl daemon-reload
[root@localhost system]# systemctl start mysqld.service
[root@localhost system]# systemctl enable --now mysqld.service
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
[root@localhost system]# systemctl status mysqld.service
● mysqld.service - mysql server daemon
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disa>
Active: activating (auto-restart) (Result: exit-code) since Tue 2022-07-05 22:08:08 >
Process: 554709 ExecStart=/usr/local/mysql/support-files/mysqld start (code=exited, s>
Main PID: 554723 (code=exited, status=1/FAILURE)
7月 05 22:08:08 localhost.localdomain systemd[1]: mysqld.service: Main process exited, >
7月 05 22:08:08 localhost.localdomain systemd[1]: mysqld.service: Failed with result 'e>
安装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
[root@localhost ~]# wget https://www.php.net/distributions/php-7.4.29.tar.xz //下载php的源码包
[root@localhost ~]# tar Jxf php-7.4.29.tar.xz //解压
编译php
[root@localhost ~]# cd php-7.4.29/
[root@localhost php-7.4.29]# ./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
[root@localhost ~]# make && make install //make编译
php编译时的报错
报错1 configure: error: Package requirements (libxml-2.0 >= 2.7.6) were not met:
Package 'libxml-2.0', required by 'virtual:world', not found
解决方法:
yum install libxml2-devel
报错2 configure: error: Package requirements (sqlite3 > 3.7.4) were not met:
Package 'sqlite3', required by 'virtual:world', not found
解决方法:
dnf -y install sqlite-devel
报错3
configure: error: Package requirements (oniguruma) were not met:
解决方法:
dnf -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
[root@localhost php-7.4.29]# cd ..
[root@localhost ~]# wget https://github.com/kkos/oniguruma/archive/v6.9.4.tar.gz -O oniguruma-6.9.4.tar.gz //下载此包
[root@localhost ~]# tar -zxf oniguruma-6.9.4.tar.gz //解压
[root@localhost ~]# cd oniguruma-6.9.4
[root@localhost ~]# ./autogen.sh && ./configure --prefix=/usr //设置存放位置
[root@localhost ~]# make && make install //make编译
报错4
configure: error: Package requirements (libzip >= 0.11 libzip != 1.3.1 libzi
解决方法:
dnf install -y libzip-devel
创建环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh //使php命令可以使用
[root@localhost ~]# source /etc/profile.d/php7.sh //读取 让其生效
[root@localhost ~]# which php
/usr/local/php7/bin/php
[root@localhost ~]# ln -s /usr/local/php7/include /usr/include/php
[root@localhost ~]# echo '/usr/local/php7/lib' > /etc/ld.so.conf.d/php.conf
[root@localhost ~]# ldconfig
[root@localhost ~]# php -v
PHP 7.4.29 (cli) (built: Jul 7 2022 18:46:04) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
配置php-fpm
[root@localhost ~]# cd php-7.4.29/
[root@localhost php-7.4.29]# cp php.ini-production /etc/php.ini
[root@localhost php-7.4.29]# cd sapi/
[root@localhost sapi]# cd fpm/
[root@localhost fpm]# ls
config.m4 init.d.php-fpm.in php-fpm.8 php-fpm.service tests
CREDITS LICENSE php-fpm.8.in php-fpm.service.in www.conf
fpm Makefile.frag php-fpm.conf status.html www.conf.in
init.d.php-fpm php-fpm php-fpm.conf.in status.html.in
[root@localhost fpm]# cp init.d.php-fpm /etc/init.d/php-fpm
[root@localhost fpm]# chmod +x /etc/init.d/php-fpm
[root@localhost fpm]# cd /usr/local/php7/etc/
[root@localhost etc]# ls
pear.conf php-fpm.conf.default php-fpm.d
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# cd php-fpm.d/
[root@localhost php-fpm.d]# ls
www.conf.default
[root@localhost php-fpm.d]# cp www.conf.default www.conf
[root@localhost php-fpm.d]# ls
www.conf www.conf.default
开启服务
[root@localhost ~]# service php-fpm start
Starting php-fpm done
配置apache
[root@localhost ~]# cd /usr/local/apache/conf/
[root@localhost conf]# vim httpd.conf
LoadModule proxy_module modules/mod_proxy.so //将这两行注释取消 启动这两个模块
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
创建测试文件
[root@localhost ~]# cd /usr/local/apache/htdocs/
[root@localhost htdocs]# mkdir test.com
[root@localhost htdocs]# cd test.com/
[root@localhost test.com]# cat index.php //使用vim创建index.php文件作为测试页面
<?php
phpinfo();
?>
#将此目录的属主和属组改为apache
[root@localhost ~]# cd /usr/local/apache/htdocs/
[root@localhost htdocs]# chown -R apache.apache /usr/local/apache
配置虚拟主机
[root@localhost conf]# pwd
/usr/local/apache/conf
[root@localhost conf]# vim httpd.conf
#搜索vhosts-vhosts.conf
Include conf/extra/httpd-vhosts.conf 取消这一行的注释"#"
#搜索AddType,添加以下内容
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php //添加这两行 //让apache支持php的页面
AddType application/x-httpd-php-source .phps //添加这两行
#搜索index.html
<IfModule dir_module>
DirectoryIndex index.php index.html //在其前面添加index.php 让网站能够访问到php类型
</IfModule>
[root@localhost conf]# pwd
/usr/local/apache/conf
[root@localhost conf]# cd extra/
[root@localhost extra]# ls
httpd-autoindex.conf httpd-languages.conf httpd-ssl.conf
httpd-dav.conf httpd-manual.conf httpd-userdir.conf
httpd-default.conf httpd-mpm.conf httpd-vhosts.conf
httpd-info.conf httpd-multilang-errordoc.conf proxy-html.conf
[root@localhost extra]# vim httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/usr/local/apache/htdocs" //网站位置
ServerName web.example.com //域名
ErrorLog "logs/web.example.com-error_log"
CustomLog "logs/web.example.com-access_log" common
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/test.com/$1 //网站位置
<Directory "/usr/local/apache/htdocs"> //网站位置
Options none
AllowOverride none
Require all granted
</Directory>
</VirtualHost>
为真机的host文件添加ip对应的域名做映射 (windows)
路径为C:\Windows\System32\drivers\etc
192.168.170.128 web.example.comm

重启服务
//重启时会有警告
[root@localhost ~]# vim /usr/local/apache/conf/httpd.conf
#搜索ServerName
ServerName www.example.com:80 取消此行注释
[root@localhost ~]# apachectl restart
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
验证
IP访问

域名访问



浙公网安备 33010602011771号