一套比较完备的在ubuntu 18.04系统上部署nginx+postgresql+php的记录

Posted blind_mokey

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一套比较完备的在ubuntu 18.04系统上部署nginx+postgresql+php的记录相关的知识,希望对你有一定的参考价值。

 

  1. 更新内核

sudo apt update

  1. 这里提示报错,错误是

E: Unable to locate package update。

对网络进行检测:

Ping www.baidu.com

提示:Unable to found this locate

Ping 14.215.177.37

PING 14.215.177.37 56(84) bytes of data.

64 bytes from 14.215.177.37: icmp_seq=1 ttl=52 time=13.8 ms

64 bytes from 14.215.177.37: icmp_seq=2 ttl=52 time=13.6 ms

64 bytes from 14.215.177.37: icmp_seq=3 ttl=52 time=13.5 ms

64 bytes from 14.215.177.37: icmp_seq=4 ttl=52 time=13.5 ms

--- www.a.shifen.com ping statistics ---

4 packets transmitted, 4 received, 0% packet loss, time 3004ms

rtt min/avg/max/mdev = 13.572/13.646/13.818/0.153 ms

结果访问成功

结论:DNS服务器未能正确查找

提出假设:网卡配置出现故障

进入网卡配置 /etc/network/interfaces

显示结果为空,说明需要对DNS域名进行设置

执行命令:sudo vi /etc/resolvconf/resolv.conf.d/base

显示内容为空,需要添加DNS服务器定位。

在里面插入:
nameserver 8.8.8.8
nameserver 8.8.4.4

保存退出,重启网卡配置

sudo resolvconf -u

接着再测试DNS寻址功能

Ping www.baidu.com

结果返回如下:

server@host-172-22-8-11:~$ ping www.baidu.com

PING www.a.shifen.com (14.215.177.38) 56(84) bytes of data.

64 bytes from 14.215.177.38: icmp_seq=1 ttl=52 time=13.2 ms

64 bytes from 14.215.177.38: icmp_seq=2 ttl=52 time=16.2 ms

64 bytes from 14.215.177.38: icmp_seq=3 ttl=52 time=16.1 ms

64 bytes from 14.215.177.38: icmp_seq=4 ttl=52 time=16.2 ms

^C

--- www.a.shifen.com ping statistics ---

4 packets transmitted, 4 received, 0% packet loss, time 11291ms

rtt min/avg/max/mdev = 13.254/15.459/16.214/1.276 ms

 

证明DNS已经配置成功,可以进行下一步操作。

Sudo apt update

 

    2.安装nginx,进行web配置

首先安装环境依赖包。

gcc、g++依赖库

Sudo apt-get install build-essential

Sudo apt-get install libtool

 

安装 pcre依赖库(http://www.pcre.org/

sudo apt-get install libpcre3 libpcre3-dev

 

安装 zlib依赖库(http://www.zlib.net

sudo apt-get install zlib1g-dev

 

安装ssl依赖库(ubuntu16.04 LTS 已经安装过了)

sudo apt-get install openssl

    sudo apt-get install libssl-dev

 

 

然后下载Nginx的压缩包(zip)

sudo wget http://nginx.org/download/nginx-1.11.11.tar.gz

进行解压缩

sudo tar -zxvf nginx-1.11.11.tar.gz.

 

添加nginx支持的模块:

然后对Nginx进行编译安装,所以在进行配置的时候进行添加(其中后缀名自己根据版本号来看,不要照着这里教程来添加)

sudo ./configure --user=daemon --group=daemon --prefix=/usr/local/nginx/ --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_gzip_static_module

make

make install

编译nginx,进行配置完成之后,执行如下操作

sudo wget https://raw.github.com/JasonGiedymin/nginx-init-ubuntu/master/nginx -O /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx
sudo update-rc.d nginx defaults
sudo service nginx start

 

 

/usr/local/nginx/sbin/nginx -V  查看安装的模块是否正确

 

nginx安装成功。

 

  1. php7环境安装

请到php.net下载7.0+版本的php安装包,并进行解压缩

sudo tar -zxf php-7.0

Cd php-7.0

进行如下指令编译安装:

sudo ./configure  --prefix=/usr/local/php7 \\

    --with-config-file-scan-dir=/usr/local/php7/etc/php.d \\

    --with-config-file-path=/usr/local/php7/etc \\

    --enable-mbstring \\

    --enable-zip \\

    --enable-bcmath \\

    --enable-pcntl \\

    --enable-ftp \\

    --enable-xml \\

    --enable-shmop \\

    --enable-soap \\

    --enable-intl \\

    --with-openssl \\

    --enable-exif \\

    --enable-calendar \\

    --enable-sysvmsg \\

    --enable-sysvsem \\

    --enable-sysvshm \\

    --enable-opcache \\

    --enable-fpm  \\

    --enable-session \\

    --enable-sockets \\

    --enable-mbregex \\

    --enable-wddx \\

    --with-curl \\

    --with-mcrypt \\

    --with-iconv \\

    --with-gd \\

    --with-jpeg-dir=/usr \\

    --with-png-dir=/usr \\

    --with-zlib-dir=/usr \\

    --with-freetype-dir=/usr \\

    --enable-gd-native-ttf \\

    --enable-gd-jis-conv \\

    --with-openssl \\

    --with-pdo-mysql=mysqlnd \\

    --with-gettext=/usr \\

    --with-zlib=/usr \\

    --with-bz2=/usr \\

    --with-recode=/usr \\

    --with-xmlrpc \\

    --with-pdo-pgsql \\

    --enable-memcache\\

 

如果提示缺少openssl

Sudo apt install openssl

如果提示缺少curl

Sudo apt install curl

Sudo apt install libcurl4-gnutls-dev

如果提示缺少jpeglib.h not found.

Sudo apt install libjpeg-dev

提示缺少png.h

Sudo apt install libpng12-dev

提示缺少freetype

apt-get install libfreetype6-dev

提示缺少libpq.h,缺少postgresql支持

sudo apt install libpq-dev

提示缺少ICU

sudo apt install libicu-dev

提示缺少libmcrypt

sudo apt install libmcrypt-dev

提示缺少recode.h

sudo apt install librecode-dev

提示缺少Bzip2

sudo apt install libbz2-dev

 

 

接着执行:make && make install

并对php的配置文件进行修改

cd /usr/local/php/etc

 

cp php-fpm.conf.default php-fpm.conf

 

cd /usr/local/php/etc/php-fpm.d

 

cp www.conf.default www.conf

 

如果提示没有用户或用户组在内,进行如下指令

Sudo groupadd server

Sudo useradd -g server server

 

如果还报错,显示nobody在用户组

Sudo Useradd  -g nobody server

 

对nginx.conf进行设置,让它支持php7

Sudo vim /usr/local/nginx/conf/nginx.conf

在server内添加:

 

rewrite ^/(.*.php)(/)(.*)$ /$1?file=/$3 last;

并在这个大括号之后添加如下内容支持.php

location ~ \\.php$

            root           html;

            fastcgi_pass   127.0.0.1:9000;

            fastcgi_index  index.php;

            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

            include        fastcgi_params;

        

 

然后启动php-fpm

Sudo /usr/local/php7/sbin/php-fpm

重启Nginx支持php7

Sudo service nginx restart

 

  1. 进行Php环境测试

Sudo touch /usr/local/nginx/html/index.php

 

Sudo vim /usr/local/nginx/html/index.php

 

添加如下内容并保存:

<?php

phpinfo();

?>

然后对服务器web根目录进行访问

http://IP地址/index.php

显示如下内容

 

环境安装成功!

 

  1. 对第二台服务器进行数据库环境安装(PostgreSQL)

利用xshell登录第二台服务器

 

(说明:因为这个服务器版本比较老,而且没有添加DNS服务器,所以需要按照步骤1的做法再做一次。)

安装好域名服务器之后,安装Postgresql。

这里安装的是Postgresql9.4,服务器已经自带了9.3,需要进行卸载。

Sudo apt remove postgresql*

删除完所有的原带安装包,进行Postgresql源选择。

sudo touch /etc/apt/sources.list.d/pgdb.list

sudo vim /etc/apt/sources.list.d/pgdb.list

在文件中添加如下语句:

          deb https://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main

执行如下指令:

sudo wget --quiet -O - https://postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

添加源完毕,进行安装:

sudo apt-get update

sudo apt-get install postgresql-9.4

 

然后进入postgres用户

Sudo -i -u postgre

运行postgresql

Psql

这里会报错:

 Please check that your locale settings: LANGUAGE = "zh_CN:zh", LC_ALL = (unset), LC_PAPER = "zh_CN", LC_ADDRESS = "zh_CN", LC_MONETARY = "zh_CN", LC_NUMERIC = "zh_CN", LC_TELEPHONE = "zh_CN", LC_IDENTIFICATION = "zh_CN", LC_MEASUREMENT = "zh_CN"

意思是不支持中文字体,需要进行中文字体安装

sudo apt-get -y install language-pack-zh-hans language-pack-zh-hans-base

编译完成之后,支持中文字体。

        输入

         Psql

        如果出现报错

    psql: 无法联接到服务器: 没有那个文件或目录

服务器是否在本地运行并且在 Unix 域套接字

"/var/run/postgresql/.s.PGSQL.5432"上准备接受联接?

       Sudo service postgresql start

       如果出现错误提示:

       No PostgreSQL clusters exist; see “man pg_createcluster”

执行如下语句:

sudo pg_createcluster 9.4 main --start

再执行:

Sudo service postgresql start

Sudo -i -u postgres

Psql

进入postgresql数据库

创建自己的数据库:

Createdb zhdj

创建用户

Create user bgxf

完成数据库服务器配置!

如果连接的时候出现端口拒绝,先检查端口是否开放,如果未开放,需要开放5432端口

如果提示password authentication for user,需要修改/etc/postgresql/9.x/main/postgresql.conf里面的Listen_address,修改为  ’*’,并且修改/etc/postgresql/9.x/main/pg_hba.conf里面,添加

host    all             all             0.0.0.0/0               trust

就可以访问了

 

  1. 对第三台服务器进行mpeg-dash流媒体服务器设置

开始步骤同上,添加DNS域名解析地址,否则无法进行apt install操作

 

首先安装环境依赖包。

gcc、g++依赖库

Sudo apt-get install build-essential

Sudo apt-get install libtool

 

安装 pcre依赖库(http://www.pcre.org/

sudo apt-get install libpcre3 libpcre3-dev

 

安装 zlib依赖库(http://www.zlib.net

sudo apt-get install zlib1g-dev

 

安装ssl依赖库

sudo apt-get install openssl

sudo apt-get install libssl-dev

 

添加yasm编码源

sudo apt install yasm

 

添加mplayer

sudo apt install mplayer

 

添加x264源

sudo apt install x264

 

安装libvpx3

sudo apt install libvpx3 (我这里是选择libvpx*安装,因为会缺少一些东西,所以选择全部安装)

 

安装pkgconfig

sudo apt install pkg-config

   安装ffmpeg

由于ubuntu版本老旧的原因需要添加ffmpeg镜像源,指令如下

sudo apt-add-repository ppa:mc3man/trusty-media

sudo apt-get update

sudo apt install ffmpeg

 

然后下载Nginx的压缩包(zip)

sudo wget http://nginx.org/download/nginx-1.11.11.tar.gz

进行解压缩

sudo tar -zxvf nginx-1.11.11.tar.gz.

 

然后对Nginx进行编译安装,因为这里我们需要用到hls和ffmpeg模块,所以在进行配置的时候进行添加(其中后缀名自己根据版本号来看,不要照着这里教程来添加)

首先添加模块文档:

让服务器支持 nginx-h264

 

wget http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz

tar zxvf nginx_mod_h264_streaming-2.2.7.tar.gz

 

第二个是nginx-rtmp-module,让nginx支持rtmp/hls协议

 

wget -O nginx-rtmp-module.zip  https://github.com/arut/nginx-rtmp-module/archive/master.zip

unzip nginx-rtmp-module.zip

下载清缓存的模块

wget -O ngx_cache_purge.zip https://github.com/FRiCKLE/ngx_cache_purge/archive/master.zip

unzip ngx_cache_purge.zip

 

./configure --user=daemon --group=daemon --prefix=/usr/local/nginx/ --add-module=../nginx-rtmp-module-master  --add-module=../nginx_mod_h264_streaming-2.2.7 --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_gzip_static_module --with-http_flv_module

(1)问题内容:

checking for C compiler ... found but is not working

./configure error : C compiler gcc is not found

(2)原因分析:

configure首先会编译一个小测试程序,通过测试其运行结果来判断编译器是否能正常工作,由于交叉编译器所编译出的程序是无法在编译主机上运行的,故而产生此错误。

(3)解决办法:

编辑auto/cc/name文件,将21行的“exit 1”注释掉(令测试程序不会报错)。

编译过程中出错:

make[1]: *** [objs/addon/src/mp4_reader.o] 错误 1

解决方法:

vim objs/Makefile (修改objs/Makefile文件, 去掉其中的"-Werror"),

 

出错:

make[1]: *** [objs/addon/src/ngx_http_h264_streaming_module.o] Error 1

make[1]: Leaving directory '/home/dash/nginx-1.11.11'

Makefile:8: recipe for target 'build' failed

 

解决办法:Vim nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c  将如下几行注释

 

/* TODO: Win32 */

 

if (r->zero_in_uri)

return NGX_DECLINED;

然后就能够正常编译了.

sudo make install
设置nginx为系统服务
sudo wget https://raw.github.com/JasonGiedymin/nginx-init-ubuntu/master/nginx -O /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx
sudo update-rc.d nginx defaults
sudo service nginx start

 

/usr/local/nginx/sbin/nginx -V  查看安装的模块是否正确

 

sudo cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bk

建立存放视频目录

 

sudo mkdir -p /mnt/media/vod

sudo mkdir /mnt/media/app

sudo mkdir /mnt/media/vedio

 

sudo vi /usr/local/nginx/conf/nginx.conf   (将文件替换为以下内容,更改前记得备份)

 

#filename:nginx.conf

 

#user  nobody;

 

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

 

use epoll;

 

worker_connections  1024;

 

 

 

rtmp

 

server

 

listen 1935;

 

chunk_size 4000;

 

 

# video on demand

 

application vod

 

play /mnt/media/vod;

 

 

 

# HLS

 

# HLS requires libavformat & should be configured as a separate

 

# NGINX module in addition to nginx-rtmp-module:

 

# ./configure … –add-module=/path/to/nginx-rtmp-module/hls …

 

# For HLS to work please create a directory in tmpfs (/tmp/app here)

 

# for the fragments. The directory contents is served via HTTP (see

 

# http section in config)

 

#

 

# Incoming stream must be in H264/AAC/MP3. For iPhones use baseline H264

 

# profile (see ffmpeg example).

 

# This example creates RTMP stream from movie ready for HLS:

 

#

 

# ffmpeg -loglevel verbose -re -i movie.avi  -vcodec libx264

 

#-vprofile baseline -acodec libmp3lame -ar 44100 -ac 1

 

#-f flv rtmp://localhost:1935/hls/movie

 

#

 

# If you need to transcode live stream use ‘exec’ feature.

 

#

 

application dash

 

dash on;

 

dash_path /mnt/media/app;

 

dash_fragment 10s;

 

dash_cleanup off;

 

dash_nested on;

 

 

 

 

http

 

include mime.types;

 

default_type application/octet-stream;

 

sendfile on;

 

keepalive_timeout 65;

 

gzip on;

 

add_header Access-Control-Allow-Origin *;

add_header Access-Control-Allow-Headers X-Requested-With;

add_header Access-Control-Allow-Methods GET,POST,OPTIONS;

 

 #log format

 

log_format  access  ‘$remote_addr – $remote_user [$time_local] “$request” ‘

 

 ‘$status $body_bytes_sent “$http_referer” ‘

 

 ‘”$http_user_agent” $http_x_forwarded_for’;

 

 #定义一个名为addr的limit_zone,大小10M内存来存储session

 

limit_conn_zone $binary_remote_addr zone=addr:10m;

 

server

 

listen 8080;

 

 server_name localhost;

 

 # HTTP can be used for accessing RTMP stats

 

# This URL provides RTMP statistics in XML

 

location /stat

 

rtmp_stat all;

 

rtmp_stat_stylesheet stat.xsl;

 

 

location /stat.xsl

 

root /mnt/soft/nginx-rtmp-module-master;

 

 

 location /control

 

rtmp_control all;

 

 

location /

 

root /mnt/soft/nginx-rtmp-module-master/test/rtmp-publisher;

 

 

 

 server

 

listen 80;

 

 server_name localhost;

 

 location /

 

root /mnt/wwwroot;

 

index index.html;

 

  

 

 location ~ \\.flv$

 

   root /mnt/media/vod;

 

 flv;

 limit_conn addr 20;

 limit_rate 200k;

 

location ~ \\.mp4$

 

 root /mnt/media/vod;

 

 mp4;

 limit_conn addr 20;

 limit_rate 200k;

 location /dash

# Serve HLS fragments

alias  /mnt/media/app;

 add_header Cache-Control no-cache;

 add_header Access-Control-Allow-Origin *;

 access_log  logs/nginxflv_access.log access;

 

 

测试

 

上传MP4文件到/mnt/media/video/

这里是对源文件进行分辨率调整,如果分辨率太大不适宜切片和播放,转码需要消耗的CPU较高,需要排队等候

 ffmpeg -i test.mp4  -s 720*480-strict -2 testsrc.mp4

 

 

切片

原指令:

ffmpeg -i dashtest.mp4 -c copy -f dash -window_size 0 -min_seg_duration 3000000 -single_file 0 -init_seg_name dashtest\\$.m4s -media_seg_name dashtest\\$-\\$Number%05d\\$.m4s -use_template 0 -bsf:a aac_adtstoasc  manifest.mpd

 

 

改进:

ffmpeg -i test.mp4 -c copy -f dash -window_size 0 -min_seg_duration 60000000 -single_file 0 -init_seg_name initavatar-stream\\$RepresentationID\\$.m4s -media_seg_name shwjtest\\$RepresentationID\\$-\\$Number%05d\\$.m4s -use_template 0 -bsf:a aac_adtstoasc  test.mpd

1)window_size: number of segments kept in the manifest,默认值:0

 

2)extra_window_size:number of segments kept outside of the manifest before removing from disk,默认值:5

 

3)min_seg_duration:minimum segment duration (in microseconds),默认值:5000000,即5秒

 

4)remove_at_exit:remove all segments when finished,默认值:0

 

5)use_template:Use SegmentTemplate instead of SegmentList,默认值:1

 

6)use_timeline:Use SegmentTemplate instead of SegmentList,默认值:1

 

7)single_file:Store all segments in one file, accessed using byte ranges,默认值:0

 

8)single_file_name:DASH-templated name to be used for baseURL. Implies storing all segments in one file, accessed using byte ranges,默认值:NULL

 

9)init_seg_name:DASH-templated name to used for the initialization segment,默认值:"init-stream$RepresentationID$.m4s"

 

10)media_seg_name:DASH-templated name to used for the media segments,默认值:"chunk-stream$RepresentationID$-$Number%05d$.m4s"

 

这里因为ubuntu的原因抛弃了原先的segmenter方案,改用ffmpeg进行切片,然后要将原本清除缓存的模块进行删除,才能保证直播+点播,并且要有相应的文件支持

如果在WEB端上传视频进行转码,需要设置一个脚本,对传入的视频进行转码然后进行切片,后续内容等Python上手了进行设计构造

 

要将切片后的.mpd和.m4s文件移动到/mnt/media/app目录里面,然后可以进行一下步骤进行播放测试:

http://dashif.org/reference/players/javascript/v1.5.1/samples/dash-if-reference-player/index.html

Load url: http://localhost/dash/manifest.mpd

 

 

2017/4/24添加内容:

修改nginx限制文件上传大小:

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;

在http总配置文件下面添加如下内容:

    client_max_body_size    3072m;

变成:

http

    include       mime.types;

    default_type  application/octet-stream;

    client_max_body_size    3072m;

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

重启Nginx :sudo service nginx restart

修改php.ini

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

 修改如下内容:

max_execution_time

修改成:max_execution_time = 0

max_execution_time

修改成:

max_execution_time=3072m

upload_max_filesize

修改成:

upload_max_filesize = 2048M

 

2017/4/25添加内容

修改Php内存限制大小,在php.ini尾部添加如下内容:

Memory_limit = 2048M

 

(2017/4/28添加内容)

2017/4/25添加的php.ini配置并未生效,打开phpinfo.php进行查看,发现如下错误:

Configuration File (php.ini) Path

/usr/local/php7/etc

Loaded Configuration File

None

 

说明php.ini文件的修改并未剩下,进行如下配置

sudo killall php-fpm

#关闭当前所有php-fpm进程,用于调试

sudo cp /usr/local/php7/etc/php.ini /home/server/

#备份当前配置文件

sudo /usr/local/php7/sbin/php-fpm -y /usr/local/php7/etc/php-fpm.conf  -c  /usr/local/php7/etc/php.ini

#重定向php-fpm的配置文件引用,分别调用/php-fpm.conf和php.ini

重启nginx 服务

sudo service nginx restart

再打开phpinfo.php查看配置

Configuration File (php.ini) Path

/usr/local/php7/etc

Loaded Configuration File

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

 

 

memory_limit

2048MB

2048MB

.

 

post_max_size

3072M

3072M

 

说明配置成功!

以上是关于一套比较完备的在ubuntu 18.04系统上部署nginx+postgresql+php的记录的主要内容,如果未能解决你的问题,请参考以下文章

Ubuntu 18.04部署ONOS+BMv2教程

Ubuntu 18.04部署ONOS+BMv2教程

Spire.Cloud 私有化部署教程- Ubuntu 18.04 系统

安装部署Ubuntu Server操作系统

ubuntu18.04下Kafka安装与部署

Ubuntu 18.04开发环境部署流程