阿里云Ubuntu14+PHP7+Nginx+Mysql環境搭建

寫在前面的話:
為了將Laravel的項目搬到阿里云上也是花費了不少功夫,記錄下來,留個紀念


  1. 安裝Ubuntu系統。在購買阿里云ECS時候選擇Ubuntu14.04.4 64位版本,安裝完成后可以使用WinSCP+putty工具來登陸ubuntu系統

  2. 安裝nginx。使用putty登陸系統后,先更新源sudo apt-get update,接著安裝nginxsudo apt-get install nginx

  3. 配置nginx。可使用WinSCP圖形化界面,打開/etc/nginx/sites-available/default文件,修改root、index和location /,增加location ~ .php$部分,重啟nginx使配置生效sudo service nginx restart。配置文件可參考以下案例:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    #root為項目代碼所在目錄
    #index在中間添加index.php
    root /var/www/zshanjun/public;
    index index.php index.html index.htm;

    # Make site accessible from http://localhost/
    #填寫購買的阿里云外網IP或者自己已經解析備案的域名
    server_name http://120.24.157.100/;

    #更改未被注釋一行
    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ /index.php?$query_string;
        # Uncomment to enable naxsi on this location
        # include /etc/nginx/naxsi.rules
    }
    #添加下面代碼
        location ~ \.php$ {
               try_files $uri /index.php =404;
               fastcgi_split_path_info ^(.+\.php)(/.+)$;
               fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
               fastcgi_index index.php;
               fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
               include fastcgi_params;
        }
}

4.** 安裝PHP7**

  • 添加PPA。PPA,表示 Personal Package Archives,也就是個人軟件包集。
sudo apt-get install python-software-properties software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
  • 安裝PHP7以及所需的一些擴展
    sudo apt-get install php7.0-fpm php7.0-cli php7.0-mcrypt php7.0-mysql php7.0-mbstring php7.0-xml

5.** 配置PHP7**

  • 打開php.ini配置文件,找到cgi.fix_pathinfo選項,去掉注釋,然后將值設置為0 (vim使用“/”進入查找模式)
    sudo vim /etc/php/7.0/fpm/php.ini
  • 啟用php7.0-mcrypt
    sudo phpenmod mcrypt
  • 重啟php7.0-fpm
    sudo service php7.0-fpm restart

6.** 安裝Mysql**
sudo apt-get install mysql-server-5.6 mysql-client-5.6
途中會提示設置MySQL的密碼,安裝好后:
mysql -u root -p
然后輸入剛剛設置的密碼,能成功進入即成功安裝。

7.** 安裝Laravel項目**

  • 安裝Composer,分別執行以下命令:
sudo apt-get install curl
cd ~
curl -sS https://getcomposer.org/installer| php
sudo mv composer.phar /usr/local/bin/composer
  • 安裝壓縮、解壓縮程序
    sudo apt-get install zip unzip
  • 安裝git
    sudo apt-get install git
    然后在Coding上創建一個私有項目laravel,里面包含所有該Laravel項目所需代碼。
  • 使用git將代碼clone到服務器上
cd /var

mkdir www

cd www

git clone your-project-git-link
  • 修改Laravel項目的訪問權限
sudo chown -R :www-data /var/www/laravel

sudo chmod -R 775 /var/www/laravel/storage
  • 導入Laravel 的vendor目錄
cd /var/www/laravel

composer install

8.訪問網站。在瀏覽器輸入外網IP或者綁定的域名就可訪問網站啦。如果有數據庫,使用mysql -u root -p進入mysql數據庫,創建數據庫,再使用php artisan migrate生成數據表。


其他問題

  1. 如果在阿里云重裝了系統,很可能在更新源的時候就出現問題,即sudo apt-get update時候不能更新源,解決辦法是使用如下命令新建一個更新源文件:
cd /etc/apt/
touch sources.list
sudo vim sources.list

再將下面的代碼粘貼進去即可:

deb http://archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse

deb http://archive.ubuntu.com/ubuntu/ trusty-security main restricted universe multiverse

deb http://archive.ubuntu.com/ubuntu/ trusty-updates main restricted universe multiverse

deb http://archive.ubuntu.com/ubuntu/ trusty-proposed main restricted universe multiverse

deb http://archive.ubuntu.com/ubuntu/ trusty-backports main restricted universe multiverse

deb-src http://archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse

deb-src http://archive.ubuntu.com/ubuntu/ trusty-security main restricted universe multiverse

deb-src http://archive.ubuntu.com/ubuntu/ trusty-updates main restricted universe multiverse

deb-src http://archive.ubuntu.com/ubuntu/ trusty-proposed main restricted universe multiverse

deb-src http://archive.ubuntu.com/ubuntu/ trusty-backports main restricted universe multiverse

deb http://archive.canonical.com/ubuntu/ trusty partner

deb http://extras.ubuntu.com/ubuntu/ trusty main

2.在安裝PHP7和擴展的時候可能因為部分組件有問題而不能安裝的,這時候可以分開安裝,逐個排除問題。


更新 2016-6-27

使用Navicat遠程登陸阿里云Mysql數據庫

  1. 修改sudo vim /etc/mysql/my.cnf文件,將#bind-address = 127.0.0.0一行注釋掉
  2. 更改Mysql授權,并重啟使配置生效。
mysql -u root -p;
use mysql;
grant all privileges on *.* to 'root'@'%' identified by 'your password' with grant option;
flush privileges;
exit;
sudo service mysql restart;
  1. 配置Navicat。在Host Name一欄填寫阿里云外網IP,用戶名為root,密碼為你的密碼,其他可保持不變

參考文章

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容