CentOS7.0下搭建nginx+php7+postgresql+laravel的運(yùn)行環(huán)境

安裝步驟大體分為以下幾步:

  1. 基本環(huán)境配置
  2. 安裝php-fpm(同時(shí)安裝了php)
  3. 安裝composer
  4. 安裝postgresql并配置
  5. 安裝nginx
  6. 創(chuàng)建一個(gè)laravel應(yīng)用并配置nginx

CentOs基本環(huán)境配置

基本

  1. 添加新用戶
    adduser [user]
    passwd [user]
    gpasswd -a [user] wheel 加入有root的用戶組
    useradd -G {group-name} username

  2. mac配置免密碼連接服務(wù)器
    安裝小工具 brew install ssh-copy-id
    配置公鑰到服務(wù)器 ssh-copy-id [user]@[ip]

  3. 禁止root用戶遠(yuǎn)程登錄
    sudo vi /etc/ssh/sshd_config
    vim 搜索: /PermitRoot 改為no
    重新加載ssh:sudo systemctl reload sshd

  4. 禁止ssh使用密碼登錄
    sudo vi /etc/ssh/sshd_config
    搜索 PasswordAuthent 改為no
    重新加載

  5. 修改ssh端口
    搜索配置文件 port 修改 重新加載

  6. 防火墻常用

  • firewall-cmd --add-port=12222/tcp --permanent
  • firewall-cmd --reload
  1. 更新系統(tǒng)和軟件
  • yum -y install yum-cron
  • 修改/etc/yum/yum-cron.conf,apply_updates = yes.
  • systemctl start crond
  • systemctl start yum-cron

yum和源

在CentOS上管理軟件可以使用yum作為包管理工具,可以用命令查詢、刪除、下載、安裝、刪除系統(tǒng)上的軟件。

yum的使用:yum參考手冊

系統(tǒng)官方的資源庫提供的軟件不能滿足我們的需求,比如搜索php70u,會(huì)提示NO Matches found,所以我們要添加額外的資源庫。

yum install epel-release

在這里可以找到ius的包,ius

復(fù)制包地址,使用wget下載,會(huì)下載到當(dāng)前目錄。

wget https://dl.iuscommunity.org/pub/ius/stable/CentOS/7/x86_64/ius-release-1.0-14.ius.centos7.noarch.rpm

然后使用命令安裝:

rpm -ivh ius-release-1.0-10.ius.el6.noarch.rpm epel-release-6-5.noarch.rpm

安裝php-fpm和php,安裝php常用擴(kuò)展

搜索php-fpm:

yum search php70u-fpm

安裝,因?yàn)閥um會(huì)自動(dòng)解決軟件之間的依賴,所以會(huì)自動(dòng)安裝php

yum install php70u-fpm

查看php-fpm的狀態(tài):

systemctl status php-fpm

啟動(dòng)php——fpm:

systemctl start php-fpm

設(shè)置開機(jī)啟動(dòng):

systemctl enable php-fpm

安裝php常用的擴(kuò)展:

yum install php70u-gd php70u-mysqlnd php70u-pdo php70u-mcrypt php70u-mbstring php70u-json php70u-cli -y

安裝完成需要重新加載php-fpm:

systemctl reload php-fpm

修改權(quán)限:/etc/php-fpm.d/www.conf
我是把用戶修改成了nginx,group是www.

安裝composer

composer是php中管理依賴的工具官網(wǎng)

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '92102166af5abdb03f49ce52a40591073a7b859a86e8ff13338cf7db58a19f7844fbc0bb79b2773bf30791e935dbd938') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

這幾條命令的意思就是:下載;校驗(yàn);安裝;刪除安裝包;

在國內(nèi)使用composer如果沒有翻墻的話,可以使用鏡像。鏡像地址

安裝postgresql并配置

直接使用yum安裝地方postgres不是最新的,一些新特性無法使用,建議按官網(wǎng)教程安裝最新版。

yum install http://yum.postgresql.org/9.5/redhat/rhel-7-x86_64/pgdg-redhat95-9.5-2.noarch.rpm

完成之后:

yum install postgresql95-server postgresql95-contrib
/usr/pgsql-9.5/bin/postgresql95-setup initdb
systemctl enable postgresql-9.5.service
    systemctl start postgresql-9.5.service
yum install php70u-pgsql

查看是否安裝成功:

rpm -aq|grep postgres

配置:
postgres安裝完成之后會(huì)生成名為postgres的數(shù)據(jù)庫、數(shù)據(jù)庫用戶和linux系統(tǒng)用戶

  • 切換到postgres用戶:

      sudo su - postgres
    
  • 使用psql命令登錄PostgreSQL:

      psql
    

這時(shí)相當(dāng)于系統(tǒng)用戶postgres以同名數(shù)據(jù)庫用戶的身份,登錄數(shù)據(jù)庫,這是不用輸入密碼的。如果一切正常,系統(tǒng)提示符會(huì)變?yōu)?postgres=#",表示這時(shí)已經(jīng)進(jìn)入了數(shù)據(jù)庫控制臺(tái)。以下的命令都在控制臺(tái)內(nèi)完成。

  • 第一件事是使用\password命令,為postgres用戶設(shè)置一個(gè)密碼。

    \password postgres
    
  • 創(chuàng)建數(shù)據(jù)庫用戶dbuser
    create user dbuser with password 'pass';

  • 在shell下創(chuàng)建超級(jí)用戶:
    sudo -u postgres createuser --superuser dbuser
    之后登錄數(shù)據(jù)庫控制臺(tái),設(shè)置dbuser用戶的密碼,完成后退出控制臺(tái)

sudo -u postgres psql
\password dbuser
\q

(postgres安裝完成之后會(huì)生成名為postgres的數(shù)據(jù)庫、數(shù)據(jù)庫用戶和linux系統(tǒng)用戶)psql命令是登錄PostgreSQL控制臺(tái)

  • 創(chuàng)建數(shù)據(jù)庫exampledb,并指定所有者為dbuser

    create database gatewayone owner dbuser;
    or shell創(chuàng)建
    sudo -u postgres createdb -O dbuser exampledb
    

我是使用Navicat Premium和Postico作為數(shù)據(jù)庫管理軟件的。Postico更輕,查看修改數(shù)據(jù)比較方便,Navicat功能比較強(qiáng)大。

數(shù)據(jù)庫直接安裝完成之后是不能遠(yuǎn)程訪問的,如果需要遠(yuǎn)程訪問的話,需要配置一下:

修改postgresql.conf和pg_hba.conf這兩個(gè)文件。

可以使用locate命令定位這兩個(gè)文件,updatedb命令可以更新locate文件,如果剛安裝完沒有找到這兩個(gè)文件,可以調(diào)用這個(gè)命令刷新一下。
需要切換到postgres用戶

  • postgresql.conf配置

      listen_addresses = '*'     //監(jiān)聽所有ip的連接,默認(rèn)是本機(jī)  
      port = 5432             //這個(gè)不開也行,默認(rèn)就是5432端口
    
  • pg_hba.conf配置

      配置host    all         all         0.0.0.0/0             md5
    
  • 如果配置防火墻,記得開放端口

修改完記得重啟。

安裝nginx并配置

nginx官網(wǎng)安裝教程

想安裝最新nginx,我們需要先添加nginx的庫。

創(chuàng)建文件/etc/yum.repos.d/nginx.repo,并寫入以下內(nèi)容:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/OSRELEASE/7/
gpgcheck=0
enabled=1   

或者使用rpm安裝:

rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

然后使用yum命令安裝nginx即可。

創(chuàng)建一個(gè)laravel應(yīng)用并配置nginx

laravel官方文檔

  • 先通過composer下載laravelcomposer global require "laravel/installer"
  • 創(chuàng)建一個(gè)laravel項(xiàng)目composer create-project --prefer-dist laravel/laravel blog

簡單的nginx配置使其可以訪問laravel應(yīng)用:
配置文件在/etc/nginx/conf.d目錄下。一個(gè)基本的配置如下:

server {
   listen 81;
   server_name www.laravel.com;     
   root /data1/www/laravelapps/app/blog/public;
   index index.php index.html index.htm;

   location / {
        try_files $uri $uri/ /index.php?$query_string;
   }

   location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
      fastcgi_param  SCRIPT_FILENAME  $document_root        $fastcgi_script_name;
      include        fastcgi_params;
      fastcgi_param   SCRIPT_NAME  $fastcgi_script_name;     
   }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容