在 ubuntu 下安裝 php7.1 、7.2 、5.5.9 等多個版本 nginx

在 ubuntu 下安裝 php7.1 、7.2 、5.5.9 等多個版本

由於 ubuntu 默認的 php 鏡像是 php5.5.9 所以需要添加 php7 鏡像

1. 添加源

$ sudo apt-get update #更新源
$ sudo apt-get install -y language-pack-en-base 
$ local-gen en_US.UTF-8

$ sudo apt-get install software-properties-common
$ sudo LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php
$ sudo apt-get update

2. installl

$ sudo apt-get install php7.1-fpm #安裝 php-fpm
$ sudo apt-get install php7.1-xml #安裝per依賴
# 安裝 php php-pear apachemod php-mysql
$ sudo apt-get install php7.1 php-pear libapache2-mod-php7.1 php7.1-mysql
# 這個地方 php 已經安裝成功

# 我也不知道這個配置什麼意思,後面有機會再深究
# 將cgi.fix_pathinfo=1這一行去掉註釋,將1改爲0
$ sudo vim /etc/php/7.1/fpm/php.ini  
#查看版本
$ php -v
#下面這個錯誤的解決辦法 可能會遇到
#php: symbol lookup error: php: undefined symbol: pcre_jit_exec
$ apt-get install apt-cache policy libpcre3
#The apt show libpcre3 doesn't actually show which libpcre3 package is installed on your system, so yeah apt-cache policy libpcre3 is needed.Upgrade your libpcre3 library to version from the repository.

3. php 版本切換

# 開啓不同版本 apache2.x
$ sudo a2dismod php5.6 #停用 php5.6
$ sudo a2enmod php7.1 #啓用 php7.1
$ sudo service apache2 restart #重啓 apache
$ sudo update-alternatives --set php /usr/bin/php7.1
## 至此已經由 php5.6 切換至 php7.1

4. Install PHP Modoules

# 添加源 ppa 在另外一個地方看到的,不知道跟上面的換源那個是不是一樣的都是 ppa
$ sudo apt-get install python-software-properties
$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt-get update
# 安裝
$ sudo apt-cache search php7-* #搜索 php modouls
$ sudo apt-install install php7-*

4. install composer

# 下載 composer.phar
$ wget https://getcomposer.org/composer.phar
# 或者
$ curl -sS https://getcomposer.org/installer | php
# 添加執行權限
$ sudo chmod +x composer.phar
# 全局安裝並重新命名爲 composer
$ sudo mv composer.phar /usr/local/bin/composer
#可以使用 composer 命令
$ composer
# 可能會缺少相應擴展,缺什麼就裝什麼
$ sudo apt-get install php7.0-zip
$ sudo apt-get install php7.0-mbstring
$ sudo apt-get install php-xml
# 安裝結束

5. Composer Install Laravel

# 配置 composer 國內全量鏡像
$ composer config -g repo.packagist composer https://packagist.phpcomposer.com #php composer
$ composer config -g repo.packagist composer https://packagist.laravel-china.org # laravel china org
# 使用 composer 下載 laravel 安裝包 全局
composer global require "laravel/installer"
$ laravel -v #查看 laravel 版本
$ laravel new demo # 創建一個 laravel 項目

6. Install Nginx

# 安裝 build 所需要的
$ sudo apt-get install build-essential
$ sudo apt-get install libtool
# 安裝 pcre zlib 前者爲了重寫rewrite,後者爲了gzip壓縮
$ cd /usr/local/src
$ sudo wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz
$ tar -zxvf pcre-8.40.tar.gz
cd pcre-8.40
./configure
make
make install
#
cd /usr/local/src
wget http://zlib.net/zlib-1.2.8.tar.gz
tar -zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure
make
make install
# 安裝 nginx
$ cd /usr/local/src
$ sudo wget http://nginx.org/download/nginx-1.12.2.tar.gz
$ sudo tar -zxvf http//nginx.org/download/nginx-1.12.2.tar.gz
$ cd nginx-1.12.2
# 這個地方我沒有配置是直接 ./configure 的,不知道有沒有什麼影響
$ ./configure --sbin-path=/usr/local/nginx/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--with-http_ssl_module \
--with-pcre=/opt/app/openet/oetal1/chenhe/pcre-8.37 \
--with-zlib=/opt/app/openet/oetal1/chenhe/zlib-1.2.8 \
--with-openssl=/opt/app/openet/oetal1/chenhe/openssl-1.0.1t
# --with-pcre=/usr/src/pcre-8.34 指的是pcre-8.34 的源碼路徑。
# --with-zlib=/usr/src/zlib-1.2.7 指的是zlib-1.2.7 的源碼路徑。
$ make
$ make install
#常用命令
#啓動nginx:
$ sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
# 注意:-c 指定配置文件的路徑,不加的話,nginx會自動加載默認路徑的配置文件,可以通過 -h查看幫助命令。
$ sudo /usr/local/nginx/sbin/nginx -t #檢查配置文件
$ sudo /usr/local/nginx/sbin/nginx -s reload #重啓
$ sudo /usr/local/nginx -s stop #停止
#查看nginx進程:
$ ps -ef|grep nginx

7. Nginx php-fpm 安裝配置

1.配置文件位置

/etc/php/7.2/fpm/php.ini
/usr/local/nginx/conf/nginx.conf
  1. 配置 nginx
vim /usr/local/nginx/conf/nginx.conf
#配置文件說明 去掉了頂部上面的幾行配置
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;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
    #root   /var/www/html;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /var/www/web;  #web項目根目錄
            index  index.php index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80

        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
       # }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        # 這裏很重要,php-fpm 必須配置
        location ~ \.php$ {
            #include snippets/fastcgi-php.conf;
        root           /var/www/web; #web項目根目錄
#下面這兩個地方需要對應 php-fpm 的/etc/php/7.2/fpm/pool.d/www.conf 的
#listen=127.0.0.1:9000
#listen=/run/php/php7.2-fpm.sock
            fastcgi_pass   127.0.0.1:9000; # 使用 TCP 鏈接
       # fastcgi_pass   unix:/run/php/php7.2-fpm.sock; #使用套接字鏈接
            fastcgi_index  index.php;
#這個地方的默認必須改,不然會報錯
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
            deny  all;
        }
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
  1. 配置 php-fpm
$ vim /etc/php/7.2fpm/pool.d/www.conf
[...]
;listen = /var/run/php5-fpm.sock
listen = 127.0.0.1:9000
[...]
  1. 重新加載
$ sudo service php7.2-fpm reload
$ sudo /usr/local/nginx/sbin/nginx -s reload
# ok
  1. 注意事項
    • 注意目錄權限,最好將 項目目錄所有者設置爲 nginx 用戶組

7. Nginx 配置 https

8. Apache 配置 https

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章