CentOS 7.2 编译安装 LNMP 环境
一、安装前的准备工作
1、执行 yum update 命令以更新系统组件。
2、运行 yum install gcc gcc-c++ autoconf automake cmake bison m4 libxml2 libxml2-devel libcurl-devel libjpeg-devel libpng-devel libicu-devel 命令,安装构建 PHP、MySQL 和 Nginx 所需的依赖库。
3、下载以下软件包。建议将所有源文件存放于 root 目录,读者可根据实际情况调整路径。
3.1 libmcrypt-2.5.8.tar.gz
3.2 mcrypt-2.6.8.tar.gz
3.3 mhash-0.9.9.9.tar.gz
3.4 zlib-1.2.8.tar.gz
解压并安装示例如下:
#tar -zvxf libmcrypt-2.5.8.tar.gz
#cd libmcrypt-2.5.8
#./configure
#make && make insatll
4、若在软件安装过程中提示缺少依赖包,可再次执行 yum install * -y 命令(其中 * 代表相关包名)进行补充安装。
二、编译安装 Nginx
1、访问官网 http://nginx.org/en/download.html,下载 nginx-1.10.1.tar.gz 稳定版本。
2、具体编译步骤如下:
1、使用 WinSCP 工具将 nginx-1.10.1.tar.gz 上传至 /root 目录。
1.1 groupadd -r nginx # 创建 nginx 用户组
1.2 useradd -r -g nginx -s /bin/false nginx # 创建无登录权限的 nginx 用户
1.3 id nginx # 验证 nginx 用户组及用户信息
2、tar -zvxf nginx-1.10.1.tar.gz # 解压安装包
3、cd nginx-1.10.1 # 进入解压后的目录
4、可通过 ./configure --help 查看编译配置参数,或参考 http://nginx.org/en/docs/configure.html。下列参数需合并写在一行中:
./configure
--prefix=/usr/local/nginx
--modules-path=/usr/local/nginx/modules
--with-http_ssl_module
--pid-path=/usr/local/nginx/nginx.pid
--user=nginx
--group=nginx
5、make && make install # 执行编译并安装
6、启动 Nginx 服务
6.1 cd /usr/local/nginx # 进入安装目录
6.2 sbin/nginx # 启动服务,可使用 sbin/nginx -? 查看相关操作命令
7、在 /usr/lib/systemd/system 目录下新建 nginx.service 文件,以便通过 systemctl stop|start|reload nginx.service 管理 Nginx。具体内容参考 https://www.nginx.com/resources/wiki/start/topics/examples/systemd/,如下所示:
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
三、编译安装 MySQL
1、访问官网 http://dev.mysql.com/Downloads/MySQL-5.7/mysql-boost-5.7.14.tar.gz,下载包含 Boost 库的 5.7.14 版本。
2、具体编译步骤如下:
1、使用 WinSCP 将 mysql-boost-5.7.14.tar.gz 上传至 /root 目录。
2、groupadd mysql # 创建 mysql 用户组
3、useradd -r -g mysql -s /bin/false mysql # 创建无登录权限的 mysql 用户
4、使用 cmake 编译 MySQL,相关参数参考 https://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html。下列参数需合并写在一行中:
cmake
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock
-DSYSCONFDIR=/usr/local/mysql/etc
-DSYSTEMD_PID_DIR=/usr/local/mysql
-DDEFAULT_CHARSET=utf8
-DDEFAULT_COLLATION=utf8_general_ci
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_ARCHIVE_STORAGE_ENGINE=1
-DWITH_BLACKHOLE_STORAGE_ENGINE=1
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1
-DMYSQL_DATADIR=/usr/local/mysql/data
-DWITH_BOOST=boost
-DWITH_SYSTEMD=1
5、make && make install # 执行编译并安装
6、配置 MySQL 并初始化数据库
6.1 cd /usr/local/mysql # 进入编译安装目录
6.2 chown -R mysql . # 修改目录所有者为 mysql
6.3 chgrp -R mysql . # 修改目录所属组为 mysql
6.4 cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld # 配置 mysqld 服务脚本
6.5 cp /usr/local/mysql/support-files/my-default.cnf /usr/local/mysql/my.cnf # 复制默认配置文件为 my.cnf
6.5.1 将以下内容复制到 my.cnf 文件的 [mysqld] 段落下:
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
server_id = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
6.5 chkconfig mysqld on # 设置 mysqld 开机自启动
6.6 bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data # 初始化数据库
6.7 bin/mysqld --user=mysql & # 启动 MySQL。若报错 Please read "Security" section of the manual to find out how to run mysqld as root!,请在 my.cnf 中添加 user=root,表示以 root 用户启动。
7、修改 root 用户登录密码并允许远程登录
7.1 mysql -u root --skip-password # 免密登录
7.2 ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; # 设置新密码
7.3 允许 root 用户远程登录
7.3.1 use mysql; # 切换数据库
7.3.2 update user set host='%' where user='root' and host='localhost'; # 允许远程访问 (update user set host='localhost' where user='root'; # 禁用远程访问)
7.3.3 flush privileges; # 刷新权限
7.3.4 service mysqld restart # 重启 MySQL 服务
8、解决 service mysqld start|stop 报 MySQL server PID file could not be found! 或 Couldn't find MySQL server (/usr/local/mysql/bin/mysqld_safe) 错误。可通过阅读相关日志文件排查错误。
8.1 chmod 777 /usr/local/mysql # 因设置 mysqld.pid 文件保存在 /usr/local/mysql 目录,故需保证该目录具有可写权限
8.2 使用 WinSCP 修改 /etc/init.d/mysqld 文件
8.2.1 basedir=/usr/local/mysql # 手动指定基础目录
8.2.2 datadir=/usr/local/mysql/data # 手动指定数据目录
8.2.3 mysqld_pid_file_path=/usr/local/mysql/mysqld.pid # 手动指定 PID 文件路径
8.2.4 将该文件中所有未注释的含有 mysqld_safe 的字符替换为 mysqld
四、编译安装 PHP
1、访问官网 http://php.net/downloads.php,下载 php7.0.10 版本。
2、具体编译步骤如下:
1、使用 WinSCP 将 php-7.0.10.tar.gz 上传至 /root 目录。
2、tar -zvxf php-7.0.10.tar.gz # 解压安装包
3、配置编译参数,可使用 ./configure --help 查看所有选项。下列参数需合并写在一行中:
./configure
--prefix=/usr/local/php
--exec-prefix=/usr/local/php
--datadir=/usr/local/php
--with-config-file-path=/usr/local/php/etc
--with-mysqli=mysqlnd
--with-pdo-mysql=mysqlnd
--with-fpm-user=nginx
--with-fpm-group=nginx
--with-gd
--with-iconv
--enable-mbstring
--enable-fpm
--enable-mysqlnd
4、make && make install # 执行编译并安装
5、cd /usr/local/php # 进入安装目录
6、修改相关配置文件
6.1 cp /usr/local/php/etc/php.ini.default /usr/local/php/etc/php.ini # php.ini 中的相关配置请依项目需求自行修改,配置 Nginx 支持 PHP 请参考 http://php.net/manual/zh/install.unix.nginx.php
6.2 cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf # 去掉 [global] 项下 pid 前的分号 ;
6.3 cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf # 大致在第 23、24 行修改 user 和 group,例如:user = nginx,group = nginx
7、chmod 777 /usr/local/php/var/run # 默认 PID 文件写入 /usr/local/php/var/run 目录,因此需修改该目录权限
8、sbin/php-fpm # 启动 PHP-FPM,可通过 sbin/php-fpm -h 查看相关操作命令列表
9、在 /usr/lib/systemd/system 目录下新建 php-fpm.service 文件,以便通过 systemctl stop|start|reload php-fpm.service 管理 PHP-FPM。内容如下:
[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target
Before=nginx.service
[Service]
Type=forking
PIDFile=/usr/local/php/var/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm
ExecStop=/bin/kill -QUIT `cat /usr/local/php/var/run/php-fpm.pid`
ExecReload=/bin/kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`
PrivateTmp=true
[Install]
WantedBy=multi-user.target
五、至此,在 VirtualBox 中的 CentOS 7.2 环境下已成功搭建 LNMP 环境。

以上所述是小编给大家介绍的 CentOS 7.2 下编译安装 PHP7.0.10+MySQL5.7.14+Nginx1.10.1 的方法详解(mini版本),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对本站网站的支持!