LNMP生产环境部署
静态资源
动态资源
一、编译安装 MySQL
MySQL 类型
1、MySQL Community Server - MySQL Community Server是社区版本,开源免费,但不提供官方技术支持。MySQL Community Server也是我们通常用的MySQL的版本。根据不同的操作系统平台细分为多个版本。 2、MySQL Enterprise Edition - MySQL Enterprise Edition企业版本,需付费,可以试用30天。 3、MySQL Cluster - MySQL Cluster集群版,开源免费。可将几个MySQL Server封装成一个Server。MySQL Cluster CGE 高级集群版,需付费。
MySQL 安装方式
1、yum 安装
-
优点:操作简单易用。不用单独下载,服务器可以联网且yum源没有问题即可(可以选择国内的163/阿里源)
2、编译安装
- 5.1.X 及之前的版本是通过下载tar包以后解压后进入软件包解压路径。然后./configure、make、make install - 5.4.X 到 5.7.X 通过下载tar包以后解压后进入软件包解压路径。然后 cmake、make、make install(cmake需要提前安装) 优点:可以定制功能特性。
3、二进制安装
官方下载二进制包,解压初始化即可直接使用不用安装。
4、rpm 安装
- 需要提前下载 rpm 软件包上传到服务器系统本地。 - 使用 rpm 或者 yum 命令直接安装
5、MySQL 版本说明
以 MySQL 5.7.27 这个版本的版本号为例说明每个数字含义。 - 第一个数字(5)主版本号:文件格式改动时,将作为新的版本发布; - 第二个数字(7)发行版本号:新增特性或者改动不兼容时,发行版本号需要更改; - 第三个数字(27)发行序列号:主要是小的改动,如bug的修复、函数添加或更改、配置参数的更改等。
mysql安装
关闭防火墙和selinux
1、编译安装mysql5.7
1.1、清理安装环境:
# yum erase mariadb mariadb-server mariadb-libs mariadb-devel -y # userdel -r mysql # rm -rf /etc/my* # rm -rf /var/lib/mysql
1.2、创建mysql用户
[root@mysql-server ~]# useradd mysql -M -s /bin/nologin -M 不创建用户的家目录 [root@mysql-server ~]# useradd -r mysql -s /bin/nologin -r 选项表示创建一个系统用户,也被称为系统账户。使用useradd -r命令创建系统用户时,会自动分配一个较高的用户ID(UID)和组ID(GID),并且不会为其创建家目录。
1.3、从官网下载tar包
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.27.tar.gz
1.4、安装编译工具
# yum -y install ncurses ncurses-devel openssl-devel bison gcc gcc-c++ make glibc automake autoconf cmake: # yum -y install cmake
1.5、解压
[root@mysql-server ~]# tar xzvf mysql-boost-5.7.27.tar.gz -C /usr/local/
1.6、编译安装
cd 解压的mysql目录 [root@mysql-server ~]# cd /usr/local/mysql-5.7.27/ [root@mysql-server mysql-5.7.27]# cmake . \ -DWITH_BOOST=boost/boost_1_59_0/ \ -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DSYSCONFDIR=/etc \ -DMYSQL_DATADIR=/usr/local/mysql/data \ -DINSTALL_MANDIR=/usr/share/man \ -DMYSQL_TCP_PORT=3306 \ -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \ -DDEFAULT_CHARSET=utf8 \ -DEXTRA_CHARSETS=all \ -DDEFAULT_COLLATION=utf8_general_ci \ -DWITH_READLINE=1 \ -DWITH_SSL=system \ -DWITH_EMBEDDED_SERVER=1 \ -DENABLED_LOCAL_INFILE=1 \ -DWITH_INNOBASE_STORAGE_ENGINE=1 参数详解: -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ 安装目录 -DSYSCONFDIR=/etc \ 配置文件存放 (默认可以不安装配置文件) -DMYSQL_DATADIR=/usr/local/mysql/data \ 数据目录 错误日志文件也会在这个目录 -DINSTALL_MANDIR=/usr/share/man \ 帮助文档 -DMYSQL_TCP_PORT=3306 \ 默认端口 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \ sock文件位置,用来做网络通信的,客户端连接服务器的时候用 -DDEFAULT_CHARSET=utf8 \ 默认字符集。字符集的支持,可以调 -DEXTRA_CHARSETS=all \ 扩展的字符集支持所有的 -DDEFAULT_COLLATION=utf8_general_ci \ 支持的 -DWITH_READLINE=1 \ 上下翻历史命令 -DWITH_SSL=system \ 使用私钥和证书登陆(公钥) 可以加密。 适用与长连接。坏处:速度慢 -DWITH_EMBEDDED_SERVER=1 \ 嵌入式数据库 -DENABLED_LOCAL_INFILE=1 \ 从本地倒入数据,不是备份和恢复。 -DWITH_INNOBASE_STORAGE_ENGINE=1 默认的存储引擎,支持外键
[root@mysql-server mysql-5.7.27]# make && make install 如果安装出错,想重新安装: 不用重新解压,只需要删除安装目录中的缓存文件CMakeCache.txt
需要很长时间!大约半小时
1.7、初始化
[root@mysql-server mysql-5.7.27]# cd /usr/local/mysql [root@mysql-server mysql]# chown -R mysql.mysql . [root@mysql-server mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data ---初始化完成之后,一定要记住提示最后的密码用于登陆或者修改密码
初始化,只需要初始化一次
设置环境变量 [root@localhost mysql]# echo 'export PATH=/usr/local/mysql/bin:$PATH' >>/etc/profile [root@localhost mysql]# source /etc/profile [root@localhost mysql]# echo $PATH /usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin [root@mysql-server ~]# vim /etc/my.cnf --如果打开文件有内容将文件中所有内容注释掉,在添加如下内容 [client] port = 3306 socket = /tmp/mysql.sock default-character-set = utf8 [mysqld] port = 3306 user = mysql basedir = /usr/local/mysql #指定安装目录 datadir = /usr/local/mysql/data #指定数据存放目录 socket = /tmp/mysql.sock character_set_server = utf8 [client] # 默认连接端口 port = 3306 # 用于本地连接的socket套接字 socket = /tmp/mysql.sock # 编码 default-character-set = utf8 [mysqld] # 服务端口号,默认3306 port = 3306 # mysql启动用户 user = mysql # mysql安装根目录 basedir = /usr/local/mysql # mysql数据文件所在位置 datadir = /usr/local/mysql/data # 为MySQL客户端程序和服务器之间的本地通讯指定一个套接字文件 socket = /tmp/mysql.sock # 数据库默认字符集,主流字符集支持一些特殊表情符号(特殊表情符占用4个字节) character_set_server = utf8
1.8、启动mysql
[root@mysql-server ~]# cd /usr/local/mysql [root@mysql-server mysql]# ./bin/mysqld_safe --user=mysql & 启动之后再按一下回车!即可后台运行
1.9、登录mysql
[root@mysql-server mysql]# /usr/local/mysql/bin/mysql -uroot -p'GP9TKGgY9i/8' 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 2 Server version: 5.7.27 Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. 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> exit
1.10、systemctl启动方式
-
拷贝启动脚本到/etc/init.d/目录下,并改名mysqld
[root@qfedu.com mysql]# cp support-files/mysql.server /etc/init.d/mysqld [root@qfedu.com mysql]# ls -l /etc/init.d/mysqld -rwxr-xr-x 1 root root 10588 Aug 1 18:33 /etc/init.d/mysqld
-
重新加载系统服务
[root@localhost mysql]# systemctl daemon-reload
-
启动MySQL数据库,并检查端口监听状态
[root@localhost mysql]# systemctl stop mysqld --停止mysqld # 或者 [root@localhost mysql]# systemctl start mysqld --启动mysqld Starting MySQL. SUCCESS! [root@localhost mysql]# netstat -lntp | grep 3306 tcp6 0 0 :::3306 :::* LISTEN 16744/mysqld
1.11、数据库修改密码
[root@localhost ~]# /usr/local/mysql/bin/mysqladmin -uroot -p'GP9TKGgY9i/8' password 'Qf@123!' [root@localhost ~]# /usr/local/mysql/bin/mysql -uroot -p'Qf@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 5 Server version: 5.7.27 Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. 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> create database wordpress; mysql> grant all on *.* to 'remote'@'%' identified by '123'; mysql> flush privileges;
二、编译安装 Nginx
静态资源(门户):MP4、MP3、jpg、html、css、js
1、安装编译 Nginx 依赖包
[root@localhost ~]# yum -y install gcc gcc-c++ make zlib-devel pcre pcre-devel openssl-devel perl-devel perl-ExtUtils-Embed gd-devel
2、官网下载 Nginx 安装包
[root@localhost ~]# wget https://nginx.org/download/nginx-1.16.0.tar.gz
3、创建 Nginx 运行用户
[root@qfedu.com ~]# useradd -s /sbin/nologin -M nginx
4、解压配置 Nginx 编译
[root@qfedu.com ~ ]# tar zxvf nginx-1.22.1.tar.gz -C /usr/local/ [root@localhost ~]# cd /usr/local/nginx-1.22.1/ [root@qfedu.com nginx-1.16.0]# ./configure \ --user=nginx \ --group=nginx \ --prefix=/usr/local/nginx \ --conf-path=/etc/nginx/nginx.conf \ --sbin-path=/usr/sbin/nginx \ --error-log-path=/var/log/nginx/nginx_error.log \ --http-log-path=/var/log/nginx/nginx_access.log \ --pid-path=/usr/local/nginx/run/nginx.pid
5、Nginx 编译安装
[root@qfedu.com nginx]# make && make install
6、测试 Nginx 是否安装成功
[root@localhost nginx-1.16.0]# nginx -V nginx version: nginx/1.16.0 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) configure arguments: --user=nginx --group=nginx --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --sbin-path=/usr/sbin/nginx --error-log-path=/var/log/nginx/nginx_error.log --http-log-path=/var/log/nginx/nginx_access.log --pid-path=/usr/local/nginx/run/nginx.pid
7、启动 Nginx 服务
[root@localhost nginx-1.16.0]# /usr/sbin/nginx
8、验证 Nginx 服务是否启动成功
[root@localhost nginx-1.16.0]# netstat -lntp | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 29740/nginx: master
9、系统添加 Nginx 服务
以 systemd 形式添加
1、创建 nginx.service 文件
[root@localhost ~]# vim /lib/systemd/system/nginx.service [Unit] Description=nginx After=network.target [Service] Type=forking ExecStart=/usr/sbin/nginx ExecReload=/usr/sbin/nginx -s reload ExecStop=/usr/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target [Unit]:服务的说明 Description:描述服务 After:描述服务类别 [Service]服务运行参数的设置 Type=forking是后台运行的形式 ExecStart为服务的具体运行命令 ExecReload为重启命令 ExecStop为停止命令 PrivateTmp=True表示给服务分配独立的临时空间 注意:[Service]的启动、重启、停止命令全部要求使用绝对路径 [Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
2、以 systemctl 方式启动 Nginx
[root@qfedu.com ~]# pkill nginx [root@qfedu.com ~]# systemctl daemon-reload [root@qfedu.com ~]# systemctl start nginx
3、查看 Nginx 服务状态
[root@qfedu.com ~]# ps -ef | grep nginx root 14116 1 0 17:36 ? 00:00:00 nginx: master process /usr/sbin/nginx nginx 14117 14116 0 17:36 ? 00:00:00 nginx: worker process root 14119 6576 0 17:36 pts/1 00:00:00 grep --color=auto nginx
4、验证 Nginx 服务是否成功启动
[root@qfedu.com ~]# netstat -ntlp | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 349/nginx: master p
5、配置 Nginx 服务自动启动
[root@qfedu.com ~]# systemctl enable nginx Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
三、编译安装 Php
1、安装编译环境依赖包
[root@qfedu.com ~]# yum -y install gcc gcc-c++ glibc automake autoconf libtool make
2、安装编译 php 依赖库
[root@qfedu.com ~]# yum -y install libxslt-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel
3、编译安装 Php
1、下载 Php 源码包
[root@qfedu.com ~]# wget https://www.php.net/distributions/php-7.3.6.tar.gz
wget https://libzip.org/download/libzip-1.3.2.tar.gz tar -xf libzip-1.3.2.tar.gz cd libzip-1.3.2/ ./configure make make install
2、配置 Php 编译
[root@localhost ~]# tar xzvf php-7.3.6.tar.gz -C /usr/local/ [root@localhost ~]# cd /usr/local/php-7.3.6/ [root@qfedu.com php-7.3.6 ]# ./configure \ --prefix=/usr/local/php7 \ --with-config-file-path=/usr/local/php7 \ --with-config-file-scan-dir=/usr/local/php7/php.d \ --enable-mysqlnd \ --with-mysqli \ --with-pdo-mysql \ --enable-fpm \ --with-fpm-user=nginx \ --with-fpm-group=nginx \ --with-gd \ --with-iconv \ --enable-xml \ --enable-shmop \ --enable-sysvsem \ --enable-inline-optimization \ --enable-mbregex \ --enable-mbstring \ --enable-ftp \ --enable-zip \ --with-openssl \ --with-zlib \ --with-libzip \ --enable-pcntl \ --enable-sockets \ --with-xmlrpc \ --enable-soap \ --without-pear \ --with-gettext \ --enable-session \ --with-curl \ --with-jpeg-dir \ --with-freetype-dir \ --enable-opcache
3、Php 编译参数说明
--prefix=/usr/local/php7 # 配置安装目录 --with-config-file-path=/usr/local/php7 # 配置文件 php.ini 的路径 --enable-sockets # 开启 socket --enable-fpm # 启用 fpm 扩展 --enable-cli # 启用 命令行模式 (从 php 4.3.0 之后这个模块默认开启所以可以不用再加此命令) --enable-mbstring # 启用 mbstring 库 --enable-pcntl # 启用 pcntl (仅 CLI / CGI) --enable-soap # 启用 soap --enable-opcache # 开启 opcache 缓存 --disable-fileinfo # 禁用 fileinfo (由于 5.3+ 之后已经不再持续维护了,但默认是开启的,所以还是禁止了吧)(1G以下内存服务器直接关了吧) --disable-rpath # 禁用在搜索路径中传递其他运行库。 --with-mysqli # 启用 mysqli 扩展 --with-pdo-mysql # 启用 pdo 扩展 --with-iconv-dir # 启用 XMLRPC-EPI 字符编码转换 扩展 --with-openssl # 启用 openssl 扩展 (需要 openssl openssl-devel) --with-fpm-user=nginx # 设定 fpm 所属的用户 --with-fpm-group=nginx # 设定 fpm 所属的组别 --with-curl # 启用 curl 扩展 --with-mhash # 开启 mhash 基于离散数学原理的不可逆向的php加密方式扩展库 # GD --with-gd # 启用 GD 图片操作 扩展 --with-jpeg-dir # 开启对 jpeg 图片的支持 (需要 libjpeg) --with-png-dir # 开启对 png 图片支持 (需要 libpng) --with-freetype-dir # 开启 freetype # xml --enable-simplexml # 启用对 simplexml 支持 --with-libxml-dir # 启用对 libxml2 支持 --enable-debug # 开启 debug 模式
4、编译安装 Php
[root@qfedu.com php-7.3.6]# make && make install
大约需要等待30分钟
5、创建 php.ini 配置文件
[root@localhost php-7.3.6]# cp php.ini-production /usr/local/php7/etc/php.ini [root@localhost php-7.3.6]# vim /usr/local/php7/etc/php.ini +1371 #php的Session存储目录 1371 session.save_path = "/tmp" #将注释打开
6、设置php-fpm配置文件
[root@qfedu.com php-7.3.6]# cd /usr/local/php7/etc [root@localhost etc]# cp php-fpm.conf.default php-fpm.conf [root@localhost etc]# vim php-fpm.conf +17 pid = /var/run/php-fpm.pid #将注释取消并修改 # php-fpm连接文件 [root@localhost etc]# cd /usr/local/php7/etc/php-fpm.d/ [root@localhost php-fpm.d]# cp www.conf.default www.conf #php-fpm子配置文件 [root@localhost php-fpm.d]# vim www.conf user = nginx group = nginx listen = 127.0.0.1:9000
7、启动 php-fpm
[root@qfedu.com php-fpm.d]# /usr/local/php7/sbin/php-fpm
8、检查 php-fpm 是否成功启动
[root@qfedu.com php-fpm.d]# ps aux | grep php-fpm
若看到相关进程,则证明启动成功。查询进程时,进程是以 nginx 用户身份执行的
9、配置 php-fpm 系统环境变量
[root@localhost php-fpm.d]# cd [root@qfedu.com ~]# vim /etc/profile export PHP_HOME=/usr/local/php7 export PATH=$PATH:$PHP_HOME/bin:$PHP_HOME/sbin
10、重载环境变量
[root@qfedu.com ~]# source /etc/profile
-
使用 echo $PATH 命令查看环境变量中是否已经加入了相关的路径
11、配置 php-fpm 开机自启动
[root@qfedu.com ~]# vim /lib/systemd/system/php-fpm.service [Unit] Description=php-fpm After=network.target [Service] Type=forking ExecStart=/usr/local/php7/sbin/php-fpm ExecStop=/bin/pkill -9 php-fpm PrivateTmp=true [Install] WantedBy=multi-user.target
12、php-fpm.service 文件说明
[Unit]:服务的说明 Description:描述服务 After:描述服务类别 [Service]服务运行参数的设置 Type=forking是后台运行的形式 ExecStart为服务的具体运行命令 ExecReload为重启命令 ExecStop为停止命令 PrivateTmp=True表示给服务分配独立的临时空间 注意:[Service]的启动、重启、停止命令全部要求使用绝对路径 [Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
13、重载 systemctl 配置
[root@qfedu.com ~]# systemctl daemon-reload
14、停止 php-fpm
[root@qfedu.com ~]# pkill php-fpm
15、用 systemctl 启动 php-fpm
[root@qfedu.com ~]# systemctl start php-fpm.service
16、设置 php-fpm 开机启动
[root@qfedu.com ~]# systemctl enable php-fpm.service
17、php-fpm 管理命令
[root@qfedu.com ~]# systemctl stop php-fpm.service # 停止服务 [root@qfedu.com ~]# systemctl restart php-fpm.service # 重新启动服务 [root@qfedu.com ~]# systemctl status php-fpm.service # 查看服务当前状态 [root@qfedu.com ~]# systemctl disable php-fpm.service # 停止开机自启动
四、Nginx 配置支持PHP
1、添加 Nginx 配置
[root@localhost ~]# cd /etc/nginx/ [root@localhost nginx]# vim nginx.conf #配置如下 server { listen 80; server_name localhost; index index.html index.htm index.php; root /usr/local/nginx/html; location ~ \.php$ { include fastcgi_params; #指定nginx连接php-fpm的常量 fastcgi_pass 127.0.0.1:9000; #连接php-fpm的地址和端口 fastcgi_index index.php; #指定默认页面 fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; #指定站点根目录 } }
2、添加 php 探测文件
[root@localhost nginx]# cd /usr/local/nginx/html/ [root@qfedu.com html]# vim index.php <?php phpinfo(); ?>
3、验证 Nginx 关联 php-fpm
1、重启 php-fpm
[root@qfedu.com html]# systemctl restart php-fpm.service [root@localhost ~]# ps -ef|grep php-fpm root 14259 1 0 17:42 ? 00:00:00 php-fpm: master process (/usr/local/php7/etc/php-fpm.conf) nginx 14260 14259 0 17:42 ? 00:00:00 php-fpm: pool www nginx 14261 14259 0 17:42 ? 00:00:00 php-fpm: pool www root 14263 6576 0 17:43 pts/1 00:00:00 grep --color=auto php-fpm
2、重载 Nginx 配置
[root@qfedu.com html]# systemctl restart nginx [root@qfedu.com html]# systemctl status nginx
3、访问验证
LNMP环境编译安装完成!
4、基于LAMP环境上线wordpress
[root@localhost ~]# tar xzvf wordpress-4.9.4-zh_CN.tar.gz [root@localhost ~]# rm -rf /usr/local/nginx/html/* [root@localhost ~]# cp -r wordpress/* /usr/local/nginx/html/ #将代码拷贝到网站发布目录中 [root@localhost ~]# systemctl restart nginx 创建数据库 [root@localhost html]# /usr/local/mysql/bin/mysql -uroot -p'Qf@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 17 Server version: 5.7.27 Source distribution Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. 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> create database wordpress; Query OK, 1 row affected (0.00 sec) mysql> \q Bye
测试访问
如果不能创建wp-config.php请手动创建 [root@localhost ~]# cd /usr/local/nginx/html/ [root@localhost html]# vim wp-config.php ---将提示框中的信息粘贴进去
重新访问