title: 编译LNMP部署动态网站环境
date: 2018-11-08 13:59:59
tags:php
LNMP动态网站部署架构是由一套 Linux+Nginx+MySQL+PHP 组成的动态网站系统解决方案.
如下配置环境为:Linux=RHEL7 --> Nginx=1.13 --> MySQL=5.6 --> PHP=7.0 无错误版.html
在使用源码包安装服务程序以前,首先要让安装主机具有编译程序源码的环境,他须要具有C语言,C++语言的编译器,以及常见的编译支持函数库程序,下面咱们将经过Yum仓库来快速部署这些包.mysql
[root@localhost ~]# 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命令,接下来咱们将解压,并编译安装这个包.nginx
[root@localhost ~]# tar -xzvf cmake-2.8.11.2.tar.gz [root@localhost ~]# cd cmake-2.8.11.2 [root@localhost ~]# ./configure [root@localhost ~]# make [root@localhost ~]# make install
Nginx是一款至关优秀的用于部署动态网站的轻量级服务程序,他最初是为俄罗斯门户站点开发的,因其性能稳定,功能丰富,占用内存小且并发能力高而备受用户的信赖,目前诸如,新浪搜狐,网易,腾讯等门户站点均已经使用了此服务.c++
坦白来讲,虽然Nginx服务程序代码质量很是高,代码很规范,技术成熟,模块扩展也很容易,但任然存在很多的问题,好比官方资料对中文的支持较少,可是Nginx服务程序,在近几年来的发展势头迅猛,相信会在轻量级Web服务器市场可以有不错的前景.正则表达式
1.在编译配置Nginx以前咱们还须要解决相关依赖问题,例如用于提供Perl语言兼容的正则表达式库的软件包pcre,就是Nginx程序用于实现伪静态功能必不可少的依赖包,下面咱们先来解压,并编译,安装这个包.算法
[root@localhost ~]# tar -xzvf pcre-8.35.tar.gz [root@localhost ~]# cd pcre-8.35 [root@localhost ~]# ./configure --prefix=/usr/local/pcre [root@localhost ~]# make [root@localhost ~]# make install
2.紧接着继续编译安装openssl,这个工具是用与提供网站加密证书服务的程序文件,安装时须要自定义服务程序的安装目录,以便于后期调用命令是更方便.sql
[root@localhost ~]# tar -xzvf openssl-1.0.1h.tar.gz [root@localhost ~]# cd openssl-1.0.1h [root@localhost ~]# ./config --prefix=/usr/local/openssl [root@localhost ~]# make [root@localhost ~]# make install
3.在openssl安装后,默认会在/usr/local/openssl/bin目录中提供不少可用命令,咱们将这个目录添加到PATH环境变量里,方便后期的调用.数据库
[root@localhost ~]# echo "export PATH=$PATH:/usr/local/openssl/bin" >> /etc/profile [root@localhost ~]# source /etc/profile [root@localhost ~]# openssl OpenSSL>
4.zlib软件包是用户提供压缩功能的函数文件,下面咱们开始编译安装.apache
[root@localhost ~]# tar -xzvf zlib-1.2.8.tar.gz [root@localhost ~]# cd zlib-1.2.8 [root@localhost ~]# ./configure --prefix=/usr/local/zlib [root@localhost ~]# make [root@localhost ~]# make install
5.在安装好zlib后,下面咱们进入编译Nginx的前戏阶段,建立一个Nginx用户
[root@localhost ~]# useradd -s /sbin/nologin -M nginx
6.下面开始编译安装Nginx,这里咱们须要指定--user与--group参数,也就是用户与组,在使用参数openssl和zlib库时应该指定具体位置.
[root@localhost ~]# tar -xzvf nginx-1.13.12.tar.gz [root@localhost ~]# cd nginx-1.13.12 [root@localhost ~]# ./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 [root@localhost ~]# make [root@localhost ~]# make install
7.启动Nginx,并设置开机自启动
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t #检测配置文件正确性 [root@localhost ~]# /usr/local/nginx/sbin/nginx #启动Nginx [root@localhost ~]# kill -QUIT $(cat /usr/local/nginx/logs/nginx.pid) #关闭Nginx [root@localhost ~]# kill -HUP $(cat /usr/local/nginx/logs/nginx.pid) #重启Nginx [root@localhost ~]# echo "/usr/local/nginx/sbin/nginx 2> /dev/null" >> /etc/profile
MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle旗下产品.MySQL是最流行的关系型数据库管理系统之一,在WEB应用方面,MySQL是最好的RDBMS应用软件,MySQL是一种关系数据库管理系统,关系数据库将数据保存在不一样的表中,而不是将全部数据放在一个大仓库内,这样就增长了速度并提升了灵活性.这里须要注意的是因为MySQL5.7之后产品闭源了,后期的话可使用MariaDB替代.
1.在编译MySQL过程当中,咱们须要建立一个mysql的系统用户,专门负责运行MySQL数据库,记得把这个帐户变成一个不可登陆的用户,且也无需建立家目录,提升系统安全性.
[root@localhost ~]# useradd -s /sbin/nologin -M mysql
2.建立一个用于保存MySQL程序,和数据库文件的目录,并把该目录全部者和所属组改为mysql,其中/usr/local/mysql用于保存MySQL数据程序,而/usr/local/mysql/data则用来保存数据库文件.
[root@localhost ~]# mkdir -p /usr/local/mysql [root@localhost ~]# mkdir -p /usr/local/mysql/data [root@localhost ~]# chown -Rf mysql:mysql /usr/local/mysql/
3.安装Ncurses,若不安装ncurses编译MySQL时会报错
[root@localhost ~]# yum install -y ncurses-devel [root@localhost ~]# tar -xzvf ncurses-6.1.tar.gz [root@localhost ~]# cd ncurses-6.1 [root@localhost ~]# ./configure --with-shared \ --without-debug \ --without-ada \ --enable-overwrite [root@localhost ~]# make [root@localhost ~]# make install # --without-ada参数为设定不编译为ada绑定,因进入chroot环境不能使用ada ; # --enable-overwrite参数为定义把头文件安装到/tools/include下而不是/tools/include/ncurses目录 # --with-shared 生成共享库
4.接下来,开始解压编译MySQL,因为MySQL体积过大,编译时间很长,耐心等待吧
[root@localhost ~]# tar -xzvf mysql-5.6.19.tar.gz [root@localhost ~]# cd mysql-5.6.19 [root@localhost ~]# 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 [root@localhost ~]# make [root@localhost ~]# make install
5.下面进入配置环节,首先删除/etc/my.cnf默认配置文件,而后在MySQL程序目录内,找到一个名为mysql_install_db的脚本文件,执行这个脚本,并使用--user参数指定MySQL服务的对应帐号名称,使用--basedir指定MySQL程序保存目录,使用--datadir指定数据库目录.
[root@localhost ~]# rm -fr /etc/my.cnf [root@localhost ~]# cd /usr/local/mysql/ [root@localhost 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的权限.
[root@localhost mysql]# cp -a my.cnf /etc/my.cnf [root@localhost mysql]# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld [root@localhost mysql]# chmod 755 -R /etc/rc.d/init.d/mysqld
7.修改刚刚的配置文件,改basedir=/usr/local/mysql,datadir=/usr/local/mysql/data这一行.
[root@localhost 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开机自启动的配置.
[root@localhost ~]# /etc/rc.d/init.d/mysqld start [root@localhost ~]# /usr/local/mysql/bin/mysqld_safe --user=mysql & [root@localhost ~]# chkconfig mysqld on [root@localhost ~]# 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 [root@localhost ~]# echo "/usr/local/mysql/bin/mysqld_safe --user=mysql &" >> /etc/rc.local
9.添加环境变量,使用相对路径命令,这样就能直接访问了.
[root@localhost ~]# echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile [root@localhost ~]# source /etc/profile [root@localhost ~]# 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数据库还会调用一些函数库,在这里咱们把它们复制到指定的位置吧.
[root@localhost ~]# mkdir /var/lib/mysql [root@localhost ~]# ln -s /usr/local/mysql/lib/* /var/lib/mysql [root@localhost ~]# ln /usr/local/mysql/include/mysql /usr/include/ [root@localhost ~]# ln -s /usr/local/mysql/include/mysql /usr/include/mysql
11.接着咱们执行 mysql_secure_installation 命令,给MySQL设置初始密码,到此配置完毕
[root@localhost ~]# mysql_secure_installation [root@localhost ~]# 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是一种通用开源脚本语言,语法吸取了C语言、Java和Perl的特色,利于学习使用普遍,主要适用于Web开发领域.PHP独特的语法混合了C、Java、Perl以及PHP自创的语法.它能够比CGI或者Perl更快速地执行动态网页.用PHP作出的动态页面与其余的编程语言相比,PHP是将程序嵌入到HTML文档中去执行,执行效率比彻底生成HTML标记的CGI要高许多,PHP还能够执行编译后代码,编译能够达到加密和优化代码运行,使代码运行更快.
1.在编译安装PHP以前首先要解决依赖的问题,例如yasm是一款常见的开源汇编器.
[root@localhost ~]# tar -xzvf yasm-1.2.0.tar.gz [root@localhost ~]# cd yasm-1.2.0 [root@localhost ~]# ./configure [root@localhost ~]# make [root@localhost ~]# make install
2.libmcrypt是用于加密算法的扩展库程序.
[root@localhost ~]# tar -xzvf libmcrypt-2.5.8.tar.gz [root@localhost ~]# cd libmcrypt-2.5.8 [root@localhost ~]# ./configure [root@localhost ~]# make [root@localhost ~]# make install
3.libvpx是用于提供视频编码器服务的程序.
[root@localhost ~]# tar -xjvf libvpx-v1.3.0.tar.bz2 [root@localhost ~]# cd libvpx-v1.3.0 [root@localhost ~]# ./configure --prefix=/usr/local/libvpx --enable-shared --enable-vp9 [root@localhost ~]# make [root@localhost ~]# make install
4.tiff是用于提供标签图像文件格式的服务程序.
[root@localhost ~]# tar -xzvf tiff-4.0.3.tar.gz [root@localhost ~]# cd tiff-4.0.3 [root@localhost ~]# ./configure --prefix=/usr/local/tiff --enable-shared [root@localhost ~]# make [root@localhost ~]# make install
5.libpng是用于提供png图片格式支持函数库的服务程序.
[root@localhost ~]# tar -xzvf libpng-1.6.12.tar.gz [root@localhost ~]# yum install -y zlib zlib-devel [root@localhost ~]# ./configure --prefix=/usr/local/libpng --enable-shared [root@localhost ~]# make [root@localhost ~]# make install
6.freetype是用于提供字体支持引擎的服务.
[root@localhost ~]# tar -xzvf freetype-2.5.3.tar.gz [root@localhost ~]# cd freetype-2.5.3 [root@localhost ~]# ./configure --prefix=/usr/local/freetype -enable-shared [root@localhost ~]# make [root@localhost ~]# make install
7.jpeg是用于提供jpeg图片格式支持的函数库.
[root@localhost ~]# tar -xzvf jpegsrc.v9a.tar.gz [root@localhost ~]# cd jpeg-9a [root@localhost ~]# ./configure --prefix=/usr/local/jpeg --enable-shared [root@localhost ~]# make [root@localhost ~]# make install
8.libgd是用于提供图形处理的服务.
[root@localhost ~]# tar -xzvf libgd-2.1.0.tar.gz [root@localhost ~]# cd libgd-2.1.0 [root@localhost ~]# ./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 [root@localhost ~]# make [root@localhost ~]# make install
9.tlib是用于提供图片生成函数库的服务程序.
[root@localhost ~]# tar -xzvf t1lib-5.1.2.tar.gz [root@localhost ~]# cd t1lib-5.1.2 [root@localhost ~]# ./configure --prefix=/usr/local/t1lib --enable-shared [root@localhost ~]# make [root@localhost ~]# make install [root@localhost ~]# ln -s /usr/lib64/libltdl.so.7 /usr/lib/libltdl.so [root@localhost ~]# cp -a frp /usr/lib64/libXpm.so* /usr/lib/
10.编译安装PHP程序.(这里既可使用5.x也可使用7.x参数通用)
[root@localhost ~]# tar -xzvf php-7.0.0.tar.gz [root@localhost ~]# yum install -y libxml2 libxml2-devel libcurl libcurl-devel libXpm-devel [root@localhost ~]# cd php-7.0.0 [root@localhost ~]# export LD_LIBRARY_PATH=/usr/local/libgd/lib [root@localhost ~]# ./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 [root@localhost ~]# make [root@localhost ~]# make install
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; ★ } 62 63 error_page 404 /404.html; 64 location = /40x.html { 65 }
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管理脚本,并加入到开机自启动列表
[root@localhost php-7.0.0]# cp -a sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm [root@localhost php-7.0.0]# chmod 755 -R /etc/rc.d/init.d/php-fpm [root@localhost php-7.0.0]# chkconfig php-fpm on [root@localhost php-7.0.0]# [root@localhost 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