title: 编译 LNMP 部署动态网站环境
- date: 2018-11-08 13:59:59
- tags:
Linux 服务配置
categories: Linux 服务配置
- copyright: true
- ---
LNMP 动态网站部署架构是由一套 Linux+Nginx+MySQL+PHP 组成的动态网站系统解决方案.
以下配置环境为: Linux=RHEL7 --> Nginx=1.13 --> MySQL=5.6 --> PHP=7.0 无错误版.
安装编译环境
在使用源码包安装服务程序之前, 首先要让安装主机具备编译程序源码的环境, 他需要具备 C 语言, C++ 语言的编译器, 以及常见的编译支持函数库程序, 下面我们将通过 Yum 仓库来快速部署这些包.
- [[email protected] ~]# yum install -y gcc gcc-c++ apr apr-util autoconf automake bison bzip2
- Loaded plugins: product-id, search-disabled-repos, subscription-manager
- This system is not registered with an entitlement server. You can use subscription-manager.
- Package gcc-4.8.5-28.el7.x86_64 already installed and latest version
- Package gcc-c++-4.8.5-28.el7.x86_64 already installed and latest version
- Package apr-1.4.8-3.el7_4.1.x86_64 already installed and latest version
- Package apr-util-1.5.2-6.el7.x86_64 already installed and latest version
- Package autoconf-2.69-11.el7.noarch already installed and latest version
- Package automake-1.13.4-3.el7.noarch already installed and latest version
- Package bison-3.0.4-1.el7.x86_64 already installed and latest version
- Package bzip2-1.0.6-13.el7.x86_64 already installed and latest version
- Nothing to do
CMake 是 Linux 系统中一款常用的编译工具, 在这里 MySQL 的编译会用到 CMake 命令, 接下来我们将解压, 并编译安装这个包.
- [[email protected] ~]# tar -xzvf cmake-2.8.11.2.tar.gz
- [[email protected] ~]# cd cmake-2.8.11.2
- [[email protected] ~]# ./configure
- [[email protected] ~]# make
- [[email protected] ~]# make install
编译安装 Nginx
Nginx 是一款相当优秀的用于部署动态网站的轻量级服务程序, 他最初是为俄罗斯门户站点开发的, 因其性能稳定, 功能丰富, 占用内存小且并发能力高而备受用户的信赖, 目前诸如, 新浪搜狐, 网易, 腾讯等门户站点均已经使用了此服务.
坦白来说, 虽然 Nginx 服务程序代码质量非常高, 代码很规范, 技术成熟, 模块扩展也很容易, 但任然存在不少的问题, 比如官方资料对中文的支持较少, 但是 Nginx 服务程序, 在近几年来的发展势头迅猛, 相信会在轻量级 Web 服务器市场能够有不错的前景.
1. 在编译配置 Nginx 之前我们还需要解决相关依赖问题, 例如用于提供 Perl 语言兼容的正则表达式库的软件包 pcre, 就是 Nginx 程序用于实现伪静态功能必不可少的依赖包, 下面我们先来解压, 并编译, 安装这个包.
- [[email protected] ~]# tar -xzvf pcre-8.35.tar.gz
- [[email protected] ~]# cd pcre-8.35
- [[email protected] ~]# ./configure --prefix=/usr/local/pcre
- [[email protected] ~]# make
- [[email protected] ~]# make install
2. 紧接着继续编译安装 openssl, 这个工具是用与提供网站加密证书服务的程序文件, 安装时需要自定义服务程序的安装目录, 以便于后期调用命令是更方便.
- [[email protected] ~]# tar -xzvf openssl-1.0.1h.tar.gz
- [[email protected] ~]# cd openssl-1.0.1h
- [[email protected] ~]# ./config --prefix=/usr/local/openssl
- [[email protected] ~]# make
- [[email protected] ~]# make install
3. 在 openssl 安装后, 默认会在 / usr/local/openssl/bin 目录中提供很多可用命令, 我们将这个目录添加到 PATH 环境变量里, 方便后期的调用.
- [[email protected] ~]# echo "export PATH=$PATH:/usr/local/openssl/bin">> /etc/profile
- [[email protected] ~]# source /etc/profile
- [[email protected] ~]# openssl
- OpenSSL>
4.zlib 软件包是用户提供压缩功能的函数文件, 下面我们开始编译安装.
- [[email protected] ~]# tar -xzvf zlib-1.2.8.tar.gz
- [[email protected] ~]# cd zlib-1.2.8
- [[email protected] ~]# ./configure --prefix=/usr/local/zlib
- [[email protected] ~]# make
- [[email protected] ~]# make install
5. 在安装好 zlib 后, 下面我们进入编译 Nginx 的前戏阶段, 创建一个 Nginx 用户
[[email protected] ~]# useradd -s /sbin/nologin -M nginx
6. 下面开始编译安装 Nginx, 这里我们需要指定 --user 与 --group 参数, 也就是用户与组, 在使用参数 openssl 和 zlib 库时应该指定具体位置.
- [[email protected] ~]# tar -xzvf nginx-1.13.12.tar.gz
- [[email protected] ~]# cd nginx-1.13.12
- [[email protected] ~]# ./configure --prefix=/usr/local/nginx> --user=nginx --group=nginx> --with-http_stub_status_module --with-http_ssl_module> --with-http_gzip_static_module --with-openssl=/root/openssl-1.0.1h> --with-zlib=/root/zlib-1.2.8 --with-pcre=/root/pcre-8.35
- [[email protected] ~]# make
- [[email protected] ~]# make install
7. 启动 Nginx, 并设置开机自启动
- [[email protected] ~]# /usr/local/nginx/sbin/nginx -t #检测配置文件正确性
- [[email protected] ~]# /usr/local/nginx/sbin/nginx #启动 Nginx
- [[email protected] ~]# kill -QUIT $(cat /usr/local/nginx/logs/nginx.pid) #关闭 Nginx
- [[email protected] ~]# kill -HUP $(cat /usr/local/nginx/logs/nginx.pid) #重启 Nginx
- [[email protected] ~]# echo "/usr/local/nginx/sbin/nginx 2> /dev/null">> /etc/profile
编译安装 MySQL
MySQL 是一个关系型数据库管理系统, 由瑞典 MySQL AB 公司开发, 目前属于 Oracle 旗下产品. MySQL 是最流行的关系型数据库管理系统之一, 在 Web 应用方面, MySQL 是最好的 RDBMS 应用软件, MySQL 是一种关系数据库管理系统, 关系数据库将数据保存在不同的表中, 而不是将所有数据放在一个大仓库内, 这样就增加了速度并提高了灵活性. 这里需要注意的是由于 MySQL5.7 以后产品闭源了, 后期的话可以使用 MariaDB 替代.
1. 在编译 MySQL 过程中, 我们需要创建一个 MySQL 的系统用户, 专门负责运行 MySQL 数据库, 记得把这个账户变成一个不可登录的用户, 且也无需创建家目录, 提高系统安全性.
[[email protected] ~]# useradd -s /sbin/nologin -M MySQL
2. 创建一个用于保存 MySQL 程序, 和数据库文件的目录, 并把该目录所有者和所属组改成 MySQL, 其中 / usr/local/MySQL 用于保存 MySQL 数据程序, 而 / usr/local/MySQL/data 则用来保存数据库文件.
- [[email protected] ~]# mkdir -p /usr/local/MySQL
- [[email protected] ~]# mkdir -p /usr/local/MySQL/data
- [[email protected] ~]# chown -Rf MySQL:MySQL /usr/local/MySQL/
3. 安装 Ncurses, 若不安装 ncurses 编译 MySQL 时会报错
- [[email protected] ~]# yum install -y ncurses-devel
- [[email protected] ~]# tar -xzvf ncurses-6.1.tar.gz
- [[email protected] ~]# cd ncurses-6.1
- [[email protected] ~]# ./configure --with-shared --without-debug --without-ada --enable-overwrite
- [[email protected] ~]# make
- [[email protected] ~]# make install
- # --without-ada 参数为设定不编译为 ada 绑定, 因进入 chroot 环境不能使用 ada ;
- # --enable-overwrite 参数为定义把头文件安装到 / tools/include 下而不是 / tools/include/ncurses 目录
- # --with-shared 生成共享库
4. 接下来, 开始解压编译 MySQL, 由于 MySQL 体积过大, 编译时间很长, 耐心等待吧
- [[email protected] ~]# tar -xzvf MySQL-5.6.19.tar.gz
- [[email protected] ~]# cd MySQL-5.6.19
- [[email protected] ~]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/MySQL -DMYSQL_DATADIR=/usr/local/MySQL/data -DSYSCONFDIR=/etc -DMYSQL_UNIX_ADDR=/tmp/MySQL.sock -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_USER=MySQL -DMYSQL_TCP_PORT=3306
- [[email protected] ~]# make
- [[email protected] ~]# make install
5. 下面进入配置环节, 首先删除 / etc/my.cnf 默认配置文件, 然后在 MySQL 程序目录内, 找到一个名为 mysql_install_db 的脚本文件, 执行这个脚本, 并使用 --user 参数指定 MySQL 服务的对应账号名称, 使用 --basedir 指定 MySQL 程序保存目录, 使用 --datadir 指定数据库目录.
- [[email protected] ~]# rm -fr /etc/my.cnf
- [[email protected] ~]# cd /usr/local/MySQL/
- [[email protected] MySQL]# ./scripts/mysql_install_db --user=MySQL --basedir=/usr/local/MySQL/ --datadir=/usr/local/MySQL/data/
- ....
- New default config file was created as /usr/local/MySQL//my.cnf and
- will be used by default by the server when you start it.
- You may edit this file to change server settings
6. 接着把, MySQL 数据库配置文件复制到 / etc / 目录下, 然后把开机程序复制到 / etc/rc.d/init.d 目录下, 给脚本 755 的权限.
- [[email protected] MySQL]# cp -a my.cnf /etc/my.cnf
- [[email protected] MySQL]# cp ./support-files/MySQL.server /etc/rc.d/init.d/mysqld
- [[email protected] MySQL]# chmod 755 -R /etc/rc.d/init.d/mysqld
7. 修改刚刚的配置文件, 改 basedir=/usr/local/MySQL,datadir=/usr/local/MySQL/data 这一行.
- [[email protected] MySQL]# VIM /etc/rc.d/init.d/mysqld
- 43 # If you change base dir, you must also change datadir. These may get
- 44 # overwritten by settings in the MySQL configuration files.
- 45
- 46 basedir= /usr/local/MySQL
- 47 datadir= /usr/local/MySQL/data
- 48
- 49 # Default value, in seconds, afterwhich the script should timeout waiting
- 50 # for server start.
8. 启动 MySQL, 与设置 chkconfig 开机自启动的配置.
- [[email protected] ~]# /etc/rc.d/init.d/mysqld start
- [[email protected] ~]# /usr/local/MySQL/bin/mysqld_safe --user=MySQL &
- [[email protected] ~]# chkconfig mysqld on
- [[email protected] ~]# chkconfig --list mysqld
- Note: This output shows SysV services only and does not include native
- systemd services. SysV configuration data might be overridden by native
- systemd configuration.
- If you want to list systemd services use 'systemctl list-unit-files'.
- To see services enabled on particular target use
- 'systemctl list-dependencies [target]'.
- mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
- [[email protected] ~]# echo "/usr/local/mysql/bin/mysqld_safe --user=mysql &">> /etc/rc.local
9. 添加环境变量, 使用相对路径命令, 这样就能直接访问了.
- [[email protected] ~]# echo "export PATH=$PATH:/usr/local/mysql/bin">> /etc/profile
- [[email protected] ~]# source /etc/profile
- [[email protected] ~]# MySQL -uroot
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 3
- Server version: 5.6.19 Source distribution
- Copyright (c) 2000, 2014, 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>
10.MySQL 数据库还会调用一些函数库, 在这里我们把它们复制到指定的位置吧.
- [[email protected] ~]# mkdir /var/lib/MySQL
- [[email protected] ~]# ln -s /usr/local/MySQL/lib/*/var/lib/MySQL
- [[email protected] ~]# ln /usr/local/MySQL/include/MySQL /usr/include/
- [[email protected] ~]# ln -s /usr/local/MySQL/include/MySQL /usr/include/MySQL
11. 接着我们执行 mysql_secure_installation 命令, 给 MySQL 设置初始密码, 到此配置完毕
- [[email protected] ~]# mysql_secure_installation
- [[email protected] ~]# MySQL -uroot -p1233
- 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 18
- Server version: 5.6.19 Source distribution
- Copyright (c) 2000, 2014, 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>
编译安装 PHP
PHP 是一种通用开源脚本语言, 语法吸收了 C 语言, Java 和 Perl 的特点, 利于学习使用广泛, 主要适用于 Web 开发领域. PHP 独特的语法混合了 C,Java,Perl 以及 PHP 自创的语法. 它可以比 CGI 或者 Perl 更快速地执行动态网页. 用 PHP 做出的动态页面与其他的编程语言相比, PHP 是将程序嵌入到 HTML 文档中去执行, 执行效率比完全生成 HTML 标记的 CGI 要高许多, PHP 还可以执行编译后代码, 编译可以达到加密和优化代码运行, 使代码运行更快.
1. 在编译安装 PHP 之前首先要解决依赖的问题, 例如 yasm 是一款常见的开源汇编器.
- [[email protected] ~]# tar -xzvf yasm-1.2.0.tar.gz
- [root[email protected] ~]# cd yasm-1.2.0
- [[email protected] ~]# ./configure
- [[email protected] ~]# make
- [[email protected] ~]# make install
2.libmcrypt 是用于加密算法的扩展库程序.
- [[email protected] ~]# tar -xzvf libmcrypt-2.5.8.tar.gz
- [[email protected] ~]# cd libmcrypt-2.5.8
- [[email protected] ~]# ./configure
- [[email protected] ~]# make
- [[email protected] ~]# make install
3.libvpx 是用于提供视频编码器服务的程序.
- [[email protected] ~]# tar -xjvf libvpx-v1.3.0.tar.bz2
- [[email protected] ~]# cd libvpx-v1.3.0
- [[email protected] ~]# ./configure --prefix=/usr/local/libvpx --enable-shared --enable-vp9
- [[email protected] ~]# make
- [[email protected] ~]# make install
4.tiff 是用于提供标签图像文件格式的服务程序.
- [[email protected] ~]# tar -xzvf tiff-4.0.3.tar.gz
- [[email protected] ~]# cd tiff-4.0.3
- [[email protected] ~]# ./configure --prefix=/usr/local/tiff --enable-shared
- [[email protected] ~]# make
- [[email protected] ~]# make install
5.libpng 是用于提供 PNG 图片格式支持函数库的服务程序.
- [[email protected] ~]# tar -xzvf libpng-1.6.12.tar.gz
- [[email protected] ~]# yum install -y zlib zlib-devel
- [[email protected] ~]# ./configure --prefix=/usr/local/libpng --enable-shared
- [[email protected] ~]# make
- [[email protected] ~]# make install
6.freetype 是用于提供字体支持引擎的服务.
- [[email protected] ~]# tar -xzvf freetype-2.5.3.tar.gz
- [[email protected] ~]# cd freetype-2.5.3
- [[email protected] ~]# ./configure --prefix=/usr/local/freetype -enable-shared
- [[email protected] ~]# make
- [[email protected] ~]# make install
7.jpeg 是用于提供 jpeg 图片格式支持的函数库.
- [[email protected] ~]# tar -xzvf jpegsrc.v9a.tar.gz
- [[email protected] ~]# cd jpeg-9a
- [[email protected] ~]# ./configure --prefix=/usr/local/jpeg --enable-shared
- [[email protected] ~]# make
- [[email protected] ~]# make install
8.libgd 是用于提供图形处理的服务.
- [[email protected] ~]# tar -xzvf libgd-2.1.0.tar.gz
- [[email protected] ~]# cd libgd-2.1.0
- [[email protected] ~]# ./configure --prefix=/usr/local/libgd --with-jpeg=/usr/local/jpeg --with-freetype=/usr/local/freetype --with-tiff=/usr/local/tiff --with-vpx=/usr/local/libvpx
- [[email protected] ~]# make
- [[email protected] ~]# make install
9.tlib 是用于提供图片生成函数库的服务程序.
- [[email protected] ~]# tar -xzvf t1lib-5.1.2.tar.gz
- [[email protected] ~]# cd t1lib-5.1.2
- [[email protected] ~]# ./configure --prefix=/usr/local/t1lib --enable-shared
- [[email protected] ~]# make
- [[email protected] ~]# make install
- [[email protected] ~]# ln -s /usr/lib64/libltdl.so.7 /usr/lib/libltdl.so
- [[email protected] ~]# cp -a frp /usr/lib64/libXpm.so*/usr/lib/
10. 编译安装 PHP 程序.(这里既可以使用 5.x 也可以使用 7.x 参数通用)
- [[email protected] ~]# tar -xzvf PHP-7.0.0.tar.gz
- [[email protected] ~]# yum install -y libxml2 libxml2-devel libcurl libcurl-devel libXpm-devel
- [[email protected] ~]# cd PHP-7.0.0
- [[email protected] ~]# export LD_LIBRARY_PATH=/usr/local/libgd/lib
- [[email protected] ~]# ./configure --prefix=/usr/local/PHP --with-config-file-path=/usr/local/PHP/etc --with-MySQL=/usr/local/MySQL --with-mysqli=/usr/local/MySQL/bin/mysql_config --with-MySQL-sock=/tmp/MySQL.sock --with-pdo-MySQL=/usr/local/MySQL --with-gd --with-PNG-dir=/usr/local/libpng --with-jpeg-dir=/usr/local/jpeg --with-freetype-dir=/usr/local/freetype --with-xpm-dir=/usr/ --with-vpx-dir=/usr/local/libvpx/ --with-zlib-dir=/usr/local/zlib --with-t1lib=/usr/local/t1lib --with-iconv --enable-libxml --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-opcache --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-SOAP --without-Pear --with-gettext --enable-session --with-mcrypt --with-curl --enable-ctype
- [[email protected] ~]# make
- [[email protected] ~]# make install
配置 PHP 与 Nginx
1. 拷贝相应配置文件
- cp -a PHP.INI-development /usr/local/php7/lib/PHP.INI
- cp -a /usr/local/php7/etc/PHP-fpm.conf.default /usr/local/php7/etc/PHP-fpm.conf
- cp -a sapi/fpm/PHP-fpm /usr/local/bin
2. 编辑配置文件, 在 PHP 文件末尾追加写入以下标★语句
- VIM /usr/local/php7/lib/PHP.INI
- CGI.fix_pathinfo=1 #将注释去掉, 开启 PHP 的 pathinfo 伪静态功能
- max_execution_time = 0 #脚本运行的最长时间, 默认 30 秒
- max_input_time = 300 #脚本可以消耗的时间, 默认 60 秒
- memory_limit = 256M #脚本运行最大消耗的内存, 根据你的需求更改数值, 默认 128M
- post_max_size = 100M #单提交的最大数据, 默认 100M
- upload_max_filesize = 10M #上载文件的最大许可大小, 默认 2M
3. 修改 PHP-fpm 的配置, 在 PHP-fpm 文件中, 修改以下标★语句
- cd /usr/local/php7/etc/PHP-fpm.d/
- cp -a www.conf.default www.conf
- VIM /usr/local/php7/etc/PHP-fpm.d/www.conf
★listen.owner = nobody #解除注释
★listen.group = nobody #解除注释
★user = nginx #将 apache 修改为 nginx
★group = nginx #将 apache 修改为 nginx
4. 修改 nginx 的主配置, 在 server 语句内, 写入以下标★语句
- VIM /usr/local/nginx/conf/nginx.conf
- 38 server {
- 39 listen 80 default_server;
- 40 listen [::]:80 default_server;
- 41 server_name _;
- 42 root /usr/share/nginx/HTML;
- 43
- 44 # Load configuration files for the default server block.
- 45 include /etc/nginx/default.d/*.conf;
- 46
★ location / {
★
★ root HTML;
★ index index.PHP index.HTML index.htm;
★
52 }
53
★ location ~ \.PHP$ {
★ root /usr/local/nginx/HTML;
★ try_files $uri =404;
★ fastcgi_pass 127.0.0.1:9000;
★ fastcgi_index index.PHP;
★ fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
★ include fastcgi_params;
★ }
- error_page 404 /404.HTML;
- location = /40x.HTML {
- }
5. 设置网页目录权限
chown -R nginx:nginx /usr/local/nginx/HTML/
6. 新建 index.PHP 测试页
- VIM /usr/local/nginx/HTML/index.PHP
- <?PHP
- phpinfo();
- ?>
7. 重启服务, 并查看 9000 端口是否启动成功
- /usr/local/nginx/sbin/nginx
- /usr/local/php7/sbin/PHP-fpm
- netstat -npa | grep 9000
8. 配置妥当后, 便可以复制 PHP 管理脚本, 并加入到开机自启动列表
- [[email protected] PHP-7.0.0]# cp -a sapi/fpm/init.d.PHP-fpm /etc/rc.d/init.d/PHP-fpm
- [[email protected] PHP-7.0.0]# chmod 755 -R /etc/rc.d/init.d/PHP-fpm
- [[email protected] PHP-7.0.0]# chkconfig PHP-fpm on
- [[email protected] PHP-7.0.0]#
- [[email protected] PHP-7.0.0]# chkconfig --list PHP-fpm
- Note: This output shows SysV services only and does not include native
- systemd services. SysV configuration data might be overridden by native
- systemd configuration.
- If you want to list systemd services use 'systemctl list-unit-files'.
- To see services enabled on particular target use
- 'systemctl list-dependencies [target]'.
- PHP-fpm 0:off 1:off 2:on 3:on 4:on 5:on 6:off
来源: http://www.bubuko.com/infodetail-3073122.html