LNMP环境搭建(Ubuntu14.04LTS+Nginx1.8.0+Mysql5.5.29+PHP5.6.13)原创

LNMP环境搭建(Ubuntu14.04LTS+Nginx1.8.0+Mysql5.5.29+PHP5.6.13)

该文章由本人参考资料并实际安装测试整理 小伙伴们转载请注明
工作需要,想练习一下自己搭建LNMP环境。 但上网搜索大多是CentOS 或者 Red Hat等系统的文档且时间大多有些久远,版本也比较老一点。(估计是服务器图一个稳定吧!)
而本地环境是Ubuntu14.04的,大多数文档都不好用,经过多次试验总结此文档方便以后使用。
因为搭建环境仅是用于个人开发,所以如果有那些地方有毛病或缺陷请指正并说明原因 万分感谢!!!

注:安装过程中如果缺少关联软件包可以直接apt-get便捷安装

I.检查编译工具 gcc,g++, make, cmake是否已经安装,如果没有安装:

sudo apt-get install -y xxxxx

II.下载所需软件包(注:请自行到各官网下载)

  • libxml2-2.9.0.tar.gz
  • libmcrypt-2.5.7.tar.gz
  • libpng-1.6.7.tar.gz
  • jpegsrc.v6b.tar.gz
  • freetype-2.4.10.tar.gz
  • autoconf-2.68.tar.gz
  • libgd-2.1.0.tar.gz
  • curl-7.39.0.tar.gz
  • pcre-8.33.tar.gz
  • nginx-1.8.0.tar.gz
  • mysql-5.5.29.tar.gz
  • php-5.6.13.tar.gz

    III. 编译安装各软件包

    1.编译安装libxml2

sudo tar –zvxf libxml2-2.9.0.tar.gz -C /usr/local/src/
cd /usr/local/src/libxml2-2.9.0
sudo ./configure–prefix=/usr/local/libxml
sudo make
sudo make install

2.编译安装libmcrypt

sudo tar –zvxf libmcrypt-2.5.7.tar.gz -C /usr/local/src/
cd /usr/local/src/libmcrypt-2.5.7
sudo ./configure –prefix=/usr/local/libmcrypt
sudo make
sudo make install

3.安装libpng库

sudo tar –zvxf libpng-1.6.7.tar.gz -C /usr/local/src/
cd /usr/local/src/libpng-1.6.7
sudo ./configure –prefix=/usr/local/libpng
sudo make
sudo make install

4.安装jpeg6

手动创建安装需要的目录,在安装时不能自动创建
sudo mkdir /usr/local/jpeg6
sudo mkdir /usr/local/jpeg6/bin
sudo mkdir /usr/local/jpeg6/lib
sudo mkdir /usr/local/jpeg6/include
sudo mkdir –p /usr/local/jpeg6/man/man1
sudo tar –zvxf jpegsrc.v6b.tar.gz -C /usr/local/src/
cd /usr/local/src/jpeg-6b
sudo ./configure –prefix=/usr/local/jpeg6 –enable-shared –enable-static
sudo make
sudo make install

可能遇到错误:

./libtool –mode=compile gcc -O2 -I. -c ./jcapimin.c
make: ./libtool:命令未找到 make: * [jcapimin.lo] 错误 127
./libtool –mode=compile gcc -O2 -I. -c ./cjpeg.c
make: ./libtool:命令未找到
make:
* [cjpeg.lo] 错误 127

解决方法 :安装libtool

sudo apt-get install -y libtool
sudo cp /usr/share/libtool/config/config.guess /usr/local/src/jpeg-6b/
sudo cp /usr/share/libtool/config/config.guess /usr/local/src/jpeg-6b/

执行 sudo make clean 重新执行 ./configure,make,make install

5.安装freetype

sudo tar –zvxf freetype-2.4.10.tar.gz -C /usr/local/src/
cd /usr/local/src/freetype-2.4.10
sudo ./configure –prefix=/usr/local/freetype sudo make sudo make install

可能遇到错误:

rm -f /usr/local/modules/freetype/include/freetype2/freetype/cache/
rmdir /usr/local/modules/freetype/include/freetype2/freetype/cache
rm -f /usr/local/modules/freetype/include/freetype2/freetype/internal/

rmdir /usr/local/modules/freetype/include/freetype2/freetype/internal
rmdir: ‘/usr/local/modules/freetype/include/freetype2/freetype/internal’:
没有那个文件或目录
make: [install] 错误 1 (忽略)
……

解决方法:
sudo mkdir usr/local/modules/freetype/include/freetype2/freetype/internal
sudo make install

另一种可能遇到的错误 :

The following settings are used:
platform unix
compiler cc
configuration directory ./builds/unix
configuration rules ./builds/unix/unix.mk
If this does not correspond to your system or settings please remove the file
‘config.mk’ from this directory then read the INSTALL file for help.
Otherwise, simply type ‘make’ again to build the library,
or ‘make refdoc’ to build the API reference (the latter needs python).
make: Nothing to be done for ‘unix’.

解决方法:
cd builds/unix
sudo ./configure
cd ../../ sudo make

6.安装autoconf库

安装autoconf需要安装m4软件,不然configure不会通过的,而且最好默认安装,不指定prefix,否则后面安装php扩展会比较麻烦。
sudo tar –zvxf autoconf-2.68.tar.gz -C /usr/lcoal/src/
cd /usr/lcoal/src/autoconf-2.68 sudo ./configure
sudo make
sudo make install
_安装大于等于2.69的autotype包 可能会遇到错误: _

checking for GNU M4 that supports accurate traces… configure: error: no acceptable m4 could >>> be found in $PATH.
GNU M4 1.4.6 or later is required; 1.4.16 or newer is recommended.
GNU M4 >>> 1.4.15 uses a buggy replacement strstr on some systems. Glibc 2.9 -

解决方法 下载 m4相应依赖

1.sudo apt-get install m4 (本地试验不成功 但是网上说有可以成功解决的例子)

  1. 下载m4-1.4.9源码包

    sudo tar -zxvf m4-1.4.9.tar.gz -C /usr/local/src
    cd m4-1.4.9/
    sudo ./configure
    sudo make && sudo make install
    export PATH=/usr/local/src/m4-1.4.9/src:$PATH
    cd aurovonf-2.69/
    sudo ./configure
    sudo make && sudo make install

7.安装gd库

sudo tar –zvxf gd-2.0.35.tar.gz -C /usr/lcoal/src/
cd /usr/lcoal/src/gd-2.0.35
sudo ./configure –prefix=/usr/local/gd2 –with-jpeg=/usr/local/jpeg6 –with-png=/usr/local/libpng –with-freetype=/usr/local/freetype
sudo make
sudo make install
(PS:首次实际安装过程中遇到了一系列错误 最后的原因是安装包本身存在问题 到官网重新下载后顺利安装 可能需要安装的依赖 libxpm-dev)

8.安装curl扩展

sudo tar -zxvf curl-7.39.0.tar.gz -C /usr/lcoal/src/
cd /usr/lcoal/src/curl-7.39.0
sudo ./configure –prefix=/usr/local/curl
sudo make
sudo make install

9.安装配置Nginx

首先下载pcre源码包并解压到/usr/loca/src/目录 不需要编译
sudo tar -zxvf pcre-8.33.tar.gz -C /usr/local/src/

_如果不下载pcre源码包 也可以通过: _
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install openssl libssl-dev
来满足依赖关系

创建用户及用户组:
sudo groupadd nobody
sudo useradd -r -g nobody nobody

开始安装Nginx:

sudo tar -xzvf nginx-1.8.0.tar.gz -C /usr/local/src/

cd /usr/local/src/nginx-1.8.0

sudo ./configure –prefix=/usr/local/nginx –sbin-path=/usr/local/nginx/sbin/nginx –conf-path=/usr/local/nginx/conf/nginx.conf –error-log-path=/usr/local/nginx/logs/error.log –http-log-path=/usr/local/nginx/logs/access.log –pid-path=/usr/local/nginx/var/nginx.pid –lock-path=/usr/local/nginx/var/nginx.lock –http-client-body-temp-path=/dev/shm/nginx_temp/client_body –http-proxy-temp-path=/dev/shm/nginx_temp/proxy –http-fastcgi-temp-path=/dev/shm/nginx_temp/fastcgi –user=nobody –group=nobody –with-cpu-opt=pentium4F –without-select_module –without-poll_module –with-http_realip_module –with-http_sub_module –with-http_gzip_static_module –with-http_stub_status_module –without-http_ssi_module –without-http_userid_module –without-http_geo_module –without-http_memcached_module –without-http_map_module –without-mail_pop3_module –without-mail_imap_module –without-mail_smtp_module –with-pcre=/usr/local/src/pcre-8.33/

sudo make

sudo make install

配置Nginx:
sudo vim /usr/local/nginx/conf/nginx.conf
修改内容:
(root :web目录根据本机修改 )

user  nobody nobody;
worker_processes  4;

#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;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '

    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

   server {
    listen 80;
    server_name localhost;
     #目录根据本机修改
    root /home/yyg/www;
    index index.html index.htm index.php;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
         try_files $uri =404;

        include fastcgi.conf;
        fastcgi_pass 127.0.0.1:9000;
    }
 }


  # another virtual host using mix of IP-, name-, and port-based configuration
  #
  #server {
  #    listen       8000;
  #    listen       somename:8080;
  #    server_name  somename  alias  another.alias;

  #    location / {
  #        root   html;
  #        index  index.html index.htm;
  #    }
  #}

#虚拟主机test.com

server {
listen 80;
server_name test.com;

     root /home/yyg/www/laravelproject/public;
     index index.html index.htm index.php;

     location / {
            try_files $uri $uri/ /index.php$is_args$args;
     }

     location ~ \.php$ {
             try_files $uri =404;

             include fastcgi.conf;
             fastcgi_pass 127.0.0.1:9000;
     }

}
  # HTTPS server
  #
  #server {
  #    listen       443 ssl;
  #    server_name  localhost;

  #    ssl_certificate      cert.pem;
  #    ssl_certificate_key  cert.key;

  #    ssl_session_cache    shared:SSL:1m;
  #    ssl_session_timeout  5m;

  #    ssl_ciphers  HIGH:!aNULL:!MD5;
  #    ssl_prefer_server_ciphers  on;

  #    location / {
  #        root   html;
  #        index  index.html index.htm;
  #    }
  #}

}

nginx的虚拟主机配置个人觉得要比apache的虚拟主机配置要简单快捷一点
别忘了在nginx配置文件中添加虚拟主机后要在hosts文件中添加对应的虚拟主机地址
/etc/hosts文件中例:

虚拟主机

127.0.0.1 test.com

Nginx启动:
(PS:Nginx启动脚本一般都是CentOS的,Ubuntu版本的暂时没找到所以暂时先不用启动脚本了。)
因为Nginx没有自带启动脚本直接运行启动文件
sudo /usr/local/nginx/sbin/nginx
因为编译时配置实体连接文件存放在
–http-client-body-temp-path=/dev/shm/nginx_temp/client_body
而/dev/shm/ 目录又是存放临时文件目录重启后会消失
所以将创建目录命令与Nginx启动命令写入/etc/rc.local 开机自动运行
sudo vim /etc/rc.local
在exit 0 上面加入:
mkdir -p /dev/shm/nginx_temp/client_body /usr/local/nginx/sbin/nginx

因为Nginx 并没有自带启动脚本 所以可以将nginx的可执行文件/usr/local/nginx/sbin/nginx设置软连接到/bin或者/sbin目录:
sudo ln -s /usr/local/nginx/sbin/nginx /bin/nginx或者/sbin/nginx
或者直接copy到/bin /sbin 目录

或者将nginx执行文件添加到PATH环境变量中:
sudo vim /etc/environment
在PARH=’XXXX:XXX:XXX’中添加nginx执行文件目录 重启后生效.

设置完之后可以使用:
sudo nginx 启动nginx服务
sudo nginx -s stop 停止nginx服务

也可以使用 sudo ps -aux|grep nginx 或者 sudo netstat -tnlp|grep nginx 或者 80端口查看nginx master进程 使用sudo pkill -9 nginx或者master进程号 来终止nginx进程

10.安装Mysql

解压Mysql

sudo tar -xzvf -mysql-5.5.29.tar.gz -C /usr/local/src/

cd /usr/local/src/mysql-5.5.29

sudo cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_READLINE=1 -DWITH_SSL=bundled -DWITH_EMBEDDED_SERVER=1 -DENABLED_LOCAL_INFILE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITHOUT_PARTITION_STORAGE_ENGINE=1 -DWITH_DEBUG=0

sudo make

sudo make install

如果依赖不足 apt-get install libncurses5-dev

sudo rm CMakeCache.txt 重新执行cmake

创建mysql 用户

sudo groupadd mysql
sudo useradd -r -g mysql mysql

权限设置及授权表的安装

cd /usr/local/mysql
sudo chown -R mysql .
sudo chgrp -R mysql .
sudo scripts/mysql_install_db –user=mysql
sudo chown -R root .
sudo chown -R mysql data

创建配置文件

sudo cp support-files/my-medium.cnf /etc/my.cnf

启动mysql服务

sudo ./bin/mysqld_safe –user=mysql &
netstat -tnl | grep 3306

配置mysql服务

sudo cp support-files/mysql.server /etc/init.d/mysql
sudo update-rc.d mysql defaults

用户账户控制

sudo ./bin/mysql -uroot

mysql> DELETE FROM mysql.user WHERE Host=’localhost’ AND User=’’;
mysql>SET PASSWORD FOR ‘root‘@’localhost’=PASSWORD(‘123456’);
启动报错:无法启动Couldn’t find MySQL server (/usr/bin/mysqld_safe)”

sudo rm /etc/mysql/my.cnf

将mysql命令加入/sbin目录 可以直接在命令行直接使用 mysql -u××× -p登陆
sudo cp /usr/local/mysql/bin/mysql /sbin/
添加环境变量或者设置软连接都可以

11.安装PHP

sudo tar -zxvf php-5.6.13.tar.gz -C /usr/local/src/

cd /usr/local/src/php-5.6.13

sudo ./configure –prefix=/usr/local/php –with-config-file-path=/usr/local/php/etc –with-pdo-mysql=/usr/local/mysql –with-mysql=/usr/local/mysql –with-mysqli=/usr/local/mysql/bin/mysql_config –with-libxml-dir=/usr/local/libxml2 –with-png-dir=/usr/local/libpng –with-jpeg-dir=/usr/local/jpeg –with-freetype-dir=/usr/local/freetype –with-gd=/usr/local/gd –with-mcrypt=/usr/local/libmcrypt –with-curl=/usr/local/curl –with-openssl –with-mhash –with-zlib –enable-soap –enable-mbstring=all –enable-sockets –enable-fpm –enable-mbstring –enable-pcntl –enable-bcmath –enable-shmop –enable-sysvsem –enable-inline-optimization –enable-xml –enable-mbregex –enable-zip –enable-opcache=no

sudo make

sudo make install

copy PHP配置文件
sudo cp php.ini-development /usr/local/php/etc/php.ini

copy PHP-FPM配置文件
cd /usr/local/php/etc
sudo cp php-fpm.conf.default php-fpm.conf(相关配置信息可自行更改)
注意本次安装配置 php-fpm.pid 文件路径为 pid=/usr/local/php/var/run/php-fpm.pid

启动PHP-FPM
sudo /usr/local/php/sbin/php-fpm
可以使用 sudo update-rc php-fpm defaults 或者 sysv-rc-conf php-fpm on 添加开机启动

在wed目录下新建 test.php
vim test.php

<?php
phpinfo();

打开浏览器 访问localhost/test.php 搞定~!

php-redis扩展编译

1: 到pecl.php.net 搜索redis
2: 下载stable版(稳定版)扩展
3: 解压,
4: 执行/php/path/bin/phpize (作用是检测PHP的内核版本,并为扩展生成相应的编译配置)
5: configure –with-php-config=/php/path/bin/php-config
6: make && make install
引入编译出的redis.so插件
1: 编辑php.ini
2: 添加redis.so扩展路径

12.参考资料

http://nonfu.me/p/7079.html
http://blog.csdn.net/beautifulencounter/article/details/45815551
http://www.cnblogs.com/whoamme/p/3678795.html
http://huoding.com/2013/10/23/290