lamp架构部署
lamp架构组成,如下图:
1、mysql
由于上一章nginx负载均衡,nginx已经部署好了,不需要部署,现在我们需要部署mysql数据库,企业常用还是5.7版本,此次部署 我们用5.7版本
1、1mysql下载
网上下载方法: 百度上访问www.mysql.com点击download
1、2 mysql编译及安装:
[root@foundation50 lamp]# evince rhel6\ lanmp.pdf & 编译时可以查看文档
编译参数,编译时可参考
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
#安装目录
-DMYSQL_DATADIR=/usr/local/mysql/data \
#数据库存放目录
-DMYSQL_UNIX_ADDR=/usr/local/mysql/data/mysql.sock \ #Unix socket 文件路径
-DWITH_MYISAM_STORAGE_ENGINE=1 \
#安装 myisam 存储引擎
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
#安装 innodb 存储引擎
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
#安装 archive 存储引擎
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
#安装 blackhole 存储引擎
-DWITH_PARTITION_STORAGE_ENGINE=1 \
#安装数据库分区
-DENABLED_LOCAL_INFILE=1 \
#允许从本地导入数据
-DWITH_READLINE=1 \
#快捷键功能
-DWITH_SSL=yes \
#支持 SSL
-DDEFAULT_CHARSET=utf8 \
#使用 utf8 字符
-DDEFAULT_COLLATION=utf8_general_ci \
#校验字符
-DEXTRA_CHARSETS=all \
#安装所有扩展字符集
-DMYSQL_TCP_PORT=3306 \
#MySQL 监听端口
[root@foundation50 lamp]# scp mysql-boost-5.7.31.tar.gz server1:/mnt 将下载好的包拷贝到server1里/mnt目录
[root@server1 mnt]# tar zxf mysql-boost-5.7.31.tar.gz 解压
[root@server1 mysql-5.7.31]# cd mysql-5.7.31/ 进入解压目录
[root@server1 mysql-5.7.31]# yum install cmake -y 安装cmake
[root@server1 mysql-5.7.31]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DMYSQL_UNIX_ADDR=/usr/local/mysql/data/mysql.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all 编译,报错
[root@server1 boost]# cd boost_1_59_0/ 可以发现原码里面有这个目录
[root@server1 boost_1_59_0]# ls
boost
[root@server1 boost_1_59_0]# pwd
/mnt/mysql-5.7.31/boost/boost_1_59_0 将这个路经加入到 -DWITH_BOOST模块里
重新编译
[root@server1 mysql-5.7.31]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DMYSQL_UNIX_ADDR=/usr/local/mysql/data/mysql.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DWITH_BOOST=/mnt/mysql-5.7.31/boost/boost_1_59_0
报错 ,缺少c++编译器
[root@server1 mysql-5.7.31]# yum install gcc-c++ -y 安装c++
重新编译
[root@server1 mysql-5.7.31]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DMYSQL_UNIX_ADDR=/usr/local/mysql/data/mysql.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DWITH_BOOST=/mnt/mysql-5.7.31/boost/boost_1_59_0
报错,却少依赖性
[root@server1 mysql-5.7.31]# yum install ncurses-devel -y 安装依赖性
重新编译
[root@server1 mysql-5.7.31]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DMYSQL_UNIX_ADDR=/usr/local/mysql/data/mysql.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DWITH_BOOST=/mnt/mysql-5.7.31/boost/boost_1_59_0
报错,提示删除cmakecache.txt文件
[root@server1 mysql-5.7.31]# rm -f CMakeCache.txt 删除文件
重新编译
[root@server1 mysql-5.7.31]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DMYSQL_UNIX_ADDR=/usr/local/mysql/data/mysql.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DWITH_BOOST=/mnt/mysql-5.7.31/boost/boost_1_59_0
提示Bison找不到
[root@server1 mysql-5.7.31]# yum install bison -y 安装
重新编译,成功
[root@server1 mysql-5.7.31]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DMYSQL_UNIX_ADDR=/usr/local/mysql/data/mysql.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DWITH_BOOST=/mnt/mysql-5.7.31/boost/boost_1_59_0
[root@server1 mysql]# make 编译
[root@server1 mysql]# make install 安装
[root@server1 support-files]# cp mysql.server /etc/init.d/ 将启动脚本拷贝到/etc/init.d/ 目录
[root@server1 support-files]# vim /etc/my.cnf 编辑全局mysql配置文件
[mysqld]
datadir=/usr/local/mysql/data 数据目录
socket=/usr/local/mysql/data/mysql.sock socket目录
symbolic-links=0
[root@server1 ~]# useradd -M -d /usr/local/mysql -s /sbin/nologin mysql 创建mysql用户,无需登陆系统
[root@server1 ~]# vim .bash_profile 添加mysql变量
[root@server1 ~]# source .bash_profile 变量生效
[root@server1 ~]# mysqld --initialize --user=mysql msql初始化,并指定运行的的用户
[root@server1 init.d]# mv mysql.server mysqld 为了方便给启动脚本起个名字mysqld
[root@server1 init.d]# mv mysql.server mysqld 运行启动脚本
[root@server1 init.d]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/usr/local/mysql/data/server1.err'.
SUCCESS! 启动成功
[root@server1 init.d]# netstat -antlp 查看端口,端口3306已经打开
[root@server1 init.d]# mysql_secure_installation nysql安装配置向导
Securing the MySQL server deployment.
Enter password for user root: 输入生成的密码
The existing password for the user account root has expired. Please set a new password.
New password: 输入密码
Re-enter new password:
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No: 回车
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : 回车
... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done!
[root@server1 init.d]# mysql -pwestos 登陆数据库
2、 PHP编译及安装
[root@foundation50 lamp]# scp php-7.4.12.tar.bz2 server1:/mnt
root@server1's password:
php-7.4.12.tar.bz2 100% 12MB
[root@server1 mnt]# yum install -y bzip2
[root@server1 mnt]# tar jxf php-7.4.12.tar.bz2 解压
php编译:
[root@server1 mnt]# cd php-7.4.12/
[root@server1 php-7.4.12]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mhash --with-zlib --with-openssl --enable-mysqlnd --with-pdo-mysql --with-mysqli --disable-debug --enable-sockets --enable-soap --enable-inline-optimization --enable-xml --enable-ftp --enable-gd --enable-mbstring --enable-bcmath --with-fpm-systemd 执行
--with-config-file-path 表示指定php主配置文件目录
--enable-fpm 表示激活fstcgi管理器fpm
--with-fpm-user 表示指定php运行时以什么样身份运行
-with-fpm-group 表示php运行时以什么样的组运行
--with-MySQL=/usr/local/mysql mysql安装目录,对mysql的支持
--with-mysqli=/usr/local/mysql/bin/mysql_config
mysqli扩展技术不仅可以调用MySQL的存储过程、处理MySQL事务,而且还可以使访问数据库工作变得更加稳定。
报错:
[root@server1 php-7.4.12]# yum install systemd-devel -y 安装
[root@server1 php-7.4.12]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mhash --with-zlib --with-openssl --enable-mysqlnd --with-pdo-mysql --with-mysqli --disable-debug --enable-sockets --enable-soap --enable-inline-optimization --enable-xml --enable-ftp --enable-gd --enable-mbstring --enable-bcmath --with-fpm-systemd 继续执行
报错:
[root@server1 php-7.4.12]# yum install libxml2-devel.x86_64 -y 安装
[root@server1 php-7.4.12]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mhash --with-zlib --with-openssl --enable-mysqlnd --with-pdo-mysql --with-mysqli --disable-debug --enable-sockets --enable-soap --enable-inline-optimization --enable-xml --enable-ftp --enable-gd --enable-mbstring --enable-bcmath --with-fpm-systemd 继续执行
报错:
[root@server1 php-7.4.12]# yum install sqlite-devel.x86_64 -y 安装
[root@server1 php-7.4.12]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mhash --with-zlib --with-openssl --enable-mysqlnd --with-pdo-mysql --with-mysqli --disable-debug --enable-sockets --enable-soap --enable-inline-optimization --enable-xml --enable-ftp --enable-gd --enable-mbstring --enable-bcmath --with-fpm-systemd 继续执行
报错:
[root@server1 php-7.4.12]# yum install libpng-devel.x86_64 -y 安装
[root@server1 php-7.4.12]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mhash --with-zlib --with-openssl --enable-mysqlnd --with-pdo-mysql --with-mysqli --disable-debug --enable-sockets --enable-soap --enable-inline-optimization --enable-xml --enable-ftp --enable-gd --enable-mbstring --enable-bcmath --with-fpm-systemd 继续执行
oniguruma系统不自带,所以需要去网上第三方下载
[root@foundation50 lamp]# scp oniguruma-* server1:/mnt 将下载好的包拷贝 到server1上
[root@server1 mnt]# yum install oniguruma-* -y 安装
[root@server1 mnt]# cd php-7.4.12/
[root@server1 php-7.4.12]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mhash --with-zlib --with-openssl --enable-mysqlnd --with-pdo-mysql --with-mysqli --disable-debug --enable-sockets --enable-soap --enable-inline-optimization --enable-xml --enable-ftp --enable-gd --enable-mbstring --enable-bcmath --with-fpm-systemd 继续执行
[root@server1 php-7.4.12]# make 编译
[root@server1 php-7.4.12]# make install 安装
root@server1 ~]# vim .bash_profile 添加php环境变量
[root@server1 ~]# source .bash_profile 使环境变量生效
[root@server1 etc]# cd /usr/local/php/etc/ 进入主配置目录
[root@server1 etc]# ls
php-fpm.conf.default php-fpm.d
[root@server1 etc]# cp php-fpm.conf.default php-fpm.conf 复制php-fpm.conf.default生成配置文件
[root@server1 etc]# cd php-fpm.d/ 进入原码
[root@server1 php-fpm.d]# cp www.conf.default www.conf 生成配置文件
[root@server1 php-7.4.12]# cp php.ini-production /usr/local/php/etc/php.ini 生成生产环境主配置文件
[root@server1 fpm]# cp init.d.php-fpm /etc/init.d/php-fpm 拷贝运行运行脚本
[root@server1 fpm]# chmod +x /etc/init.d/php-fpm 加上执行权限
[root@server1 fpm]# /etc/init.d/php-fpm start 启动php
Starting php-fpm done 启动成功
还有另一种启动方式:
[root@server1 fpm]# /etc/init.d/php-fpm stop 先停止启动
Gracefully shutting down php-fpm . done
[root@server1 fpm]# cp php-fpm.service /usr/lib/systemd/system/ 拷贝php-fpm.services 到system目录
[root@server1 fpm]# systemctl daemon-reload 重载服务
[root@server1 fpm]# systemctl start php-fpm 启动,发现报错
Job for php-fpm.service failed because the control process exited with error code. See "systemctl status php-fpm.service" and "journalctl -xe" for details.
[root@server1 fpm]# cat /var/log/messages 查看日志
[root@server1 system]# cd /usr/lib/systemd/system
[root@server1 system]# vim php-fpm.service 编辑启动脚本
[root@server1 system]# systemctl daemon-reload 重载服务
[root@server1 system]# systemctl start php-fpm 再次启动,成功
php设置时区
[root@server1 system]# cd /usr/local/php/etc 进入主配置目录
[root@server1 etc]# vim php.ini 编辑php主配置文件
[root@server1 etc]# systemctl reload php-fpm.service 重载服务
3、nginx、php做整合
[root@server1 etc]# cd /usr/local/nginx/conf/
[root@server1 conf]# vim nginx.conf
[root@server1 conf]# nginx -s reload 重载nginx服务
做一个nginx开机自启 直接去百度搜nginx systemd,复制即可,如下:
[root@server1 conf]# cd /usr/lib/systemd/system
[root@server1 system]# vim nginx.service 创建nginx服务
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@server1 system]# systemctl enable nginx.service 设置开机自启成功
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
测试:
[root@server1 etc]# cd /usr/local/nginx/
[root@server1 nginx]# cd html/ 进入nginx默认发布目录
[root@server1 html]# vim index.php 创建一个动态页面
<?php
phpinfo()
?>
访问172.25.50.1/index.php 访问成功
整合mysql
[root@server1 ~]# cd /usr/local/php/etc/ 进入php配置目录
[root@server1 etc]# vim php.ini 编辑主配置文件
`[root@server1 etc]# systemctl reload php-fpm.service` 重载服务
测试:
[root@foundation50 lamp]# scp phpMyAdmin-5.0.2-all-languages.zip server1:/mnt 拷贝下载的mysql管理工具包到server1的/mnt下
root@server1's password:
phpMyAdmin-5.0.2-all-languages.zip 100% 14MB 73.1MB/s 00:00
[root@server1 mnt]# unzip phpMyAdmin-5.0.2-all-languages.zip 解压
[root@server1 mnt]# mv phpMyAdmin-5.0.2-all-languages /usr/local/nginx/html/phpadmin 将解压的文件拷贝到nginx默认发布目录
[root@server1 conf]# cd /usr/local/nginx/conf/ 进入nginx配置目录
[root@server1 conf]# vim nginx.conf 编辑配置文件
添加index.php在最前,表示nginx最先访问index.php页面
[root@server1 conf]# nginx -s reload 重载nginx服务
访问 172.25.50.1/phpadmin
输入用户名和密码,无法登陆,没有权限
[root@server1 conf]# ll /usr/local/mysql/data/mysql.sock 查看mysql.sock 文件,权限好着
srwxrwxrwx 1 mysql mysql 0 Dec 13 22:38 /usr/local/mysql/data/mysql.sock
[root@server1 conf]# ll /usr/local/mysql/data/ -d 查看目录权限,发现mysql对于其他用户不能执行,php和ninx都是nginx用户
drwxr-x--- 5 mysql mysql 4096 Dec 13 22:38 /usr/local/mysql/data/
[root@server1 conf]# chmod 755 /usr/local/mysql/data/ 添加权限
访问 172.25.50.1/phpadmin ,访问成功
|