在 Ubuntu 14.04 上搭建 LNMP 环境

背景:

1
2
3
4
2核8G, Intel Haswell CPU、DDR4 内存
40G ssd 系统盘 + 20G ssd 数据盘
ubuntu14.04
2Mbps带宽

PS : 脑子抽了我才会在 Ubuntu 16.04 出来后还选 14.04 系统。。。。后来多做了好多事情。。。

PHP编译安装部分请参考 编译安装的 PHP7.0 升级 7.2

准备工作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
apt-get update //更新apt-get 
apt-get install unzip //安装unzip
apt-get install //安装git
//生成ssh密钥,并配置code平台
apt-get install //安装Nginx, ubuntu 14.04 的主坑之一,默认Nginx版本过低,后面升级HTTP2又要重新升级版本
service nginx start // 启动nginx
apt-get install redis-server redis-tools // 安装Redis,检查是否绑定ip,默认bind 127.0.0.1
apt-get install memcached // 安装Memcached,telnet localhost 11211可访问,外网不可访问
apt-get -y install mysql-server mysql-client // 安装mysql,中间设置root-pwd

//安装依赖库
apt-get install libxml2-dev libxml2
apt-get install openssl libssl-dev mcrypt
(find / -name libssl.so)
(ln -s /usr/lib/x86_64-linux-gnu/libssl.so /usr/lib)
apt-get install curl libcurl3
apt-get install libkrb5-dev \
libc-client2007e \
libc-client2007e-dev \
libcurl4-openssl-dev \
libbz2-dev \
libjpeg-dev \
libmcrypt-dev \
libxslt1-dev \
libxslt1.1 \
libpq-dev \
libpng12-dev \
libfreetype6-dev \
build-essential

编译安装php

源码下载:PHP官网 此处用的是 PHP 7.0 版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
./configure \
--prefix=/usr/local/php7 \
--with-config-file-path=/usr/local/php7/etc \
--with-zlib-dir \
--with-freetype-dir \
--enable-mbstring \
--with-libxml-dir=/usr \
--enable-soap \
--enable-calendar \
--with-curl \
--with-mcrypt \
--with-zlib \
--with-gd \
--disable-rpath \
--enable-inline-optimization \
--with-bz2 \
--with-zlib \
--enable-sockets \
--enable-sysvsem \
--enable-sysvshm \
--enable-pcntl \
--enable-mbregex \
--enable-exif \
--enable-bcmath \
--with-mhash \
--enable-zip \
--with-pcre-regex \
--with-pdo-mysql \
--with-mysqli \
--with-mysql-sock=/var/run/mysqld/mysqld.sock \
--with-jpeg-dir=/usr \
--with-png-dir=/usr \
--enable-gd-native-ttf \
--with-openssl \
--with-fpm-user=www-data \
--with-fpm-group=www-data \
--enable-ftp \
--with-imap \
--with-imap-ssl \
--with-kerberos \
--with-gettext \
--with-xmlrpc \
--with-xsl \
--enable-opcache \
--enable-fpm

编译成功后安装:

1
make && sudo make install

配置php-fpm

1
2
3
sudo mv /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
sudo mv /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
sudo cp ./php.ini-production /usr/local/php7/etc/php.ini

修改

/usr/local/php7/etc/php-fpm.d/www.conf:

1
2
3
listen.owner = www-data
listen.group = www-data
listen.mode = 0660

修改fpm监听端口

/usr/local/php7/etc/php-fpm.d/www.conf

1
2
>;listen = 127.0.0.1:9000 #注释掉
>listen = /var/run/php7-fpm.sock

然后启动服务

1
sudo /usr/local/php7/sbin/php-fpm

查看php-fpm进程ID

1
ps aux | grep -i "php-fpm" | grep -v grep  | awk '{print $2}' 

配置nginx反向代理使用:

1
fastcgi_pass unix:/var/run/php7-fpm.sock;

安装php-redis 扩展

参考文章:PHP7安装Redis扩展

1
2
3
4
5
6
$ git clone  https://github.com/phpredis/phpredis.git
$ cd phpredis
$ git checkout php7
$ phpize
$ ./configure
$ sudo make && sudo make install

修改PHP配置文件

/usr/local/php7/etc/php.ini

文末添加:

1
2
[Redis]
extension=redis.so

重启php-fpm

安装php-memcached扩展

参考:How to Install the PHP Memcached Extension

1
2
3
4
5
6
7
8
9
10
11
12
sudo apt-get install gcc make autoconf libc-dev pkg-config
sudo apt-get install zlib1g-dev
sudo apt-get install libmemcached-dev
git clone https://github.com/php-memcached-dev/php-memcached
cd php-memcached
git checkout php7
phpize
./configure --disable-memcached-sasl
make
sudo make install
//同上,在 php.ini 中添加extension=memcached.so
//then 重启php-fpm