Ubuntu 16.04 编译安装 LNMP 环境

源代码包放在 /usr/local/src 目录下,安装路径在 /usr/local下

编译 Nginx

  • 下载 Nginx 源码包,我选择的是 1.10.3 稳定版

    1
    2
    # cd /usr/local/src
    $ wget http://nginx.org/download/nginx-1.10.3.tar.gz
  • 解压源码包

    1
    2
    $ cd /usr/local/src
    $ tar vfzx nginx-1.10.3.tar.gz
  • 配置安装路径

    1
    2
    $ cd /usr/local/src/nginx-1.10.3/
    $ ./configure --prefix=usr/local/nginx
  • 此时编译报错:没有 C 编译器 ./configure: error: C compiler cc is not found

    1
    sudo apt-get install build-essential
  • 再次配置安装路径,报错:缺少 PCRE 库 ./configure: error: the HTTP rewrite module requires the PCRE library.

    • 下载 pcre 库源码并解压

      1
      2
      3
      $ cd /usr/local/src/
      $ wget https://ftp.pcre.org/pub/pcre/pcre-8.40.tar.gz
      $ tar vfxz pcre-8.40.tar.gz
  • 再次配置安装路径

    1
    2
    $ cd /usr/local/src/nginx-1.10.3/
    $ ./configure --prefix=/usr/local/nginx --with-pcre=/usr/local/src/pcre-8.40
  • 报错:缺少 zlib 库 ./configure: error: the HTTP gzip module requires the zlib library.

    • 如上方法,下载 zlib 源码并解压

      1
      2
      3
      $ cd /usr/local/src/
      $ wget http://www.zlib.net/zlib-1.2.11.tar.gz
      $ tar vfzx zlib-1.2.11.tar.gz
  • 再次配置安装路径

    1
    2
    $ cd /usr/local/src/nginx-1.10.3/
    $ ./configure --prefix=/usr/local/nginx --with-pcre=/usr/local/src/pcre-8.40 --with-zlib=/usr/local/src/zlib-1.2.11
  • 此时成功了 o( ̄▽ ̄)

  • 别忘了最后一步

    1
    make && make install
  • 启动 nginx

    1
    $ /user/local/nginx/sbin/nginx
    • 可能会出现的问题

      1
      2
      3
      4
      5
      6
      The program 'nginx' can be found in the following packages:
      * nginx-core
      * nginx-extras
      * nginx-full
      * nginx-light
      Try: apt install <selected package>
* 解决方案:

1
2
3
4
apt-get install nginx-core
apt-get install nginx-extras
apt-get install nginx-full
apt-get install nginx-light
  • 查看网页

    • 在chrome查看80端口,出现该页面,说明我已经成功安装nginx了o( ̄▽ ̄)

编译 PHP

  • 下载源码包,我选择的是最新稳定版 7.1.4
1
2
3
$ cd /usr/local/src/
$ wget http://cn2.php.net/get/php-7.1.4.tar.gz/from/this/mirror
$ mv mirror php-7.1.4.tar.gz
  • 解压源码包
1
2
$ cd /usr/local/src/
$ tar vfxz php-7.1.4.tar.gz
  • 配置安装路径
1
2
$ cd /usr/local/src/php-7.1.4
$ ./configure --prefix=/usr/local/php --with-gd --enable-gd-native-ttf --enable-gd-jis-conv --enable-mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --enable-mbstring --enable-fpm
  • 报错:缺少xml2-config,需要安装libxml2 configure: error: xml2-config not found. Please check your libxml2 installation.
1
2
# 安装libxml2和libxml2-dev
$ apt-get install libxml2 libxml2-dev
  • 继续配置安装路径,报错:缺少openssl configure: error: Cannot find OpenSSL’s
1
2
# 安装openssl和libssl-dev Ubuntu上没有openssl-develb包
$ apt-get install openssl libssl-dev
  • 继续配置安装路径,报错:configure: error: Cannot find OpenSSL’s libraries

解决方案:http://stackoverflow.com/questions/39919283/configure-error-cannot-find-openssls-libraries-ubuntu-16-04

1
$ apt-get install pkg-config libssl-dev openssl
  • 继续配置安装路径,缺少gd库,但是我找了下,并没有php7.1-gd这个库,退而且其次,安装了jpeg和png的库 configure: error: png.h not found.
1
$ apt-get install libjpeg-dev libpng-dev
  • 继续配置安装路径,这一次终于成功了😄

  • 编译PHP

1
2
$ cd /usr/local/src/php-7.1.4
$ make && make install
  • 几个PHP相关的配置文件
1
2
3
4
$ cd /usr/local/php
$ cp etc/php-fpm.conf.default etc/php-fpm.conf
$ cd /usr/local/src/php-7.1.4
$ cp php.ini-development /usr/local/php/etc/php.ini

Nginx和PHP整合

  • 修改 Nginx 配置文件
1
2
3
4
5
6
7
8
# 位置 /etc/nginx/nginx.conf
location ~ \.php$ {
root /var/wwwroot/test.com/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $DOCUMENT_ROOT$fastcgi_script_name;
include fastcgi_params;
}

配置文件里这些都给你写了,其实只要把注释去掉即可。

然后优雅的重启下 Nginxphp-fpm

1
2
3
4
5
6
7
8
9
10
11
12
# 目录 /usr/local/nginx
$ ./sbin/nginx -s reload
# 目录 /usr/local/php
$ ./sbin/php-fpm
```

报错:**WARNING: Nothing matches the include pattern /usr/local/php/etc/php-fpm.d/*.conf from /usr/local/php/etc/php-fpm.conf at line 125.**

很简单,在 `/usr/local/php/etc/php-fpm.d/` 目录下,把 `www.conf.default` 重命名或复制份,以 `conf` 为后缀即可


- 出现报错:

ERROR: [pool www] cannot get gid for group ‘nobody’

1
2

- 解决:

$ cd /usr/local/php/etc/php-fpm.d/
$ vim www.conf

group 后边的 nobody 改为 www-data

group=www-data

1
2
3
4
5
6
7
8
9
10

然后再启动`php-fpm`。

ok~
![](http://p5x7w1vrd.bkt.clouddn.com/cca4245fe2abe651899ca691fcdcee56.jpg)


### 编译MySQL

* 新增mysql用户组和用户

groupadd mysql
useradd -g mysql mysql

1
2

- 下载源码

目录 /usr/local/src/

$ wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.10.tar.gz

1
2

- 安装说明文档

Preconfiguration setup

shell> groupadd mysql
shell> useradd -r -g mysql -s /bin/false mysql

Beginning of source-build specific instructions

shell> tar zxvf mysql-VERSION.tar.gz
shell> cd mysql-VERSION
shell> mkdir bld
shell> cd bld
shell> cmake ..
shell> make
shell> make install

End of source-build specific instructions

Postinstallation setup

shell> cd /usr/local/mysql
shell> chown -R mysql .
shell> chgrp -R mysql .
shell> bin/mysql_install_db –user=mysql # Before MySQL 5.7.6
shell> bin/mysqld –initialize –user=mysql # MySQL 5.7.6 and up
shell> bin/mysql_ssl_rsa_setup # MySQL 5.7.6 and up
shell> chown -R root .
shell> chown -R mysql data
shell> bin/mysqld_safe –user=mysql &

Next command is optional

shell> cp support-files/mysql.server /etc/init.d/mysql.server

1
2

- 报错`The program 'cmake' is currently not installed. `

$ apt install cmake

1
2


cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_TCP_PORT=3306 -DMYSQL_USER=mysql -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost

1
2
3
4
5


### 安装 nginx-rtmp-module

- 下载 nginx-rtmp-module

git clone https://github.com/sergey-dryabzhinsky/nginx-rtmp-module.git

1
2

- 安装 Nginx 依赖关系

sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev

1
2

- 安装

./configure –prefix=/usr/local/nginx –with-pcre=/usr/local/src/pcre-8.40 –with-zlib=/usr/local/src/zlib-1.2.11 –with-http_ssl_module –add-module=/usr/local/src/nginx-rtmp-module
make -j 1
sudo make install

1
2
3
4
5
6
7
8
9
10
11

> `--add-module=/usr/local/src/nginx-rtmp-module` 写成你 Git 下来的那个路径


- 配置 `Nginx` 文件

`nginx`中的应用程序意味着`rtmp`端点。
基本`uri`语法:`rtmp://nginx_host[:nginx_port]/app_name/stream_name`
我们将使用流作为流名称,因此我们的端点将是:`rtmp://localhost/show/stream`,以后可以作为`http://localhost:8080/hls/stream.m3u8`。

- `rtmp module config`

rtmp {
server {
listen 1935; # Listen on standard RTMP port
chunk_size 4000;

    application show {
        live on;
        # Turn on HLS
        hls on;
        hls_path /mnt/hls/; #是播放的流文件以及视频分段文件的地址,你可以更改任意位置
        hls_fragment 3; #3秒片段
        hls_playlist_length 60; #60秒播放列表。
        # disable consuming the stream from nginx as rtmp
        deny play all;
    }
}

}

1
2

* `http server config`

server {
listen 8080;

location /hls {
    # Disable cache
    add_header Cache-Control no-cache;

    # CORS setup
    add_header 'Access-Control-Allow-Origin' '*' always;
    add_header 'Access-Control-Expose-Headers' 'Content-Length';

    # allow CORS preflight requests
    if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain charset=UTF-8';
        add_header 'Content-Length' 0;
        return 204;
    }

    types {
        application/vnd.apple.mpegurl m3u8;
        video/mp2t ts;
    }

    root /mnt/;
}

}

1
2

* `nginx.conf` 配置文件模板

worker_processes auto;
events {
worker_connections 1024;
}

RTMP configuration

rtmp {
server {
listen 1935; # Listen on standard RTMP port
chunk_size 4000;

    application show {
        live on;
        # Turn on HLS
        hls on;
        hls_path /mnt/hls/;
        hls_fragment 3;
        hls_playlist_length 60;
        # disable consuming the stream from nginx as rtmp
        deny play all;
    }
}

}

http {
sendfile off;
tcp_nopush on;
aio on;
directio 512;
default_type application/octet-stream;

server {
    listen 8080;

    location / {
        # Disable cache
        add_header 'Cache-Control' 'no-cache';

        # CORS setup
        add_header 'Access-Control-Allow-Origin' '*' always;
        add_header 'Access-Control-Expose-Headers' 'Content-Length';

        # allow CORS preflight requests
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            return 204;
        }

        types {
            application/dash+xml mpd;
            application/vnd.apple.mpegurl m3u8;
            video/mp2t ts;
        }

        root /mnt/;
    }
}

}

1
2

- 重启 `Nginx`

$ /usr/local/nginx/sbin/nginx -t #测试配置文件是否成功
$ /usr/local/nginx/sbin/nginx -t && nginx -s reload #启动并重新加载配置文件
`