MENU



簡単!Proxy環境下でVMWareを使用しUbuntu 18.04 に nginx、mysql、php、phpmyadmin環境作成方法(スクレイピングのため)

ども♪普段はシステムエンジニアをしているマコトです。

Proxy環境下でVMWareを使用しUbuntu18.04仮想マシンにスクレイピングの環境構築をしてみました。

今回は取得したデータをmysqlにインサートしたかったため、mysqlもインストールします。

構成はPHP、nginx、mysqlです。

nginx、mysqlインストール

nginxとmysqlをインストールします

sudo apt-get install nginx mysql-server

nginxの起動を確認します。

sudo systemctl start nginx

ブラウザーで http://localhost としてnginxのトップページが表示されることを確認してください。

php、php-fpmなどをインストール

php、php-fpmなどをインストールします。

sudo apt-get install php php-fpm php-mysql php-gettext php-common php-mbstring php-mbstring

php.iniの設定変更します。

sudo vim /etc/php/7.2/fpm/php.ini

cgi.fix_pathinfo=1を0に変更します。(776行目辺り)

cgi.fix_pathinfo=0

nginx php連携

設定ファイルの変更します。

sudo vim /etc/nginx/sites-available/default

以下をserver {}内に追記します。

    location ~ ¥.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

テストファイルを作成します。

sudo vim /var/www/html/test.php

以下の1行を記述してください。

<?php phpinfo(); ?>

nginx再起動をします。

sudo systemctl restart nginx

firefoxを立ち上げ、http://localhost/test.php へアクセスします。

PHPのバージョン情報が表示がされればOKです。

phpmyadminのインストール

phpmyadminをインストールします。

sudo apt-get install phpmyadmin

途中でWebサーバの種類を聞かれるので「apache2」を選択します。(nginxを使用するので使わないがとりあえず…)
dbconfig-commonで設定しますか?と聞かれるので「yes」を選択。続いてMySQLサーバ上の「phpmyadmin」ユーザのパスワードを聞かれるので2回入力します。

設定ファイルのの変更します。

sudo vim /etc/nginx/sites-available/default

以下をserver {}内に追記します。

location /phpmyadmin {
    root /usr/share;
    index index.php;
 
    location ~ ^/phpmyadmin.+¥.php$ {
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
 
}

nginx再起動します。

sudo systemctl stop nginx
sudo systemctl start nginx

動作確認します。

http://localhost/phpmyadmin/index.phpにアクセスして、以下のような表示になれば成功です。

念の為、ログインできるか確認してください。

ID:phpmyadminのインストール時に設定したID
pw:phpmyadminのインストール時に設定したパスワード

Mysql rootパスワードの設定

root権限で接続できないと後々、設定が面倒なのでrootパスワードを設定します。

ターミナルでmysqlに接続します。

sudo mysql -u root

mysqlにしたら、以下のコマンドを入力し、パスワードを設定します。

mysql> USE mysql
mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='root';
mysql> FLUSH PRIVILEGES; (ユーザー権限の確認)
mysql> exit;

参考(実際のコマンドやり取り)

makoto@ubuntu:~$ sudo mysql -u root
Welcome to the MySQL monitor. Commands end with ; or ¥g.
Your MySQL connection id is 9
Server version: 5.7.23-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '¥h' for help. Type '¥c' to clear the current input statement.

mysql> USE mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> exit;
Bye

パスワードを設定します。

mysql -u root
mysql> SET PASSWORD FOR root@'localhost' = PASSWORD('makoto');
mysql> exit;

参考(実際のコマンドやり取り)

makoto@ubuntu:~$ mysql -u root
Welcome to the MySQL monitor. Commands end with ; or ¥g.
Your MySQL connection id is 11
Server version: 5.7.23-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '¥h' for help. Type '¥c' to clear the current input statement.

mysql> SET PASSWORD FOR root@'localhost' = PASSWORD('makoto');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> exit;
Bye

MySQLを外部から接続できるようにする設定

開発環境とDB環境を分けたかったため設定しました。

外部接続用のユーザを作成するため、ルートで接続します。

mysql -u root -p

リモートユーザーを作成します。

mysql> create user remoteuser identified by 'password';

ユーザに外部から接続できる権限を付与します。

mysql> grant all privileges on *.* to remoteuser@"%" identified by 'password';

※privileges on databasename.tablename とデータベース・テーブル単位でのアクセス制限可能です。 また、@以降は「172.16.24.%」と、IPを指定する事ができます。

設定内容を反映させます。

mysql> flush privileges;

追加・編集したユーザの内容は、以下のコマンドで確認します。

mysql> select user, host from mysql.user;

参考(実際のコマンドやり取り)

makoto@ubuntu:~$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or ¥g.
Your MySQL connection id is 28
Server version: 5.7.23-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '¥h' for help. Type '¥c' to clear the current input statement.

mysql> create user remoteuser identified by 'makoto';
create user remoteuser identified by 'makoto';
^C
mysql> grant all privileges on *.* to remoteuser@"%" identified by 'makoto';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> select user, host from mysql.user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| remoteuser | % |
| debian-sys-maint | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| phpmyadmin | localhost |
| root | localhost |
+------------------+-----------+
6 rows in set (0.00 sec)

mysql> exit;
Bye

 

ファイアーウォールの設定

# ufwがなければインストールします。

sudo apt-get install ufw

mysqlのデフォルトポート3306の通信を許可します。

sudo ufw allow 3306

IPアドレスの制限を外します。

mysqlでは接続元のIPを制限しているため,その設定を変更しないと外部から接続できない模様です。

mysqld.cnfの編集します。

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

bind-addressをコメントアウトします。(44行目辺り)

# bind-address = 127.0.0.1

mysqlを再起動します。

sudo systemctl restart mysql

これで、remoteuserで接続できるはず。

最後にアップグレード

sudo apt-get update
sudo apt-get -y upgrade
sudo apt-get -y dist-upgrade
sudo apt-get clean
sudo apt-get -y autoremove

conda update conda
conda update --all

以上です。お疲れ様でした!

備忘録

MySQLの停止/起動/再起動/ステータス

sudo systemctl stop mysql
sudo systemctl start mysql
sudo systemctl restart mysql
sudo systemctl status mysql

 

自動起動の無効化/有効化

sudo systemctl disable mysql
sudo systemctl enable mysql

 

今、松井証券、DMM、でお得なキャンペーンが行われています。
相場が低迷している今こそ小さな金額から投資を始めるのはいいかもしれません。

松井証券FX 新規口座開設獲得プロモーション
漫画★全巻ドットコム
【DMM FX】入金
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

コメント

コメントする

目次