centos6.x安装部署LNMP环境
因公司业务需求,需要熟悉LNMP架构。利用空余时间自己搭建了LNMP测试环境。下面来详细讲解如何一步一步搭建LNMP
LNMP是Linux,Nginx,Mysql,Php 这种架构的简写。 这是一种常见的web服务架构。
一、Linux
Linux 我们采用的是较为成熟稳定的centos6.6版本,其他版本,这里不做介绍。安装centos6.6最好采用完整安装,以免后期部署服务的时候缺少各种库,麻烦。
查看系统版本:
[root@localhost ~]# cat /etc/redhat-release CentOS release 6.6 (Final)
[root@localhost ~]# uname -r
2.6.32-504.el6.x86_64
可以看出我们系统当前的版本是centos6.6 64位的。
二、Nginx
Nginx 是一款开源的web反向代理服务器,也是一个IMAP/POP3/SMTP服务器。其拥有高并发,高性能,低资源消耗等特点。我们这里采用最新稳定版本Nginx1.12.1
下载Nginx: wget http://nginx.org/download/nginx-1.12.1.tar.gz
[root@localhost ~]# mkdir soft;cd soft [root@localhost soft]# wget http://nginx.org/download/nginx-1.12.1.tar.gz --2017-07-31 11:23:26-- http://nginx.org/download/nginx-1.12.1.tar.gz
下载后,解压nginx安装包。tar zxvf nginx-1.12.1.tar.gz
tar zxvf nginx-1.12.1.tar.gz
进入到nginx解压后文件目录
cd nginx-1.12.1 [root@localhost nginx-1.12.1]# ls auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src [root@localhost nginx-1.12.1]#
因为nginx的rewrite需要pcre支持(perl正则表达式),所以linux环境需要先安装pcre。查看pcre是否已经安装
[root@localhost nginx-1.12.1]# rpm -qa pcre-devel pcre-devel-7.8-6.el6.x86_64
我们的环境已经安装过pcre,所以就不需要安装了。如果未安装,则使用编译安装的方法安装pcre。
另外nginx如果要支持https,则需要安装openssl模块。查看是否安装
[root@localhost nginx-1.12.1]# rpm -qa openssl openssl-devel openssl-1.0.1e-30.el6.x86_64 openssl-devel-1.0.1e-30.el6.x86_64
可以看到openssl也已经安装。所以这时候我们就可以开始nginx的编译安装了。
添加nginx用户
[root@localhost nginx-1.12.1]# useradd nginx -s /sbin/nologin -M
开始编译安装
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx/ --with-http_stub_status_module --with-http_ssl_module
make && make install
至此,nginx安装已经完成。启动nginx
[root@localhost sbin]# ps -ef |grep nginx root 6210 1 0 11:39 ? 00:00:00 nginx: master process ./nginx nginx 6211 6210 0 11:39 ? 00:00:00 nginx: worker process root 6222 2478 0 11:39 pts/0 00:00:00 grep nginx
三、Mysql安装
mysql安装方式有多种,可以yum install mysql,也可以编译安装,或者是二进制安装。这里采用二进制安装(因为比较方便,也可以根据自己的需求定制版本,安装目录等)
二进制安装包和源码安装包的包名是有区别的,如:
mysql二进制安装包: mysql-5.5.32-linux2.6-x86_64.tar.gz
mysql源码安装包: mysql-5.5.32.tar.gz
因此在选择安装包的时候不要选错了。
下载mysql二进制安装包,我们选的版本是5.6.37:
wget https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.37-linux-glibc2.12-x86_64.tar.gz
在 ~/soft/目录解压。
[root@localhost mysql]# cd ~/soft/ [root@localhost soft]# tar xf mysql-5.6.37-linux-glibc2.12-x86_64.tar.gz
解压后将配置文件拷贝到/etc/my.cnf 下
[root@localhost mysql]# cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
创建数据库文件目录 datadir,并修改权限。
[root@localhost mysql]# mkdir -p data;chown -R mysql:mysql data
如果系统内还没有mysql用户,则需要先创建。
useradd -s /sbin/nologin -g mysql -M mysql
接下来,初始化mysql数据库文件
[root@localhost mysql]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --user=mysql
如果过程没有报错,则代表mysql安装成功了。可以启动mysql数据库。为了方便启动,把mysql启动脚本放到/etc/init.d/目录下。
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld
将mysql的环境变量添加到系统环境变量中。
[root@localhost mysql]# echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile
[root@localhost mysql]# source /etc/profile
启动mysql
service mysqld start
因mysql默认的root密码为空,所以需要修改root密码
[root@localhost ~]# mysqladmin -uroot password '123456' # 将root密码修改为123456
四、PHP
PHP程序在开发及运行时会调用一些诸如zlib、gd等函数库,因此需要确认linux环境是否已经安装了zlib,gd。
[root@localhost ~]# rpm -qa zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel zlib-devel-1.2.3-29.el6.x86_64 libxml2-devel-2.7.6-14.el6_5.2.x86_64 libjpeg-turbo-devel-1.2.1-3.el6_5.x86_64
[root@localhost ~]# rpm -qa freetype-devel libpng-devel gd-devel libcurl-devel libxstl-devel
libcurl-devel-7.19.7-37.el6_5.3.x86_64
freetype-devel-2.3.11-14.el6_3.1.x86_64
libpng-devel-1.2.49-1.el6_2.x86_64
从上面可以看出哪些库没有安装,接下来安装这些需要的库。
yum install gd-devel libxstl-devel -y
libiconv这个库无法通过yum安装,我们采用源码安装。
[root@localhost soft]# wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz [root@localhost soft]# tar xf libiconv-1.14.tar.gz [root@localhost soft]# cd libiconv-1.14 [root@localhost libiconv-1.14]# ./configure --prefix=/usr/local/libiconv [root@localhost libiconv-1.14]# make && make install
安装libmcrypt库
在centos默认的yum源里没有libmcrypt-devel,因此我们事先配置epel第三方yum源,具体命令如下:
[root@localhost soft]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo --2017-07-31 14:02:08-- http://mirrors.aliyun.com/repo/epel-6.repo 正在解析主机 mirrors.aliyun.com... 112.124.140.210, 115.28.122.210 正在连接 mirrors.aliyun.com|112.124.140.210|:80... 已连接。 已发出 HTTP 请求,正在等待回应... 200 OK 长度:1083 (1.1K) [application/octet-stream] 正在保存至: “/etc/yum.repos.d/epel.repo” 100%[===================================================================================================================>] 1,083 --.-K/s in 0s 2017-07-31 14:02:08 (160 MB/s) - 已保存 “/etc/yum.repos.d/epel.repo” [1083/1083]) [root@localhost soft]#
然后即可通过yum安装libmcrypt库
[root@localhost soft]# yum install libmcrypt-devel -y
安装mhash和mcrypt加密扩展库
[root@localhost soft]# yum install mhash mcrypt -y
以上依赖库都安装完成之后,就可以开始安装PHP(FastCGI方式)服务了
下载PHP安装包,并解压
[root@localhost soft]# wget http://cn2.php.net/get/php-5.3.27.tar.gz/from/this/mirror [root@localhost soft]# tar xf mirror
[root@localhost soft]# cd php-5.3.27
[root@localhost php-5.3.27]#
编译安装./configure --prefix=/usr/local/php \
--with-mysql=/usr/local/mysql \
--with-iconv-dir=/usr/local/libiconv \
--with-freetype-dir --with-jpeg-dir \
--with-png-dir --with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-safe-mode \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--with-curlwrappers \
--enable-mbregex \
--enable-fpm \
--enable-mbstring \
--with-mcrypt \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--enable-short-tags \
--enable-zend-multibyte \
--enable-static \
--with-xsl \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-ftp
编译PHP软件。
[root@localhost php-5.3.27]# ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/ [root@localhost php-5.3.27]# make
make过程不能出现错误,如出现报错,则要先解决掉,再接着往下
[root@localhost php-5.3.27]# make install Installing PHP SAPI module: fpm Installing PHP CLI binary: /usr/local/php/bin/ Installing PHP CLI man page: /usr/local/php/man/man1/ Installing PHP FPM binary: /usr/local/php/sbin/ Installing PHP FPM config: /usr/local/php/etc/ Installing PHP FPM man page: /usr/local/php/man/man8/ Installing PHP FPM status page: /usr/local/php/share/php/fpm/ Installing build environment: /usr/local/php/lib/php/build/ Installing header files: /usr/local/php/include/php/ Installing helper programs: /usr/local/php/bin/ program: phpize program: php-config Installing man pages: /usr/local/php/man/man1/ page: phpize.1 page: php-config.1 Installing PEAR environment: /usr/local/php/lib/php/ [PEAR] Archive_Tar - installed: 1.3.11 [PEAR] Console_Getopt - installed: 1.3.1 warning: pear/PEAR requires package "pear/Structures_Graph" (recommended version 1.0.4) warning: pear/PEAR requires package "pear/XML_Util" (recommended version 1.2.1) [PEAR] PEAR - installed: 1.9.4 Wrote PEAR system config file at: /usr/local/php/etc/pear.conf You may want to add: /usr/local/php/lib/php to your php.ini include_path [PEAR] Structures_Graph- installed: 1.0.4 [PEAR] XML_Util - installed: 1.2.1 /root/soft/php-5.3.27/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin ln -s -f /usr/local/php/bin/phar.phar /usr/local/php/bin/phar Installing PDO headers: /usr/local/php/include/php/ext/pdo/
至此,PHP安装完成。
配置PHP服务(FastCGI方式)的配置文件 php-fpm.conf
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
启动PHP服务(FastCGI方式)
[root@localhost etc]# /usr/local/php/sbin/php-fpm
[root@localhost etc]#
检查PHP是否已经启动
[root@localhost etc]# ps -ef |grep php root 104600 1 0 14:44 ? 00:00:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf) nginx 104601 104600 0 14:44 ? 00:00:00 php-fpm: pool www nginx 104602 104600 0 14:44 ? 00:00:00 php-fpm: pool www root 104618 2478 0 14:45 pts/0 00:00:00 grep php [root@localhost etc]#
五,以上步骤完成了LNMP各个组件的安装部署,接下来配置NGINX支持PHP访问。
切换到nginx配置文件目录,修改配置文件。
[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# sed -r -i '/^[ ]+#/d' nginx.conf # 删除无用的配置行
修改后的nginx.conf配置文件.
[root@localhost conf]# cat nginx.conf user nginx; worker_processes 1; error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name test.phpweb.com; location / { root html/testphp; index index.html index.htm; } location ~ .*\.(php|php5)?$ { root html/testphp; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } } } [root@localhost conf]#
修改nginx配置后,检查配置,并重载配置文件
[root@localhost conf]# ../sbin/nginx -t nginx: the configuration file /usr/local/nginx//conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx//conf/nginx.conf test is successful [root@localhost conf]#
[root@localhost conf]# ../sbin/nginx -s reload
[root@localhost conf]#
添加测试的php文件
[root@localhost ~]# cd /usr/local/nginx/html/ 50x.html index.html [root@localhost ~]# cd /usr/local/nginx/html/ [root@localhost html]# ls 50x.html index.html [root@localhost html]# mkdir testphp [root@localhost html]# [root@localhost html]# echo "<?php phpinfo(); ?>" >testphp/testpage.php [root@localhost html]# cd testphp/ [root@localhost testphp]# cat testpage.php <?php phpinfo(); ?> [root@localhost testphp]#
在windows主机的hosts文件添加域名解析内容

保存后退出,并用浏览器打开地址http://test.phpweb.com/testpage.php访问测试。结果如下:

如果出现这个页面,恭喜您,LNMP环境搭建成功。
posted on 2017-07-31 10:44 hz_pythoner 阅读(93) 评论(0) 收藏 举报
浙公网安备 33010602011771号