2016年9月8日 星期四

ubuntu 增加有admin權限的使用者帳戶


建立帳戶
adduser allie

接下來就會有一些資訊,填完之後就會自動建立好home dir


要讓帳戶可以使用sudo
sudo usermod -a -G sudo hduser



收工

ubuntu change hostname

vagrant@vagrant:/$ sudo vim /etc/hostname




















vagrant@vagrant:/$ sudo vim /etc/hosts




















vagrant@vagrant:/$ sudo reboot now

2016年9月7日 星期三

架設Apache及vsftpd設定


Install apache2

sudo apt-get update
sudo apt-get install apache2
sudo apt-get install apache2 apache2-doc apache2-utils



若 sudo service apache2 restart 時出現
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message

只要在/etc/apache2/apache2.conf加入
ServerName localhost
即可解決



Install & setting vsftpd

sudo apt-get update
sudo apt-get install vsftpd libpam-pwdfile


安裝好了之後,接下來要設定vsftp
本文設定為針對此目錄結構:


















假設www資料夾裡存放網站程式碼(html)及專案資料(ftp),
html目錄下會有許多專案 (以下以wiff為例),
每個專案裡會有一個使用者帳號,
只能存取自己專案資料夾裡的資料。


現在,假設資料夾都已經建好,
立馬開始進行vsftpd的設定:
sudo vim /etc/vsftpd.conf

進入編輯畫面之後,我們要注意的設定如下:
# 預設的情況下,要將使用者限制在自己的家目錄之內
chroot_local_user=YES
# 支援檔案上傳
write_enable=YES
# 為了讓透過 FTP 上傳的檔案的預設群組權限為「可寫入」
local_umask=002
# 支援local端登入使用
local_enable=YES
# 要求vsftpd讓使用者在對自己的家目錄有寫入的權限下仍然允許連線
allow_writeable_chroot=YES

接下來要設定pam(身份認證模組)
sudo vim /etc/pam.d/vsftpd

其實就是全部註解掉,然後加上passwd的設定2行
#Standard behaviour for ftpd(8).
auth   required        pam_listfile.so item=user sense=deny file=/etc/ftpusers onerr=succeed
#Note: vsftpd handles anonymous logins on its own. Do not enable pam_ftp.so.
#Standard pam includes
#@include common-account #註解掉
#@include common-session #註解掉
#@include common-auth #註解掉
#auth   required        pam_shells.so #註解掉


#下面兩行加上
auth    required pam_pwdfile.so pwdfile /etc/vsftpd.passwd
account required pam_permit.so

接下來建立FTP的使用者帳號
# 創造一個不能登入系統的帳號
sudo useradd -M  -s /usr/sbin/nologin htmlAdmin
sudo passwd htmlAdmin

# 第一次設定密碼需要先create htpasswd檔案 (HTTP基本認證的user帳密)
sudo htpasswd -cd /etc/vsftpd.passwd htmlAdmin 
# -c : Create the passwdfile.
# -d : Use crypt() encryption for passwords.
# ps. 如果要創第二組帳號的密碼,去掉-c (因為已經create過了)
# sudo htpasswd -d /etc/vsftpd.passwd user2

接下來設定資料夾,並幫帳號指定根目錄
sudo chmod 555 /var/www/
sudo chmod 755 /var/www/html/
sudo chown htmlAdmin:ftp -R /var/www/html/
sudo usermod --home /var/www/html/ htmlAdmin
sudo service vsftpd restart

好了,這樣應該就可以了~
開一下filezilla來試試看吧!

接下來要設定meteor和wiff 等等子資料夾的權限都跟下面一樣
sudo useradd -M -s /usr/sbin/nologin meteor
sudo passwd meteor
sudo htpasswd -d /etc/vsftpd.passwd meteor
sudo chmod 777 /var/www/html/meteor
sudo chown meteor:ftp -R /var/www/html/meteor/
sudo usermod --home /var/www/html/meteor meteor
sudo service vsftpd restart



最後,注意!切記!
如果要用filezilla來測試,
每次vsftpd restart之後要把filezilla整個關掉再開。



以上參考:
http://hong-s-note.logdown.com/posts/760695-tutorial-ubuntu-set-up-ftp-server-vsftpd
http://ifresh.cc/solution-to-vsftpd-refusing-to-run-with-writable-root-inside-chroot/





SSL(https)設定


現在的apache都已經有內建openssl了,
所以只要裝好apache之後,開啟SSL模組即可
sudo a2enmod ssl
接下來要建立金鑰,為了管理方便,
先建個資料夾,再把金鑰放進去。
sudo mkdir /etc/apache2/ssl
金鑰設定為365天後到期
sudo openssl req -new -x509 -days 365 -nodes -out /etc/apache2/ssl/apache.pem -keyout /etc/apache2/ssl/apache.key
金鑰產生過程會有一些額外資訊要求你填寫,
填寫完之後就算產生完成,
再接著設定apache,
讓預設的http和https都可以使用
sudo vim /etc/apache2/sites-available/000-default.conf
設定內容如下:
<VirtualHost *:443>
        ServerAdmin webmaster@localhost
        ServerName example.com:443
        DocumentRoot /var/www/html
        <Directory />
          Options FollowSymLinks
          AllowOverride None
       </Directory>
       <Directory /var/www/html/>
         Options Indexes FollowSymLinks MultiViews
         AllowOverride None
         Order allow,deny
         allow from all
      </Directory>

      ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
      <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
      </Directory>
      #   SSL Engine Switch:
      #   Enable/Disable SSL for this virtual host.
      SSLEngine on

      #   A self-signed (snakeoil) certificate can be created by installing
      #   the ssl-cert package. See
      #   /usr/share/doc/apache2.2-common/README.Debian.gz for more info.
      #   If both key and certificate are stored in the same file, only the
      #   SSLCertificateFile directive is needed.    
      SSLCertificateFile /etc/apache2/ssl/apache.pem
      SSLCertificateKeyFile /etc/apache2/ssl/apache.key

      ErrorLog ${APACHE_LOG_DIR}/error.log

      # Possible values include: debug, info, notice, warn, error, crit,
      # alert, emerg.
      LogLevel warn

      CustomLog ${APACHE_LOG_DIR}/access.log combined

      Alias /doc/ "/usr/share/doc/"
      <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
      </Directory>

    </VirtualHost>



重啟,搞定!
sudo service apache2 restart






2016年9月5日 星期一

VirtualBox+Vagran與ubuntu網路設定

Virtualbox+Vagrant


目標:快速架設好ubuntu,包含使用者


Virtualbox 與 Vagrant 至官網下載並照預設安裝後,

找一個地方建資料夾準備放Vagrantfile(vagrant的設定檔)。

Vagrantfile可重複建立多個virtualbox os。



接下來到Vagrant Cloud 搜尋想要的box,

這些box是別人整理好的直接灌之後就可以用,

也可以自己上傳。

這裡以ubuntu 14.04 LTS的desktop版本為例。






打開terminal,移動到剛剛建立要來放Vagrantfile的資料夾,

然後照著網頁指示輸入 vagrant init box-cutter/ubuntu1404-desktop;











建好的Vagrantfile出現之後就可以直接下 vagrant up

vagrant就會自動幫你灌好virtualbox的os




噹噹!灌好開好已!



















設定root密碼



由vagrant架好的os通常會預設帳號、密碼都是vagrant。


登入後搜尋並打開terminal輸入指令:sudo passwd root

就可以設定root密碼。

為了應付不時之需,早點設好吧XD


設好之後用su -登入試試看是否設定成功










網路設定


目標:ubuntu有固定IP,並可連上外部網路,且pietty可連入ubuntu


其實通常剛裝完的時候就已經有預設的NAT網路了,

但由於我們要架設的是SERVER,也需要使用putty/pietty來連線以方便操作,

必須有static ip,

所以才需要特別設定這一段網路的部份。


廢話不多說,快趁可以連外網的時候先裝好vim啊啊啊啊啊~~~

更新軟體清單:sudo apt-get update



安裝vim:sudo apt-get install vim


























裝好之後就先幫virtual os關機 準備正式開始設定吧!

首先要使用virtualbox來設定os的(偽)硬體網路










接下來設定軟體部份

本機網路IP:192.168.16.92

要設定給VirtualBox的IP:192.168.16.99

先用ifconfig來設定看看能不能通

sudo ifconfig eth0 192.168.16.99 netmask 255.255.255.0











確定可以通了之後再設到網路設定檔上

使用 root 權限打開 /etc/network/interfaces 檔案

sudo vim /etc/network/interfaces


輸入以下:
iface eth0 inet static
address 192.168.16.99
netmask 255.255.255.0
gateway 192.168.16.1
dns-nameservers 8.8.8.8









存檔後重新啟動網卡,然後ping看看是否可以與本機互通

ubuntu 14.04 LTS的重新啟動指令是sudo ifdown eth0 && sudo ifup eth0















































再ping本機看看、開網頁看看,確定都可以通之後就可以用pietty囉~(灑花

















































※以上許多參考: https://babu0628.wordpress.com/2013/01/09/ubuntu-virtual-box-%E7%B6%B2%E8%B7%AF%E5%B0%8D%E5%A4%96%E9%80%A3%E6%8E%A5%E8%A8%AD%E5%AE%9A/









2016年8月30日 星期二

MQTT 安裝及web socket設定

MQTT:是一個client server的publish/subscribe訊息交換協定

安裝過程:

wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key

sudo apt-key add mosquitto-repo.gpg.key

rm mosquitto-repo.gpg.key

cd /etc/apt/sources.list.d/

sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list

sudo apt-get update

sudo apt-get install mosquitto mosquitto-clients
安裝好之後,
其實就會隨著開機啟動MQTT把這台機器也當作Topic Server,
可以做本地端測試

開啟兩個終端機,分別執行下面的1以及2

1.建立Subscriber(訂閱者),訂閱topic名稱為 TEST_TOPIC
mosquitto_sub -d -t TEST_TOPIC

2.建立Publisher(發布者),發佈到topic名稱為TEST_TOPIC
mosquitto_pub -d -t TEST_TOPIC -m "Test message. Test message. Test message."

此時在1號終端機,Subscriber應該會看到來自Publisher的訊息

而2號終端機所key的那行指令,若預設沒有加上-h指定topic server,則會以自身當作topic server以及一個publisher的腳色

※ 如果要指定port的話 使用-p 8888 參數
※ console使用的port 和 websocket的port不同
=============================================================

使用js與MQTT溝通
sudo vim /etc/mosquitto/mosquitto.conf

修改內容:
 bind_address localhost
port 9999  #預設MQTT的port

#下面2行是設定給websockets的port
listener 8888
protocol websockets



設完重開
sudo service mosquitto restart

server部份設定完成
===============================================================


sample code: https://github.com/xms/MQTT-Client-with-web-socket








2016年8月24日 星期三

Install mongoDB on ubuntu 14.04 LTS

MongoDB is a NoSQL database intended for storing large amounts of data in document-oriented storage with dynamic schemas. NoSQL refers to a database with a data model other than the tabular format used in relational databases such as MySQL, PostgreSQL, and Microsoft SQL. MongoDB features include: full index support, replication, high availability, and auto-sharding.
Pre-Flight Check
  • These instructions are intended for installing MongoDB on a single Ubuntu 14.04 LTS node.
  • I’ll be working from a Liquid Web Core Managed Ubuntu 14.04 LTS server, and I’ll be logged in as a non-root user, but with sudo access. For information on giving a user sudo access visit our page on How to Add a User and Grant Root Privileges on Ubuntu 14.04.
Step #1: Setup a the Package Database
First we’ll import the MongoDB public key used by the package management system:
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
Then we’ll create a list file for MongoDB:
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
Now reload the package database:
sudo apt-get update
Step #2: Install Latest Stable Version MongoDB
At this point, installing MongoDB is as simple as running just one command:
sudo apt-get install -y mongodb-org
If you’d like MongoDB to auto-update with apt-get than you’re done with the installation. But, it’s possible to ‘pin’ the version of MongoDB you just installed to prevent apt-get from auto-updating.
echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-org-shell hold" | sudo dpkg --set-selections
echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo "mongodb-org-tools hold" | sudo dpkg --set-selections
Step #3: Get MongoDB Running
Start-Up MongoDB
sudo service mongod start
Check MongoDB Service Status
sudo service mongod status
Summary List of Status Statistics (Continuous)
mongostat
Summary List of Status Statistics (5 Rows, Summarized Every 2 Seconds)
mongostat --rowcount 5 2
Enter the MongoDB Command Line
mongo
By default, running this command will look for a MongoDB server listening on port 27017 on the localhost interface.
If you’d like to connect to a MongoDB server running on a different port, then use the –port option. For example, if you wanted to connect to a local MongoDB server listening on port 22222, then you’d issue the following command:
mongo --port 22222
Shutdown MongoDB
sudo service mongod stop
Restart MongoDB
sudo service mongod restart

2016年2月27日 星期六

[JS] 陣列排序

一般文字排序:array.sort()

數字排序比較特別,
如果直接下.sort()的話,
[ 12, 5, 20] 會排成 [12, 20, 5]。

解法:
function sortFunction(x,y){return x-y;}
arr.sort(sortFunction);



範例程式
var gimme = function (inputArray) {
  function sortFunction(x,y){return x-y;}  //如果要倒序則將x-y改成y-x
  inputArray.sort(sortFunction);
  return inputArray;
};

alert(gimme([2, 3, 1]); // 1,2,3
alert(gimme([5, 10, 14]); //5,10,14