阿里雲服務器搭建 nginx + php+ laravel 環境

ECS 服務器環境搭建

使用阿里雲的服務器,配置一些基本的環境,來搭建一個網站 主要是使用 nginx + php + mysqlphp

服務器環境: centoscss

1. nginx 安裝

1. 直接使用 yum 安裝

後面一個參數表示指定安裝的位置html

yum install nginx  --prefix=/app/soft/nginx

上面這種安裝,在配置https時,會有問題,提示要安裝ssl模塊啥的,所以能夠這麼添加一下參數mysql

yum install nginx --prefix=/app/soft/nginx --with-http_stub_status_module --with-http_ssl_module

若是你是先執行了上面的步驟,後面發現須要安裝ssl模塊,要怎麼辦 ?nginx

操做以下:laravel

1. 獲取安裝的nginx版本 `nginx -V`
2. 獲取對應的源碼包  `wget http://nginx.org/download/nginx-1.12.0.tar.gz`
3. 解壓源碼包  `tar -zxvf nginx-1.12.0.tar.gz`, 進入解壓的目錄
4. 編譯 `./configure --prefix=/app/soft/nginx --with-http_stub_status_module --with-http_ssl_module`
5. `make`  
6. 備份老的nginx     `cp /app/soft/nginx/sbin/nginx  cp /app/soft/nginx/sbin/nginx.bk`
7. 拷貝新的nginx     `cp sbin/nginx /app/soft/nginx/sbin/nginx`

2. 源碼安裝

上面其實已經包含了源碼安裝的步驟,下面簡單的列一下web

wget http://nginx.org/download/nginx-1.12.0.tar.gz
tar -zxvf nginx-1.12.0.tar.gz; cd nginx-1.12.0
./configure --prefix=/app/soft/nginx --with-http_stub_status_module --with-http_ssl_module
make 
make install

3. 命令

nginx 命令sql

# 啓動
/app/soft/nginx/sbin/nginx  

# 中止
/app/soft/nginx/sbin/nginx -s stop

驗證是否安裝成功,瀏覽器輸入 localhost 便可,若是出現nginx的歡迎頁面,則表示成功bootstrap

2. php 安裝

安裝

安裝 php, 執行下面的命令安裝出來的可能版本是5.+ 的vim

yum install php php-devel php-fpm php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc

由於我安裝的php7, 因此執行的下面的方案

php-7 安裝過程

rpm -ivh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm


rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm


## php 安裝
yum install php70w


## fpm 安裝
yum install php70w-fpm


## 依賴安裝
yum -y install gd-devel zlib-devel libjpeg-devel libpng-devel libiconv-devel freetype-devel libxml2 libxml2-devel openssl openssl-devel curl-devel libxslt-devel libmcrypt-devel mhash mcrypt


## mysql 安裝
yum install php70w-mysql

若是php缺乏一些什麼依賴,能夠經過下面命令

## 列出可安裝的依賴項
yum list | grep php70w 

## 找到對應的依賴配置,執行
yum install php70w-xxx

## 如配置  laravel 的mysql讀取時, 須要配置 pdo,能夠執行一下命令

yum install php70w-pdo_dblib php70w-pdo_mysql php70w-mysql php70w-pdo

php-fpm

啓動: `service php-fpm start`
中止: `service php-fpm stop`

nginx 配置

server {
          listen  80;
          server_name localhost;
          rewrite ^(.*)$  https://localhost$1 permanent;   ## 這個表示將http轉到走https
          set $root_path '/var/www/html';     ## 這裏設置的是存放html代碼的地址
          root $root_path;

          index index.php index.html index.htm;

          try_files $uri $uri/ @rewrite;

          location @rewrite {
              rewrite ^/(.*)$ /index.php?_url=/$1;
          }

          location ~ \.php {
              fastcgi_pass 127.0.0.1:9000;
              fastcgi_index /index.php;

              include fastcgi_params;

              fastcgi_split_path_info       ^(.+\.php)(/.+)$;
              fastcgi_param PATH_INFO       $fastcgi_path_info;
              fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
              fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          }

          location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
              root $root_path;
          }

          location ~ /\.ht {
              deny all;
          }

    }

3. laravel 配置

折騰這個弄了很久,如今加一個過程記錄

  1. 安裝 Composer

    wget http://laravel.com/laravel.phar
    
    mv laravel.phar laravel
    
    sudo mv laravel /usr/local/bin
  2. 安裝 laravel

    // 下面這一步可能要等待好久好久
    composer global require "laravel/installer=~1.1"
    
    
    // 上一步成功以後, 將 `~/.composer/vendor/bin` 路徑放置於您的 PATH 裏, 這樣 laravel 執行文件就會存在你的系統。
  3. 建立一個項目

    // 第三個參數爲項目名
    laravel new blog
  4. 設置權限

    cd blog/
    
    // 注意將 storage 目錄以及其目錄下的全部文件都設置可讀寫, 不然會有問題
    chmod -R 777 storage
    
    chmod -R 777  bootstrap/cache
    
    
    // 打開debug,  設置  APP_DEBUG , true
    vim config/app.php
    // xxx
    
    
    // 生成加密
    php artisan key:generate
  5. 啓動

    composer install
  6. 配置 nginx.conf

    server {
              listen  80;
              server_name locahost;
              set $root_path '/var/www/html/blog/public';
              root $root_path;
    
              index index.php index.html index.htm;
    
              try_files $uri $uri/ @rewrite;
    
              location @rewrite {
                  rewrite ^/(.*)$ /index.php?_url=/$1;
              }
    
              location ~ \.php {
                  fastcgi_pass 127.0.0.1:9000;
                  fastcgi_index /index.php;
    
                  include fastcgi_params;
    
                  fastcgi_split_path_info       ^(.+\.php)(/.+)$;
                  fastcgi_param PATH_INFO       $fastcgi_path_info;
                  fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
                  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
              }
    
              location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
                  root $root_path;
              }
    
              location ~ /\.ht {
                  deny all;
              }
    
        }

##4. https 證書配置

阿里有個免費的我的證書可使用,在使用證書以前請保證本身有一個域名

進入我的控制檯 > 雲頓控制檯概覽 -> 購買證書 -> 選擇免費(收費的也能夠)

購買完成以後,在個人訂單下會多一個記錄,須要補全你的信息,按照提示作便可,等待審覈經過(很快就經過了),點下載去獲取證書

獲取到了證書以後,按照指示照着操做便可,惟一須要注意的是

<font color='red'>走https請開啓443端口</font>

<font color='red'>走https請開啓443端口</font>

<font color='red'>走https請開啓443端口</font>

在這個地方我弄了很久,配好了https就是死活不行,後來查了一些文章,才意識到多是端口號的問題

開啓強制走https

在nginx的配置中加下面一行便可, 上面的nginx配置其實已經包含了

rewrite ^(.*)$  https://localhost$1 permanent;

瀏覽器輸入域名,開始測試....

我的演示以下

相關文章
相關標籤/搜索