lamp = Linux/unix/windows + apache/nginx/... + mysql/pgsql + php/python/golang
(lamp架构涉及到的都是开源软件)
1. NGINX的部署
Apache和NGINX任意选择
- 先解压
(((((解压后是源码,nginx是C语言,需要源码编译(安装gcc) ))))
[root@server1 test]# tar zxf nginx-1.18.0.tar.gz
-
进入nginx的目录,如果看见目录中有configure脚本,说明是gmake
gmake:GNU组织的make,开源的
cmake:商业化 -
configure脚本的介绍
configure --help
,可以看见很多参数(这些都是开源软件的定制)
(不像rpm包,不需要告诉它rpm安装在哪里)
--help
里的--without
代表默认有的选项,如果在脚本后加上这个参数,代表把它去掉
--with
代表没有激活的功能
运行后的刷屏操作是检测当前的环境,有没有符合它的需求; -
解决依赖性
nginx安装到/usr/local/nginx
下,并支持ssl,支持http状态监控
[root@server1 test]# cd nginx-1.18.0/
[root@server1 nginx-1.18.0]# yum install gcc pcre-devel openssl-devel
[root@server1 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
因为依赖性出现的错误:
第1个错误:没有安装gcc;
第2个错误:PCRE library
不符合要求,这个库用于网站的重写(重定向,比如,curl -I 163.com
,在访问域名时,重定向到www上);
解决办法:安装pcre-devel
库文件,在Redhat里,所有的开发库文件都是以-devel
结尾;
第3个错误:http状态监控需要Openssl library
的支持,在Linux上都是用openssl来做加密的;
解决办法:安装openssl-devel
库
configure命令是为了在当前环境生成Makefile
(保证一定是在第一层解压路径/test/nginx-1.18.0
)
configure
配置的所有参数,都写到了/test/nginx-1.18.0/Makefile
后续的make
操作,都是按照Makefile
文件来进行编译(会在当前目录编译生成符合当前系统的二进制程序)
接着的操作是make install
,编译好的程序在/test/nginx-1.18.0/objs/nginx
,同时也在/usr/local/nginx/sbin
- 优化二进制程序
这个二进制程序有点大,要进行优化,把bug去掉
删除之前的内容
[root@server1 nginx-1.18.0]# cd /usr/local/nginx/
[root@server1 nginx]# du -sh
5.8M .
[root@server1 nginx]# rm -fr * //删除,重新来
[root@server1 nginx]# ls
[root@server1 nginx]# du -sh
0 .
删除之前的nginx的目录,也可以执行make clean
命令
关闭debug
[root@server1 nginx]# cd - //回到源码包中
/test/nginx-1.18.0
[root@server1 nginx-1.18.0]# vim auto/cc/gcc
###注释172行
[root@server1 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-file-aio
[root@server1 nginx-1.18.0]# make //重新编译,当然也可以执行make clean 命令
[root@server1 nginx-1.18.0]# make install
[root@server1 nginx-1.18.0]# cd /usr/local/nginx/
[root@server1 nginx]# du -sh
984K .
- nginx的启动脚本
nginx二进制程序在/usr/local/nginx/sbin
下,但是它并不在系统的PATH
环境里面
解决办法:将这个路径加到自己的环境变量里面
[root@server1 nginx]# cd sbin/
[root@server1 sbin]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@server1 sbin]# vim /root/.bash_profile
[root@server1 sbin]# source /root/.bash_profile
[root@server1 sbin]# cat /root/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin:/usr/local/nginx/sbin //添加到这里
export PATH
[root@server1 sbin]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin:/usr/local/nginx/sbin
[root@server1 sbin]# which nginx
/usr/local/nginx/sbin/nginx
[root@server1 sbin]# nginx //直接启动
- 查看操作系统所开的端口,80端口是我们需要的端口(http)
[root@server1 sbin]# yum install -y net-tools //查看操作系统所开的端口
[root@server1 sbin]# netstat -antlp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 23580/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 3278/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 3448/master
tcp 0 0 172.25.23.1:46164 172.25.23.250:80 TIME_WAIT -
tcp 0 0 172.25.23.1:22 172.25.23.250:36684 ESTABLISHED 3541/sshd: root@pts
tcp6 0 0 :::22 :::* LISTEN 3278/sshd
tcp6 0 0 ::1:25 :::* LISTEN 3448/master
- 测试:
[root@server1 sbin]# curl localhost
firefox: 172.25.23.1
[root@server1 sbin]# curl 172.25.23.1
- 目录介绍
默认发布目录在/usr/local/nginx/html
下
一旦程序启动,就会在/usr/local/nginx
下生成很多的临时目录
conf:默认配置目录
logs:默认日志目录
html:默认发布目录
现在编辑文件,发布文件
[root@server1 sbin]# cd /usr/local/nginx/html/
[root@server1 html]# ls
50x.html index.html
[root@server1 html]# vim test.html
[root@server1 html]# cat test.html
hello!
[root@server1 html]# curl 172.25.23.1/test.html
hello!
- 已经开启了nginx就不能再次重复开启,否则,会报错
因为端口会被冲突了
-s :传参signal
关闭nginx之后,80端口就没有了
[root@server1 html]# nginx -s reload //重载
[root@server1 html]# nginx -s stop //关闭
2. MySQL的部署
使用MySQL社区版本
下载源码包
1. 解压
[root@server1 test]# tar zxf mysql-boost-5.7.31.tar.gz
[root@server1 test]# cd mysql-5.7.31/
[root@server1 mysql-5.7.31]# ls
boost extra mysys storage
BUILD include mysys_ssl strings
client INSTALL packaging support-files
cmake libbinlogevents plugin testclients
CMakeLists.txt libbinlogstandalone rapid unittest
cmd-line-utils libmysql README VERSION
config.h.cmake libmysqld regex vio
configure.cmake libservices scripts win
dbug LICENSE source_downloads zlib
Docs man sql
Doxyfile-perfschema mysql-test sql-common
- 没有
configure
,因为MySQL被Oracle收购之后,使用的是cmake
源码编译定制的能力是需要具备的,但是,在生产环境中是可以使用rpm包
定制:安装在哪里,拥有哪些功能,不需要并且关闭并哪些功能都由个人决定
(编译时,一定要在顶级路径里面)
[root@server1 mysql-5.7.31]# yum install -y cmake
[root@server1 mysql-5.7.31]# cd boost/boost_1_59_0/
[root@server1 boost_1_59_0]# pwd
/test/mysql-5.7.31/boost/boost_1_59_0
[root@server1 boost_1_59_0]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ //指定安装路径
-DMYSQL_DATADIR=/usr/local/mysql/data \ //数据库存放目录
-DMYSQL_UNIX_ADDR=/usr/local/mysql/data/mysql.sock \ //Unix socket 文件路径,如果不指定的话,会默认放到/tmp下
-DWITH_INNOBASE_STORAGE_ENGINE=1 \ //需要加载的数据库引擎,可以使用innodb存储引擎
-DDEFAULT_CHARSET=utf8 \ //受用utf8字符
-DDEFAULT_COLLATION=utf8_general_ci \ //检验字符
-DEXTRA_CHARSETS=all \ //安装所有扩展字符集
-DWITH_BOOST=/test/mysql-5.7.31/boost/boost_1_59_0
报错:
CMake Error: The source directory "/test/mysql-5.7.31/boost/boost_1_59_0" does not appear to contain CMakeLists.txt.
Specify --help for usage, or press the help button on the CMake GUI.
- 解决依赖性,回到指定目录去编译
[root@server1 boost_1_59_0]# yum install -y ncurses-devel.x86_64
[root@server1 mysql-5.7.31]# rm -fr 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=/test/mysql-5.7.31/boost/boost_1_59_0
[root@server1 mysql-5.7.31]# yum install -y gcc-c++
[root@server1 mysql-5.7.31]# rm -fr 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=/test/mysql-5.7.31/boost/boost_1_59_0
[root@server1 mysql-5.7.31]# yum install -y bison
[root@server1 mysql-5.7.31]# rm -fr 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=/test/mysql-5.7.31/boost/boost_1_59_0
- 编译,回到指定目录去编译
cmake VS gmake
cmake:有颜色,有进度
[root@server1 mysql-5.7.31]# make
需要向/usr/local/mysql
下连续写入数据
[root@server1 mysql-5.7.31]# ll -d /usr/local/mysql
ls: cannot access /usr/local/mysql: No such file or directory
[root@server1 mysql-5.7.31]# make install
- 拷贝,为MySQL进行初始化做准备
[root@server1 mysql-5.7.31]# cd /usr/local/mysql/
[root@server1 mysql]# du -sh
1.9G .
[root@server1 mysql]# ls
bin include LICENSE mysql-test README-test support-files
docs lib man README share
[root@server1 mysql]# cd support-files/
[root@server1 support-files]# ls
magic mysqld_multi.server mysql-log-rotate mysql.server
[root@server1 support-files]# cp mysql.server /etc/init.d/mysqld //脚本
[root@server1 support-files]# ll /etc/init.d/mysqld
-rwxr-xr-x 1 root root 10576 Mar 30 10:44 /etc/init.d/mysqld //脚本给权限
######[root@server1 mysql-5.7.31]# cd mysql-test/ //没有用的操作
######[root@server1 mysql-test]# pwd
######/test/mysql-5.7.31/mysql-test
[root@server1 mysql-test]# vim /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data //数据目录的路径
socket=/usr/local/mysql/data/mysql.sock //编译的路径
symbolic-links=0
- 添加环境变量
MySQL的指令都在/usr/local/mysql/bin
目录里
[root@server1 mysql-test]# vim ~/.bash_profile
[root@server1 mysql-test]# source ~/.bash_profile
[root@server1 mysql-test]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/nginx/sbin:/root/bin:/usr/local/nginx/sbin:/usr/local/mysql/bin
- 在
/usr/local/mysql
下没有data
目录,需要做一个初始化
源码不会像rpm包会自动创建相应的用户,需要我们自己创建
MySQL不支持超级用户
[root@server1 mysql]# useradd -u 1001 -M -d /usr/local/mysql/data -s /sbin/nologin mysql
//不默认创建家目录,指定到/usr/local/mysql/data
自动读取/etc/my.cnf
,生成临时密码(初始化密码只允许登陆)
[root@server1 mysql]# mysqld --verbose --help | less
[root@server1 mysql]# mysqld --initialize --user=mysql
#####
2021-03-30T03:05:42.577373Z 1 [Note] A temporary password is generated for root@localhost: oPyNwZkp!2y.
[root@server1 mysql]# ll /usr/local/mysql/data //查看
[root@server1 mysql]# /etc/init.d/mysqld start //启动
Starting MySQL.Logging to '/usr/local/mysql/data/server1.err'.
SUCCESS!
[root@server1 mysql]# netstat -antuple | grep mysql
tcp6 0 0 :::3306 :::* LISTEN 1001 50802 2965/mysqld
改密码(不要检查密码的强壮度)
[root@server1 mysql]# mysql_secure_installation //改密码
启动MySQL,如果MySQL已经启动,就不要重复启动,以免socket冲突
[root@server1 mysql]# mysql -p
3. PHP的部署
- 解压
[root@server1 test]# yum install -y bzip2.x86_64
[root@server1 test]# tar jxf php-7.4.12.tar.bz2
- 定制,同时解决依赖性
首先,创建一个用户,
之前运行nginx的是nobody
创建一个名为nginx的用户,PHP和nginx都使用这个用户
修改mysql的用户和用户组
[root@server1 ~]# nginx //开启nginx
[root@server1 ~]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 3531 0.0 0.0 45868 1132 ? Ss 22:11 0:00 nginx: master p
nobody 3532 0.0 0.0 46312 1912 ? S 22:11 0:00 nginx: worker p
root 3533 0.0 0.0 155360 1884 pts/0 R+ 22:11 0:00 ps aux
[root@server1 ~]# useradd -M -d /usr/local/nginx/ -s /sbin/nologin nginx
[root@server1 ~]# id nginx
uid=1002(nginx) gid=1002(nginx) groups=1002(nginx)
[root@server1 ~]# cd /usr/local/nginx/
[root@server1 nginx]# cd conf/
[root@server1 conf]# ls
fastcgi.conf koi-win scgi_params
fastcgi.conf.default mime.types scgi_params.default
fastcgi_params mime.types.default uwsgi_params
fastcgi_params.default nginx.conf uwsgi_params.default
koi-utf nginx.conf.default win-utf
[root@server1 conf]# vim nginx.conf
[root@server1 conf]# cat nginx.conf
#######
user nginx nginx;
worker_processes 1;
#######
[root@server1 conf]# nginx -s reload
[root@server1 conf]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 3531 0.0 0.0 46004 1792 ? Ss 22:11 0:00 nginx: master p
nginx 3557 0.0 0.0 46432 2032 ? S 22:15 0:00 nginx: worker p
root 3558 0.0 0.0 155360 1884 pts/0 R+ 22:15 0:00 ps aux
在/test/php-7.4.12里存在configure,属于gmake
查看其参数
[root@server1 php-7.4.12]# ./configure --prefix=/usr/local/php \ //配置文件的路径
> --with-config-file-path=/usr/local/php/etc \ //配置目录
> --enable-mysqlnd \ //驱动
> --with-pdo-mysql \
> --with-mysqli \
> --with-openssl-dir \ //加密
> --enable-gd \ //开启图像
> --with-zlib-dir \ //压缩
> --with-curl \ //支持curl命令
> --with-pear \ //php重要扩展
> --enable-inline-optimization \ //优化选项
> --enable-soap \ //动态编译支持
> --enable-sockets \ //sockets支持
> --enable-mbstring \
> --enable-fpm \ //fpm管理器
> --with-fpm-user=nginx \ //用户身份,以什么身份运行,尽量和nginx保持一致
> --with-fpm-group=nginx \ //用户组身份
> --with-fpm-systemd
第1个错误:需要libsystemd的版本高于209
[root@server1 php-7.4.12]# yum install -y systemd-devel.x86_64
第2个错误:安装libxml2-devel
[root@server1 php-7.4.12]# yum install -y libxml2-devel
第3个错误:安装sqlite-devel
[root@server1 php-7.4.12]# yum install -y sqlite-devel.x86_64
第4个错误:安装libcurl
[root@server1 php-7.4.12]# yum install -y libcurl-devel
第5个错误:安装libpng-devel
[root@server1 php-7.4.12]# yum install -y libpng-devel
第6个错误:安装oniguruma
[root@server1 test]# yum install -y oniguruma-6.8.2-1.el7.x86_64.rpm oniguruma-devel-6.8.2-1.el7.x86_64.rpm
- 编译
[root@server1 php-7.4.12]# make
[root@server1 php-7.4.12]# make install
- 通过脚本重载php。在编译php时,configure后加了systemd,会生成systemctl的启动后台,但是,好像没有生成到我们应该在的位置
将启动systemd的脚本/test/php-7.4.12/sapi/fpm/init.d.php-fpm
拷贝到/etc/init.d
/下,
设定执行权限
这是在systemd
以前,最常用的启动方式
直接调用该脚本,系统会打印响应的帮助
[root@server1 php-7.4.12]# systemctl status php-fpm
Unit php-fpm.service could not be found.
[root@server1 php-7.4.12]# cd sapi/fpm/
[root@server1 fpm]# ls
config.m4 LICENSE php-fpm.conf status.html.in
CREDITS Makefile.frag php-fpm.conf.in tests
fpm php-fpm php-fpm.service www.conf
init.d.php-fpm php-fpm.8 php-fpm.service.in www.conf.in
init.d.php-fpm.in php-fpm.8.in status.html
[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]# ll /etc/init.d/php-fpm
-rwxr-xr-x 1 root root 2401 Apr 3 20:48 /etc/init.d/php-fpm
[root@server1 fpm]# /etc/init.d/php-fpm
Usage: /etc/init.d/php-fpm {start|stop|force-quit|restart|reload|status|configtest} //打印帮助
- 在安装路径里看到
php-fpm.conf.default
是php控制器的配置文件(模板)
拷贝其生成配置文件;
修改配置文件中PID的位置;
配置文件中全部都是注释的内容,要注意的是注释后面是否有默认值,该有的不能为空
[root@server1 fpm]# cd /usr/local/php/etc/ //安装路径
[root@server1 etc]# cp php-fpm.conf.default php-fpm.conf
[root@server1 etc]# vim php-fpm.conf
####
17 pid = run/php-fpm.pid //告诉系统,php的PID放在哪个位置
取消17行的注释
- 修改用户权限
查看到用户和用户组是nginx
[root@server1 etc]# cd php-fpm.d/
[root@server1 php-fpm.d]# ls
www.conf.default
[root@server1 php-fpm.d]# vim www.conf.default
23 user = nginx
24 group = nginx
- 查看
www.conf
用户身份正常,
默认监听的是本机127.0.0.1:9000
端口;
默认开启的服务数量最大是5,默认空闲1个;
[root@server1 php-fpm.d]# cp www.conf.default www.conf
[root@server1 php-fpm.d]# ls
www.conf www.conf.default
- 拷贝主配置文件,并启动php
php.ini-production
PHP的生产文件
PHP配置文件的命名方式一定是php.ini
[root@server1 php-7.4.12]# cd /test/php-7.4.12/
[root@server1 php-7.4.12]# cp php.ini-production /usr/local/php/etc/php.ini
[root@server1 php-7.4.12]# /etc/init.d/php-fpm start //启动PHP
Starting php-fpm done
[root@server1 php-7.4.12]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 24334 0.0 0.1 235960 6252 ? Ss 21:04 0:00 php-fpm: master
nginx 24335 0.0 0.1 235960 5796 ? S 21:04 0:00 php-fpm: pool w
nginx 24336 0.0 0.1 235960 5796 ? S 21:04 0:00 php-fpm: pool w
- PHP启动之后,用
ps
命令查看是否成功,netstat
命令查看PHP端口9000
[root@server1 php-7.4.12]# netstat -antuple
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 0 36671 24334/php-fpm: mast
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 0 23378 3266/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 0 24628 3349/master
tcp 0 0 172.25.23.1:22 172.25.23.250:35246 ESTABLISHED 0 24512 3513/sshd: root@pts
tcp6 0 0 :::22 :::* LISTEN 0 23380 3266/sshd
tcp6 0 0 ::1:25 :::* LISTEN 0 24629 3349/master