编译安装LNMP生产环境_编译安装坏处

39 阅读7分钟

参数详解: -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


**需要很长时间!**


8、初始化



[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


9、启动mysql



[root@mysql-server ~]# cd /usr/local/mysql [root@mysql-server mysql]# ./bin/mysqld_safe --user=mysql & ​ 启动之后再按一下回车!即可后台运行


10、登录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


#### 11、启动MySQL数据库


* 拷贝启动脚本到/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


* 重新加载系统服务,将MySQL数据库加入开机自启动



[root@localhost mysql]# systemctl daemon-reload [root@localhost mysql]# chkconfig mysqld on #设置开机启动


* 启动MySQL数据库,并检查端口监听状态



[root@localhost mysql]# /etc/init.d/mysqld stop --停止mysqld

或者

[root@localhost mysql]# /etc/init.d/mysqld start --启动mysqld Starting MySQL. SUCCESS! ​ [root@localhost mysql]# netstat -lntp | grep 3306 tcp6 0 0 :::3306 :::* LISTEN 16744/mysqld


#### 给数据库修改密码



[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>


### 二、编译安装 Nginx


  Nginx简介:Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。



#### 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 nginx.org/download/ng…


#### 3、创建 Nginx 运行用户



[root@qfedu.com ~]# useradd -s /sbin/nologin -M nginx


#### 5、解压配置 Nginx 编译



[root@qfedu.com ~ ]# tar zxvf nginx-1.16.0.tar.gz -C /usr/local/ [root@localhost ~]# cd /usr/local/nginx-1.16.0/ [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


#### 7、Nginx 编译安装



[root@qfedu.com nginx]# make && make install


#### 9、测试 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


#### 10、启动 Nginx 服务



[root@localhost nginx-1.16.0]# /usr/sbin/nginx


#### 11、验证 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


#### 12、系统添加 Nginx 服务



#### 1、以 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


#### 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 31469 1 0 23:11 ? 00:00:00 nginx: master process /usr/sbin/nginx nginx 31470 31469 0 23:11 ? 00:00:00 nginx: worker process root 31554 1182 0 23:11 pts/0 00:00:00 grep --color=auto ngin


#### 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、创建安装目录



[root@qfedu.com ~]# mkdir -p /usr/local/php7


#### 3、安装编译 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


升级cmake



[root@localhost build]# yum remove cmake -y [root@localhost ~]# curl -O cmake.org/files/v3.6/… #下载cmake安装包,解压后直接使用 [root@localhost ~]# tar xzvf cmake-3.6.0-Linux-x86_64.tar.gz 设置环境变量 [root@localhost ~]# echo "export PATH=$PATH:/root/cmake-3.6.0-Linux-x86_64/bin" >> /etc/profile [root@localhost ~]# source /etc/profile #让环境变量生效 ​ [root@localhost ~]# cmake -version cmake version 3.6.0


#### 6、安装 libzip



#### 1、下载 libzip



[root@qfedu.com ~]# wget libzip.org/download/li…


#### 2、安装 libzip



[root@qfedu.com ~]# tar -zxvf libzip-1.5.2.tar.gz [root@qfedu.com ~]# cd libzip-1.5.2/ [root@qfedu.com libzip-1.5.2]# mkdir build [root@qfedu.com libzip-1.5.2]# cd build [root@qfedu.com build]# cmake .. [root@qfedu.com build]# make install


#### 7、编译安装 Php



#### 1、下载 Php 源码包



[root@qfedu.com build]# cd [root@qfedu.com php]# wget www.php.net/distributio…


#### 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
--with-zlib
--enable-xml
--enable-shmop
--enable-sysvsem
--enable-inline-optimization
--enable-mbregex
--enable-mbstring
--enable-ftp
--with-openssl
--enable-pcntl
--enable-sockets
--with-xmlrpc
--enable-zip
--enable-soap
--without-pear
--with-gettext
--enable-session
--with-curl
--with-jpeg-dir
--with-freetype-dir
--enable-opcache


#### 3、编译中 off\_t 问题解决



[root@qfedu.com php-7.3.6 ]# vim /etc/ld.so.conf

添加如下几行

/usr/local/lib64 /usr/local/lib /usr/lib /usr/lib64

使配置生效

[root@qfedu.com php-7.3.6 ]# ldconfig -v ​ 在重新执行./configure命令 ​ #需要等待30分钟左右


#### 4、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

压缩

--enable-zip # 启用 zip --with-zlib # 启用对 zlib 支持

xml

--enable-simplexml # 启用对 simplexml 支持 --with-libxml-dir # 启用对 libxml2 支持 ​ --enable-debug # 开启 debug 模式


#### 5、编译安装 Php



[root@qfedu.com php-7.3.6]# make && make install


#### 6、创建 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" #将注释打开


#### 7、设置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 [root@localhost php-fpm.d]# vim www.conf user = nginx group = nginx listen = 127.0.0.1:9000 pm.max_children = 100 pm.start_servers = 20 pm.min_spare_servers = 5 pm.max_spare_servers = 35


#### 8、php-fpm 进程池配置


* 下面4个参数的意思分别为:
	+ pm.max\_children:动态方式下他限定php-fpm的最大进程数(这里要注意pm.max\_spare\_servers的值只能小于等 pm.max\_children)
	+ pm.start\_servers:动态方式下的起始php-fpm进程数量。
	+ pm.min\_spare\_servers:动态方式空闲状态下的最小php-fpm进程数量。
	+ pm.max\_spare\_servers:动态方式空闲状态下的最大php-fpm进程数量。



#### 9、启动 php-fpm



[root@qfedu.com php-fpm.d]# /usr/local/php7/sbin/php-fpm


#### 10、检查 php-fpm 是否成功启动



[root@qfedu.com php-fpm.d]# ps aux | grep php-fpm


若看到相关进程,则证明启动成功。查询进程时,进程是以 nginx 用户身份执行的



#### 11、配置 php-fpm 系统环境变量



[root@localhost php-fpm.d]# cd [root@qfedu.com ~]# vim /etc/profile export PHP_HOME=/usr/local/php7 export PATH=PATH:PATH:PHP_HOME/bin:$PHP_HOME/sbin


#### 12、重载环境变量



[root@qfedu.com ~]# source /etc/profile


* 使用 echo $PATH 命令查看环境变量中是否已经加入了相关的路径


#### 13、配置 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


#### 14、php-fpm.service 文件说明



Description:描述服务 After:描述服务类别 [Service]服务运行参数的设置 Type=forking是后台运行的形式 ExecStart为服务的具体运行命令 ExecReload为重启命令 ExecStop为停止命令 PrivateTmp=True表示给服务分配独立的临时空间 注意:[Service]的启动、重启、停止命令全部要求使用绝对路径 [Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3


#### 15、重载 systemctl 配置



[root@qfedu.com ~]# systemctl daemon-reload


#### 16、停止 php-fpm



[root@qfedu.com ~]# pkill php-fpm


#### 17、用 systemctl 启动 php-fpm



[root@qfedu.com ~]# systemctl start php-fpm.service


#### 18、设置 php-fpm 开机启动



[root@qfedu.com ~]# systemctl enable php-fpm.service


#### 19、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 # 查看服务当前状态