lnmp环境是我们大型WEB服务器常用的一个环境了,在这里我来给各位整理了一篇非常详细的lnmp环境编译安装步骤,希望下文可以帮助到各位.
linux采用的是centos,软件包统一放在/usr/local/src目录下.由于 centos源比较老,而且更新起来也比较慢,此处建议你换成163源,提示:如果你真打算采用这种编译安装php的话,请至少安排半天时间来完成,如果是在虚拟机里测试的话,建议调大内存以让时间更短,我本机给了虚拟机2G内存,在安装完成后可以再设置小点内存跑.安装的时候请按顺序安装.
更改yum源:
- #更换到163源
- cd /etc/yum.repos.d/
- wget http://mirrors.163.com/.help/CentOS6-Base-163.repo
- mv CentOS-Base.repo CentOS-Base.repo.bak
- mv CentOS6-Base-163.repo CentOS-Base.repo
- #先更新一下
- yum clean all
- yum makecache
- yum update(这步比较慢,建议先不要运行)
准备lnmp安装环境:先采用yum包安装一些依赖的东西:
- yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers make cmake libtool* git tree
下载一些软件都放在/usr/local/src下,安装libiconv:
- cd /usr/local/src
- wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
- tar zxvf libiconv-1.14.tar.gz
- cd libiconv-1.14/
- ./configure --prefix=/usr/local
- make && make install
在我make && make install的时候出现错误了error:‘gets’ undeclared here(not in a function),如果你不要遇到遇到,如果遇到请做如下修改:(国外的这文章记录了http://www.itkb.ro/kb/linux/patch-libiconv-pentru-glibc-216).
cd libiconv-1.14/srclib
编辑stdio.in.h文件,搜索这句话:
_GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");
把这句删除换成如下几句:
- #if defined(__GLIBC__) && !defined(__UCLIBC__) && !__GLIBC_PREREQ(2, 16)
- _GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");
- #endif
- //然后
- cd ..
- make && make install
下载并安装libmcrypt:
- cd /usr/local/src
- wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz
- tar -zxvf libmcrypt-2.5.7.tar.gz
- cd libmcrypt-2.5.7
- ./configure
- make
- make install
- cd libltdl/
- ./configure --enable-ltdl-install
- make
- make install
下载并安装mbash 网址:http://mhash.sourceforge.net/,下载下来之后用winscp这种工具传到linux的/usr/local/src目录下:
- cd /usr/local/src
- tar -jxvf mhash-0.9.9.9.tar.bz2
- cd mhash-0.9.9.9
- ./configure
- make
- make install
下载并安装mcript:
- cd /usr/local/src
- tar zxvf mcrypt-2.6.8.tar.gz
- cd mcrypt-2.6.8/
- #注意一下这步运行下,不然下面可能报错
- export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
- ./configure
- make
- make install
- ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
- ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
- ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
- ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
- ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
- ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
- ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
- ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
- ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
- ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
安装mysql(mysql5.6.13),先检查一下mysql是否已经存在了,如果存在就删除mysql:
- rpm -qa | grep mysql
- #我的显示如下(如果没有任何显示就表示没有mysql呢)
- mysql-libs-5.1.73-3.el6_5.i686
- #如果存在就卸载mysql
- rpm -e mysql-libs-5.1.73-3.el6_5.i686 --nodeps
- #此时再运行
- rpm -qa | grep mysql
- #发现没有任何显示
添加一个mysql用户(nologin用户并且没有家目录):
useradd -M -s /sbin/nologin mysql
初次编译mysql的时候时间比较长,请耐心等待吧.
- cd /usr/local/src
- tar -zxf mysql-5.6.13.tar.gz
- cd mysql-5.6.13
- #注意下面是cmake .注意点
- cmake .
- make && make install
- #复制mysql配置文件
- cp support-files/my-default.cnf /etc/my.cnf
- #创建系统数据库表
- cd /usr/local/mysql
- scripts/mysql_install_db --user=mysql
- #更改权限
- cd /usr/local/mysql
- chown -R root:mysql .
- chown -R mysql:mysql data
- #把mysql添加到系统服务中去
- cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
- #启动mysql启动的时候,一直不动的话按下enter.,就行了
- /usr/local/mysql/bin/mysqld_safe --user=mysql &
- #查看mysql是否启动
- netstat -tunpl | grep 3306
- #或者pstree,看到有mysqld进程也行
- #修改mysql密码
- /usr/local/mysql/bin/mysqladmin -u root password "xxxxx"
- #进入mysql中删除空密码和匿名用户
- /usr/local/mysql/bin/mysql -u root -p
- select host,user,password from mysql.user;
- delete from mysql.user where password="" or user="";
- flush privileges;
- #也可以在mysql修改用户密码(只是说明一下)
- set password form 'root'@'localhost' = password('123456');
- #关闭mysql的时候可以使用
- /usr/local/mysql/bin/mysqladmin -u root -p shutdown
- //Vevb.com
- #把mysql添加到开机启动
- vim /etc/rc.local
- #在最后再加一行
- /usr/local/mysql/bin/mysqld_safe --user=mysql &
- #最后做一个软链接方便使用mysql
- ln -s /usr/local/mysql/bin/mysql /usr/bin/
nginx安装(此处我本人安装了基于nginx改版的tengine,nginx的安装跟这基本完全一样,你完全可以参照并完整安装).
nginx安装前编译安装pcre zlib openssl[openssl安装用./config]
pcre安装:
- cd /usr/local/src
- tar -jxf pcre-8.31.tar.bz2
- cd pcre-8.31
- ./configure
- make && make install
zlib安装:
- cd /usr/local/src
- tar -zxf zlib-1.2.8.tar.gz
- cd zlib-1.2.8
- ./configure
- make && make install
openssl安装:
- cd /usr/local/src
- tar -zxf openssl-1.0.1c.tar.gz
- cd openssl-1.0.1c
- ./config
- make && make install
- //注意这时我安装tengine了,如果你用nginx,完全参照安装就行
- cd /usr/local/src
- useradd -M -s /sbin/nologin nginx
- #如果是nginx,请把这里换成nginx
- tar -zxf tengine-2.0.1.tar.gz
- cd tengine-2.0.1
- #注意的是with-pcre这些都指向/usr/local/src这种目录,这非常奇怪
- ./configure --with-pcre=/usr/local/src/pcre-8.31 --with-http_ssl_module --with-zlib=/usr/local/src/zlib-1.2.8 --with-openssl=/usr/local/src/openssl-1.0.1c --with-http_concat_module=shared(注:如果是nginx的话不需要 --with-http_concat_module=shared)
- make && make install
- #做软链接方便nginx启动
- ln -s /usr/local/nginx/sbin/nginx /usr/bin/
- #修改nginx配置文件
- vim /usr/local/nginx/conf/nginx.conf
把#user nobody 改成 user nginx;
- #测试一下nginx配置文件
- nginx -t
- #启动nginx
- nginx
- #说一下nginx启动的一些事情
- #一般来说在nginx的配置文件修改后进行如下操作,nginx -t检测一下配置文件是否正确,如果正确的话
- #再使用nginx -s reload 使nginx平滑启动
- #把nginx添加到开机启动
- vim /etc/rc.local
- #在最后新加一行
- /usr/local/nginx/sbin/nginx
- #查看nginx是否启动
- netstat -tunpl | grep 80
- pstree 看下有没有nginx就行了
安装php:
- cd /usr/local/src
- useradd -M -s /sbin/nologin php
- tar -jxf php-5.5.12.tar.bz2
- cd php-5.5.12
- ./configure --enable-opcache --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 --enable-mbstring=all --with-pdo-mysql --enable-sockets --enable-mbstring --enable-fpm --with-curl --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --enable-xml --with-gd --with-mcrypt --with-libxml-dir=/usr --enable-xml --with-openssl --with-iconv=/usr/local/iconv
- make ZEND_EXTRA_LIBS='-liconv'
- make install
- cp php.ini-development /usr/local/php/etc/php.ini
- #配置php-fpm
- cd /usr/local/php/etc
- cp php-fpm.conf.default php-fpm.conf
- vim php-fpm.conf
- 将
- user = nobody
- group = nobody
- 改为
- user = php
- group = php
- #启动php-fpm
- /usr/local/php/sbin/php-fpm
- #把php-fpm添加到开机启动
- vim /etc/rc.local
- #最后一行加上
- /usr/local/php/sbin/php-fpm
- #修改nginx配置文件使之支持php
- vim /usr/local/nginx/conf/nginx.conf
- #location ~ /.php$ {
- # root html;
- # fastcgi_pass 127.0.0.1:9000;
- # fastcgi_index index.php;
- # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
- # include fastcgi_params;
- #}
- 修改为
- location ~ /.php$ {
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- include fastcgi_params;
- }
- #也顺便把gzip给开启下吧
gzip on前面的#去掉
修改完配置之后:nginx -s reload
ok,现在来测试一下环境.
- cd /usr/local/nginx
- cd html
- vim test.php
写上:<?php phpinfo();?>
然后访问一下localhost/test.php,正常的话,就一切正常了,如果出问题的话,请看nginx有没有启动,php-fpm有没有启动,注明几个问题如果修改php配置文件的时候,需要重启php-fpm就可以了.
- #先关闭php-fpm
- pkill php-fpm
- #然后运行
- /usr/local/php/sbin/php-fpm
- #就可以了可以看到php-fpm就启动了pstree可以看到这个进程
- pstree
为了更方便的使用php和php-fpm,我做了两个链链接.
- ln -s /usr/local/php/sbin/php-fpm /usr/bin/
- ln -s /usr/local/php/bin/php /usr/bin/
lnmp安装就彻底完成了,有什么问题可以联系我,这里没没讲到如何优化一些配置的问题,以后可以会写文章分析.
在这里也特别说明一下nginx如何支持path_info这种形式,配置文件中可以这样写,当然可能对你不需要.
- location ~ ^(.+/.php)(.*)$ {
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- fastcgi_split_path_info ^(.+/.php)(.*)$;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- fastcgi_param PATH_INFO $fastcgi_path_info;
- fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
- include fastcgi_params;
- }
新闻热点
疑难解答