阿里云Ubuntu14+PHP7+Nginx+Mysql環(huán)境搭建

寫在前面的話:
為了將Laravel的項(xiàng)目搬到阿里云上也是花費(fèi)了不少功夫,記錄下來(lái),留個(gè)紀(jì)念


  1. 安裝Ubuntu系統(tǒng)。在購(gòu)買阿里云ECS時(shí)候選擇Ubuntu14.04.4 64位版本,安裝完成后可以使用WinSCP+putty工具來(lái)登陸ubuntu系統(tǒng)

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

  3. 配置nginx。可使用WinSCP圖形化界面,打開(kāi)/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為項(xiàng)目代碼所在目錄
    #index在中間添加index.php
    root /var/www/zshanjun/public;
    index index.php index.html index.htm;

    # Make site accessible from http://localhost/
    #填寫購(gòu)買的阿里云外網(wǎng)IP或者自己已經(jīng)解析備案的域名
    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,也就是個(gè)人軟件包集。
sudo apt-get install python-software-properties software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
  • 安裝PHP7以及所需的一些擴(kuò)展
    sudo apt-get install php7.0-fpm php7.0-cli php7.0-mcrypt php7.0-mysql php7.0-mbstring php7.0-xml

5.** 配置PHP7**

  • 打開(kāi)php.ini配置文件,找到cgi.fix_pathinfo選項(xiàng),去掉注釋,然后將值設(shè)置為0 (vim使用“/”進(jìn)入查找模式)
    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
途中會(huì)提示設(shè)置MySQL的密碼,安裝好后:
mysql -u root -p
然后輸入剛剛設(shè)置的密碼,能成功進(jìn)入即成功安裝。

7.** 安裝Laravel項(xiàng)目**

  • 安裝Composer,分別執(zhí)行以下命令:
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上創(chuàng)建一個(gè)私有項(xiàng)目laravel,里面包含所有該Laravel項(xiàng)目所需代碼。
  • 使用git將代碼clone到服務(wù)器上
cd /var

mkdir www

cd www

git clone your-project-git-link
  • 修改Laravel項(xiàng)目的訪問(wèn)權(quán)限
sudo chown -R :www-data /var/www/laravel

sudo chmod -R 775 /var/www/laravel/storage
  • 導(dǎo)入Laravel 的vendor目錄
cd /var/www/laravel

composer install

8.訪問(wèn)網(wǎng)站。在瀏覽器輸入外網(wǎng)IP或者綁定的域名就可訪問(wèn)網(wǎng)站啦。如果有數(shù)據(jù)庫(kù),使用mysql -u root -p進(jìn)入mysql數(shù)據(jù)庫(kù),創(chuàng)建數(shù)據(jù)庫(kù),再使用php artisan migrate生成數(shù)據(jù)表。


其他問(wèn)題

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

再將下面的代碼粘貼進(jìn)去即可:

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和擴(kuò)展的時(shí)候可能因?yàn)椴糠纸M件有問(wèn)題而不能安裝的,這時(shí)候可以分開(kāi)安裝,逐個(gè)排除問(wèn)題。


更新 2016-6-27

使用Navicat遠(yuǎn)程登陸阿里云Mysql數(shù)據(jù)庫(kù)

  1. 修改sudo vim /etc/mysql/my.cnf文件,將#bind-address = 127.0.0.0一行注釋掉
  2. 更改Mysql授權(quán),并重啟使配置生效。
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一欄填寫阿里云外網(wǎng)IP,用戶名為root,密碼為你的密碼,其他可保持不變

參考文章

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

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

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