LoginSignup
3
4

More than 5 years have passed since last update.

サーバーセットアップ(CentOS7 + Node.js + Mysql + Nginx)

Last updated at Posted at 2018-03-18

立ち上げ

Vultrの管理画面から新規サーバーを追加する。
*Vultrってなんだ、って人はここからどうぞ

管理画面のpasswordでサーバーに入る。

サーバーセットアップ


# ユーザー追加、パスワード設定
$ useradd hoge_user
$ passwd hoge_user
# sudoの設定
$ usermod -G wheel hoge_user
$ visudo

wheelのところコメントアウトされてるので、下記の様に有効にする。

## Allows people in group wheel to run all commands 
%wheel ALL=(ALL) ALL

設定したら、一般ユーザーでログインし直す。


# sudo試してみる、問題無くできたらおk
$ sudo ls

次にssh周りをやっていく。先に鍵を置いておくことにする。ssh鍵を作るので、一旦サーバーから出て、ローカルで鍵を作る。


$ cd ~
$ mkdir .ssh
$ cd ~/.ssh
# 4096bitのid_rsaという名前のkeyを作る(秘密鍵:id_rsa, 公開鍵:id_rsa.pubが作成される)
$ ssh-keygen -f id_rsa -t rsa -b 4096
# 公開鍵をサーバー側に送る
$ scp ~/.ssh/id_rsa.pub hoge_user@IPアドレス:~/

hoge_userでサーバー側に入る


# hoge_userのままでやる
$ cd /home/hoge_user
$ mkdir .ssh
$ chmod 700 .ssh
$ cat id_rsa.pub > .ssh/authorized_keys 
$ chmod 600 .ssh/authorized_keys 
$ rm -f id_rsa.pub

これで一応鍵指定でsshログインできるようになるはず。 $ ssh -i ~/.ssh/id_rsa hoge_user@IPアドレス

さらにsshの設定をしていく。(sshポート変更、rootログイン禁止、パスワードログイン禁止)

の前に、さくっとfirewall系の設定をしていく。

次にfirewall対応していく。ざっとコマンド確認。起動してなかったら起動させておく。


# status
$ systemctl status firewalld

# start
$ systemctl start firewalld

# stop
$ systemctl stop firewalld
# portsとかservicesとかを確認
$ firewall-cmd --list-all

sshであけるポートを書いておく。

/usr/lib/firewalld/services/ssh.xml
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>SSH</short>
  <description>Secure Shell (SSH) is a protocol for logging into and executing commands on remote machines. It provides secure encrypted communications. If you plan on accessing your machine remotely via SSH over a firewalled interface, enable this option. You need the openssh-server package installed for this option to be useful.</description>
  <port protocol="tcp" port="12345"/>
</service>

# 反映
$ systemctl reload firewalld.service

今回利用するhttp(80)、https(443)、smtp(25)、smtps(587)もここで合わせて対応しておく。

$ firewall-cmd --add-port=80/tcp --zone=public --permanent
$ firewall-cmd --add-port=443/tcp --zone=public --permanent
$ firewall-cmd --add-port=25/tcp --zone=public --permanent
$ firewall-cmd --add-port=587/tcp --zone=public --permanent
$ firewall-cmd --add-service=http --zone=public --permanent
$ firewall-cmd --add-service=https --zone=public --permanent
$ firewall-cmd --add-service=smtp --zone=public --permanent
$ firewall-cmd --add-service=smtps --zone=public --permanent

$ firewall-cmd --reload

さて、ここでsshに戻ってきて対応やっていく。(sshポート変更、rootログイン禁止、パスワードログイン禁止)


$ vi /etc/ssh/sshd_config

で中身を、

#Port 22 
→Port 12345

#PermitRootLogin yes 
→PermitRootLogin no 

PasswordAuthentication yes 
→PasswordAuthentication no    

設定したら、反映させる。


$ systemctl restart sshd.service

ここまでで、ローカルからhoge_userで鍵ログインだけでログインできるようになってるはず。$ ssh -p 12345 -i ~/.ssh/id_rsa hoge_user@IPアドレス(一応ポート・鍵指定した)

楽するために、ローカルに設定を書いておく。


$ vim ~/.ssh/config
~/.ssh/config
Host hogepiyo
    HostName IPアドレス
    Port 12345
    User hoge_user
    IdentityFile ~/.ssh/id_rsa

こうしとくと、$ ssh hoge_user@hogepiyoでログインできる。

Node.jsセットアップ

nvmでnode入れる。

$ sudo curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

$ export NVM_DIR="$HOME/.nvm"
$ [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
$ [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"

$ nvm -v

$ nvm install stable

$ node -v
$ npm -v
$ nvm ls

これでNodeも入った! :tada:

おまけ、mysql入れる

まずはじめに、MariaDBがデフォルトで導入されているかどうか確認しておきます。

$ yum list installed | grep maria
mariadb-libs.x86_64 1:5.5.44-2.el7.centos @base

もし見つかった場合は削除しておきます。

$ sudo yum remove mariadb-libs.x86_64

https://dev.mysql.com/downloads/repo/yum/ のリンク先で、Downloadの先の「No thanks, just start my download.」のリンク先をコピーします。

# インストール
$ yum localinstall http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm(上記でコピーしたリンク)
$ yum install mysql mysql-devel mysql-server mysql-utilities

# 確認
$ mysql --version

# 起動
$ systemctl enable mysqld.service
$ systemctl start mysqld.service

参考:
- https://qiita.com/prgseek/items/7c77d4b14d0afbf84f5c
- http://vdeep.net/centos7-mysql

mysql_secure_installationコマンドを利用すると、初期rootパスワード変更など行えて便利。

$ mysql_secure_installation

初期パスワードは、 grep password /var/log/mysqld.log で確認。

# ユーザー追加
mysql -u root -- rootでログイン
create user {user名}@localhost identified by {パスワード} -- ユーザー作成


-- test_dbの全権限をtestユーザーに追加
grant all on test_db.* to test@localhost identified by {パスワード};


-- 作成したユーザーでログイン
mysql -u test -p

-- db確認。 権限を追加したDBを確認できる。
show databases

nginx入れる

$ yum install nginx
無いので、レポ作って入れる

リポジトリを追加するので nginx.repo のファイルを作成

$ sudo vi /etc/yum.repos.d/nginx.repo
nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=0
enabled=1
nginxをインストール
$ sudo yum install nginx
バージョンの確認
$ nginx -v
自動起動設定
$ sudo systemctl enable nginx
起動
$ sudo systemctl start nginx

アクセスするとnginxページが表示される。
起動させた単純なexpressのAppを表示する。

$ express testApp -e ejs
$ vi /etc/nginx/conf.d/testapp.conf
/etc/nginx/conf.d/testapp.conf
upstream testApp {
    server 127.0.0.1:3000;
}

server {
    listen       80;

    # アクセスするIPアドレスか、ドメイン名
    server_name  hogehoge.com;

    proxy_redirect                          off;
    proxy_set_header Host                   $host;
    proxy_set_header X-Real-IP              $remote_addr;
    proxy_set_header X-Forwarded-Host       $host;
    proxy_set_header X-Forwarded-Server     $host;
    proxy_set_header X-Forwarded-For        $proxy_add_x_forwarded_for;
    location / {
        proxy_pass http://testApp/;
    }
}

これでtestApp起動し、hogehoge.comで表示できたらOK

*かなり簡易版ですが、足りない設定などあれば、ぜひ追記・コメント下さいmm

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4