Author Topic: Ubuntu 16.04 + Nginx + PHP7 + Laravel + MongoDB  (Read 3899 times)

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Ubuntu 16.04 + Nginx + PHP7 + Laravel + MongoDB
« on: เมษายน 04, 2017, 09:33:45 AM »
วันนี้มีโจทย์ว่าจะทำสร้าง instance เป็น Ubuntu 16.04 + Nginx + PHP7 + Laravel + MongoDB
เพื่อเตรียมเทสงาน mongodb replica เหตุที่เลือกเป็น nginx เพราะว่าตัว engine ทำงานได้ไวกว่า Apache และเหมาะกับงานมากกว่าครับ
แล้วใช้ framework เป็น Laravel + NoSQL Database เป็น MongoDB
และให้สามารถเรียกเว็บเป็น

URL1 => http://ip_address
URL2 => http://nginx.pkl.com

ได้

##เริ่มแรก กัน Begin step
apt-get update
apt-get install git nginx
add-apt-repository ppa:ondrej/php-7.0
apt-get update
apt-get install php7.0-fpm php7.0 php7.0-fpm php7.0-mbstring php7.0-xml php7.0-curl

##ทำการ configure ไฟล์ default หรือถ้าสร้าง virtual host ก็จะเป็นชื่อ domain เช่น packetlove.com
vi /etc/nginx/sites-available/default

#เพิ่ม configure ให้ใช้งานกับ PHP7 + PHP7.0-FPM ครับ
vi /etc/nginx/sites-available/default
cat <<EOF > /etc/nginx/sites-available/default
server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/html;
    index index.php index.html index.htm;

    server_name _;
    charset   utf-8;

    gzip on;
    gzip_vary on;
    gzip_disable "msie6";
    gzip_comp_level 6;
    gzip_min_length 1100;
    gzip_buffers 16 8k;
    gzip_proxied any;
    gzip_types
        text/plain
        text/css
        text/js
        text/xml
        text/javascript
        application/javascript
        application/x-javascript
        application/json
        application/xml
        application/xml+rss;

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

    location ~ \.php$ {
        try_files \$uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|svg|woff|woff2|ttf)$ {
      expires 1M;
      access_log off;
      add_header Cache-Control "public";
    }

    location ~* \.(?:css|js)$ {
      expires 7d;
      access_log off;
      add_header Cache-Control "public";
    }

    location ~ /\.ht {
        deny  all;
    }

}
EOF

##ทำการสร้าง virtual host ใช้งานกับ URL=> nginx.pkl.com  และใช้กับ ipv4 เท่านั้น ipv6 ปิดไว้ครับ
vi /etc/nginx/sites-available/nginx.pkl.com
cat <<EOF > /etc/nginx/sites-available/nginx.pkl.com
server {
    listen 80;
    #listen [::]:80 ipv6only=on;

    root /var/www/nginx.pkl.com/html;
    index index.php index.html index.htm;

    server_name _;
    charset   utf-8;

    gzip on;
    gzip_vary on;
    gzip_disable "msie6";
    gzip_comp_level 6;
    gzip_min_length 1100;
    gzip_buffers 16 8k;
    gzip_proxied any;
    gzip_types
        text/plain
        text/css
        text/js
        text/xml
        text/javascript
        application/javascript
        application/x-javascript
        application/json
        application/xml
        application/xml+rss;

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

    #Support php7 on nginx
    location ~ \.php$ {
        try_files \$uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        ##support php script
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|svg|woff|woff2|ttf)$ {
      expires 1M;
      access_log off;
      add_header Cache-Control "public";
    }

    location ~* \.(?:css|js)$ {
      expires 7d;
      access_log off;
      add_header Cache-Control "public";
    }

    location ~ /\.ht {
        deny  all;
    }

}
EOF

##ทำการเพิ่ม cgi.fix ลงไปใน php.ini
echo ‘cgi.fix_pathinfo=0’ >> /etc/php/7.0/fpm/php.ini

##ทำการ restart nginx + php7.0-fpm
systemctl restart nginx
systemctl restart php7.0-fpm

##ทำการ enable nginx + php7.0-fpm
systemctl enable nginx
systemctl enable php7.0-fpm

## ทำการ install composer & laravel ซึ่งสามารถลงไว้กับเว็บที่จะใช้งาน laravel ได้เลยครับ ในเคสนี้ลงใน Document Root ของเว็บ nginx.pkl.com

cd ~
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
cd /var/www/nginx.pkl.com/html
rm -f index.nginx-debian.html
##install laravel on folder /var/www/nginx.pkl.com/html/laravel
composer create-project laravel/laravel /var/www/nginx.pkl.com/htm/laravel

chown -R www-data:www-data /var/www/nginx.pkl.com/laravel
chmod -R 775 /var/www/nginx.pkl.com/html/laravel/storage

##install mongodb on ubuntu 16.04
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927

echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

##เริ่มทำการ install
apt-get update
apt-get install -y mongodb-org

nano /etc/systemd/system/mongodb.service

#### Start mongodb.service ####
[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target

[Service]
User=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf

[Install]
WantedBy=multi-user.target
#### End mongodb.service ####


##Start service mongodb
systemctl start mongodb
##Enable service mongodb ตอนเครื่อง restart
systemctl enable mongodb

##allow traffic in fw
ufw app list

##เช็คดูสถานะของ firewall
ufw status

#if allow specific ip to connect this mongoldb server
ufw allow from your_other_server_ip/32 to any port 27017

##to allow SSH to connect
ufw allow OpenSSH

###Reference document
#Install mongodb on ubuntu16
https://www.digitalocean.com/community/tutorials/how-to-install-mongodb-on-ubuntu-16-04
#Install laravel on ubuntu
https://www.digitalocean.com/community/questions/installing-laravel-5-1-with-nginx-error500
https://asked.io/how-to-install-php-7-x--nginx-1-9-x---laravel-5-x
https://www.sitepoint.com/setting-up-php-7-servers-with-laravel-forge-and-digitalocean/
http://www.morphatic.com/2015/11/24/installing-laravel-5-1-at-digital-ocean-with-php-7/
http://www.tecmint.com/install-nginx-mysql-php-lemp-in-ubuntu-16-10-ubuntu-16-04/
« Last Edit: เมษายน 04, 2017, 09:41:31 AM by golfreeze »