问题

软件 >> Xubuntu
Questions in category: Xubuntu (Xubuntu).

在Xubuntu 16.04 LTS 中安装 Nginx+PHP7+PostgreSQL

Posted by haifeng on 2017-10-08 16:39:35 last update 2017-10-08 22:02:28 | Answers (0) | 收藏


Step 1. 安装Nginx

sudo apt-get install nginx

这将会同时安装下列软件:
  nginx-common nginx-core

建议安装:
  fcgiwrap nginx-doc

可以使用whereis命令查看nginx安装的位置

查看nginx的安装位置
haifeng@X220i:/etc/nginx$ whereis nginx
nginx: /usr/sbin/nginx /etc/nginx /usr/share/nginx

 

cd /etc/nginx/sites-available

sudo cp default default-original
sudo vim default

将其中的root目录改为自己设定的目录,比如

    #root /var/www/html;
    root /home/haifeng/public_html/htdocs;

 

启动 nginx

sudo /etc/init.d/nginx start

打开浏览器, 输入网址 http://localhost/  将会看到 nginx 欢迎的页面。

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

 

在安装好下面的PHP7后,继续修改 default 文件中的PHP部分,如下:

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
    
        # With php7.0-cgi alone:
        #fastcgi_pass 127.0.0.1:9000;
        # With php7.0-fpm:
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }


 

Step 2. 安装PHP7

haifeng@X220i:~$ sudo apt-get install php7.0 php7.0-fpm php7.0-mysql
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
将会同时安装下列软件:
  php-common php7.0-cli php7.0-common php7.0-json php7.0-opcache
  php7.0-readline
建议安装:
  php-pear
下列【新】软件包将被安装:
  php-common php7.0 php7.0-cli php7.0-common php7.0-fpm php7.0-json
  php7.0-mysql php7.0-opcache php7.0-readline
升级了 0 个软件包,新安装了 9 个软件包,要卸载 0 个软件包,有 133 个软件包未被升级。
需要下载 3,666 kB 的归档。
解压缩后会消耗 14.6 MB 的额外空间。
您希望继续执行吗? [Y/n] y

建议安装
php7.0-curl  php7.0-mcrypt php7.0-mbstring php7.0-xml

sudo apt-get install php7.0-curl php7.0-mcrypt php7.0-mbstring php7.0-xml

将会同时安装下列软件:
  libmcrypt4
建议安装:
  libmcrypt-dev mcrypt

最后将建议安装的软件包也安装上

sudo apt-get install php-pear libmcrypt-dev mcrypt

haifeng@X220i:~$ sudo apt-get install fcgiwrap nginx-doc
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
将会同时安装下列软件:
  libfcgi0ldbl spawn-fcgi
下列【新】软件包将被安装:
  fcgiwrap libfcgi0ldbl nginx-doc spawn-fcgi
升级了 0 个软件包,新安装了 4 个软件包,要卸载 0 个软件包,有 133 个软件包未被升级。
需要下载 210 kB 的归档。
解压缩后会消耗 728 kB 的额外空间。
您希望继续执行吗? [Y/n] y


 

Step 3. 安装PostgreSQL

haifeng@X220i:~$ sudo apt-get install postgresql
[sudo] haifeng 的密码:
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
将会同时安装下列软件:
  libpq5 postgresql-9.5 postgresql-client-9.5 postgresql-client-common
  postgresql-common postgresql-contrib-9.5 sysstat
建议安装:
  postgresql-doc locales-all postgresql-doc-9.5 libdbd-pg-perl isag
下列【新】软件包将被安装:
  libpq5 postgresql postgresql-9.5 postgresql-client-9.5
  postgresql-client-common postgresql-common postgresql-contrib-9.5 sysstat
升级了 0 个软件包,新安装了 8 个软件包,要卸载 0 个软件包,有 133 个软件包未被升级。
需要下载 4,801 kB 的归档。
解压缩后会消耗 19.4 MB 的额外空间。
您希望继续执行吗? [Y/n]

将建议的安装包也装上

sudo apt-get install postgresql-doc locales-all postgresql-doc-9.5 libdbd-pg-perl isag

haifeng@X220i:~$ sudo apt-get install postgresql-doc locales-all postgresql-doc-9.5 libdbd-pg-perl isag
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
将会同时安装下列软件:
  aglfn gnuplot-data gnuplot-tex gnuplot-x11 libdbi-perl liblua5.1-0
建议安装:
  gnuplot-doc rcs libmldbm-perl libnet-daemon-perl libsql-statement-perl
下列【新】软件包将被安装:
  aglfn gnuplot-data gnuplot-tex gnuplot-x11 isag libdbd-pg-perl libdbi-perl
  liblua5.1-0 locales-all postgresql-doc postgresql-doc-9.5
升级了 0 个软件包,新安装了 11 个软件包,要卸载 0 个软件包,有 133 个软件包未被升级。
需要下载 7,280 kB 的归档。
解压缩后会消耗 149 MB 的额外空间。
您希望继续执行吗? [Y/n] y

 

 

这里还无法安装pgadmin4, 只能安装pgadmin3

haifeng@X220i:~$ sudo apt-get install pgadmin3
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
将会同时安装下列软件:
  libwxbase3.0-0v5 libwxgtk3.0-0v5 pgadmin3-data pgagent
建议安装:
  postgresql-contrib
下列【新】软件包将被安装:
  libwxbase3.0-0v5 libwxgtk3.0-0v5 pgadmin3 pgadmin3-data pgagent
升级了 0 个软件包,新安装了 5 个软件包,要卸载 0 个软件包,有 133 个软件包未被升级。
需要下载 10.3 MB 的归档。
解压缩后会消耗 42.1 MB 的额外空间。
您希望继续执行吗? [Y/n]

 

sudo apt-get install postgresql-contrib

 

使用 which psql 和 which pgadmin3 检查一下

haifeng@X220i:~$ which pgadmin3
/usr/bin/pgadmin3

haifeng@X220i:~$ which psql
/usr/bin/psql

haifeng@X220i:~$ whereis postgresql
postgresql: /usr/lib/postgresql /etc/postgresql /usr/share/postgresql

 

启动PostgreSQL

haifeng@X220i:~$ sudo /etc/init.d/postgresql start
[ ok ] Starting postgresql (via systemctl): postgresql.service.

如果需要关闭,则执行 stop

haifeng@X220i:~$ sudo /etc/init.d/postgresql stop
[ ok ] Stopping postgresql (via systemctl): postgresql.service.

我们再次开启PostgreSQL, 然后执行下面的语句

haifeng@X220i:~$ sudo su postgres -c psql template1
psql (9.5.9)
Type "help" for help.

postgres=#

更改postgres的密码

postgres=# ALTER USER postgres WITH PASSWORD 'newpasswd';
ALTER ROLE

更新系统中用户postgres的密码

haifeng@X220i:~$ sudo passwd postgres
[sudo] haifeng 的密码:
输入新的 UNIX 密码:
重新输入新的 UNIX 密码:
passwd:已成功更新密码

 

切换到 postgres 用户

haifeng@X220i:~$ su postgres
密码:
postgres@X220i:/home/haifeng$

使用createdb命令建立一个数据库, 名称为mydb

postgres@X220i:/home/haifeng$ which createdb
/usr/bin/createdb
postgres@X220i:/home/haifeng$ createdb mydb

然后就可以连接到此数据库

postgres@X220i:/home/haifeng$ psql -U postgres -d mydb
psql (9.5.9)
Type "help" for help.

mydb=#

 

 

具体参考

http://wiki.ubuntu.com.cn/PostgreSQL