LAMP環境構築 - mitsuaki0717/shino-repo GitHub Wiki
LAMP環境を作ってみよう!!!
RHEL7にLAMP環境を構築し、MySQLのテーブル情報をPHPで取得しHTMLで表示する。
======================
●Apacheインストール
======================
yum -y install expat-devel
◆porgインストール
tar xzvf porg-0.8.tar.gz
cd porg-0.8
./configure —prefix=/usr/local —disable-grop
make && make install
make logme
◆Apacheインストール
事前準備1(APR)
wget http://ftp.jaist.ac.jp/pub/apache//apr/apr-1.5.2.tar.gz
tar zxvf apr-1.5.2.tar.gz
cd apr-1.5.2
./configure —prefix=/opt/apr-1.5.2
make
porg -lD “make install”
porg -a
事前準備2(APR-Unit)
wget http://ftp.jaist.ac.jp/pub/apache//apr/apr-util-1.5.4.tar.gz
tar zxvf apr-util-1.5.4.tar.gz
cd apr-util-1.5.4
./configure —prefix=/opt/apr-util-1.5.4 —with-apr=/opt/apr-1.5.2
make
porg -lD “make install”
porg -a
事前準備3(PCRE)
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz
tar zxvf pcre-8.37.tar.gz
cd pcre-8.37
./configure —prefix=/opt/pcre-8.37
make
porg -lD “make install”
==========
アンインストール
- porg -a
python-3.6.1 - porg -r python-3.6.1
==========
Apache インストール
wget ftp://ftp.meisei-u.ac.jp/mirror/apache/dist/httpd/httpd-2.4.25.tar.gz
tar zxvf httpd-2.4.20.tar.gz
cd httpd-2.4.20
./configure —prefix=/opt/httpd-2.4.20 —with-apr=/opt/apr-1.5.2 —with-apr-util=/opt/apr-util-1.5.4 —with-pcre=/opt/pcre-8.37 —enable-so —enable-mods-shared=all —enable-proxy=shared —enable-proxy-ajp=shared —with-mpm=worker
make
porg -lD “make install”
■Apacheの自動起動設定
- vi /etc/systemd/system/httpd.service
以下を記載
[Unit]
Description=The Apache2.4 HTTP Server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/opt/httpd-2.4.20/bin/httpd -k start
ExecReload=/opt/httpd-2.4.20/bin/httpd -k restart
ExecStop=/opt/httpd-2.4.20/bin/httpd -k stop
[Install]
WantedBy=multi-user.target
- systemctl daemon-reload
- systemctl start httpd.service
- systemctl enable httpd.service
============
●MySQLインストール
============
yum -y remove mariadb-libs
RHEL6にRPMインストール
mysql-community-client-5.7.18-1.el6.×8664.rpm
mysql-community-common-5.7.18-1.el6.×8664.rpm
mysql-community-libs-5.7.18-1.el6.×86_64.rpm
mysql-community-libs-compat-5.7.18-1.el6.×8664.rpm
mysql-community-server-5.7.18-1.el6.×8664.rpm
上記のRPMをインストールする
RHEL7にRPMインストール
mysql-community-client-5.7.19-1.el7.×8664.rpm
mysql-community-devel-5.7.19-1.el7.×8664.rpm
mysql-community-libs-5.7.19-1.el7.×8664.rpm
mysql-community-libs-compat-5.7.19-1.el7.×8664.rpm
mysql-community-common-5.7.19-1.el7.×8664.rpm
mysql-community-server-5.7.19-1.el7.×8664.rpm
RHEL6と同様
詳細は以下のURLを確認
https://dev.mysql.com/doc/refman/5.6/ja/linux-installation-yum-repo.html
yumを利用する場合は
mysql57-community-release-el6-11.noarch.rpmをインストールすることでyumでのインストールが可能
yumを使う場合はDNSが必要
yum localinstall mysql57-community-release-el6-11.noarch.rpm
■初期設定
- systemctl start mysqld
- cat /var/log/mysqld.log | grep ‘password is generated’
[root@TEST-LAMP mysql]# mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
The existing password for the user account root has expired. Please set a new password.
New password:
Re-enter new password:
The ‘validate_password’ plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.
1. rootのパスワード変更
Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y
New password:
Re-enter new password:
Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
2. anonymousの削除
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
Normally, root should only be allowed to connect from
‘localhost’. This ensures that someone cannot guess at
the root password from the network.
3. リモートからのrootユーザでのログインの禁止
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.
By default, MySQL comes with a database named ‘test’ that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
4. testデータベースの削除
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
– Dropping test database…
Success.
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done!
- vi /etc/my.cnf
以下を追記
character-set-server=utf8
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
- Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[client]
default-character-set=utf8
[mysqldump]
default-character-set=utf8
[mysql]
default-character-set=utf8
======================
●PHPインストール
======================
■PHPのインストール
- yum install libxml2-devel
- tar zxvf php-7.1.7.tar.gz
- cd php-7.1.7
- ./configure —with-apxs2=/usr/local/apache2/bin/apxs —with-mysql-sock=/var/lib/mysql/mysql.sock —enable-mbstring —enable-mysqlnd —with-pdo-mysql —with-mysqli
- make
- porg -lD “make install”
■PHPの設定
- vi /usr/local/apache2/conf/httpd.conf
以下の設定を追加
AddType application/x-httpd-php .html
<FilesMatch \.php$>
SetHandler application/x-httpd-php
以下の記載があるかを確認する
LoadModule php7_module modules/libphp7.so
vi /usr/local/apache2/htdocs/phpinfo.php
<?php
phpinfo();
?>
======================
●MySQLを使ってみよう!
======================
・MySQLに接続
- mysql -u root -p
・データベース作成
CREATE DATABASE test_db;
・ユーザ作成
CREATE USER ’user01’@’localhost’ IDENTIFIED BY ‘P@ssw0rd’;
GRANT ALL PRIVILEGES ON test_db.* TO ’user01’@’localhost’;
・ユーザ権限確認
SHOW GRANTS FOR ’user01’@’localhost’;
mysql -u user01 -p test_db
・テーブル作成
SHOW TABLES;
CREATE TABLE test_table ( NO smallint, NAME varchar(50) );
SHOW TABLES;
DESC test_table;
INSERT INTO test_table (NO, NAME) values (1, ‘柳’);
INSERT INTO test_table (NO, NAME) values (2, ‘齋藤’);
INSERT INTO test_table (NO, NAME) values (3, ‘篠原’);
SELECT * FROM test_table;
・テーブル削除
DROP TABLE test_table;
・テーブルにカラムを追加/変更
ALTER TABLE test_table ADD DATE date;
ALTER TABLE test_table MODIFY DATE date;
・既存のレコードにデータを追加
UPDATE test_table SET DATE=20170910 where NAME=“柳”;
カラムの順番を変更
ALTER TABLE test_table MODIFY datetime AFTER kana;
カラムの型変更
ALTER TABLE test_table CHANGE NO NO int not null auto_increment primary key;
ALTER TABLE test_table ADD PRIMARY KEY NO;
・レコード削除
DELETE FROM test_table WHERE NO=5;
=================
サンプルコード(mysqlのテーブル情報を出力)
=================
<?php
$dsn = ‘mysql:host=localhost;dbname=test_db;charset=utf8’;
$user = ‘user01’;
$password = ‘P@ssw0rd’;
try{
$pdo = new PDO;
print(‘接続に成功しました。
’);
$pdo→query(‘SET NAMES utf8’);
$sql = ‘SELECT * FROM test_table’;
foreach ($pdo→query($sql) as $row) {
print($row[‘NO’]);
print “:”;
print($row[‘NAME’].‘
’);
}
}catch (PDOException $e){
print(‘Error:’.$e→getMessage());
die();
}
$pdo = null;
?>
======================
●phpMyAdminインストール
======================
- wget https://files.phpmyadmin.net/phpMyAdmin/4.7.4/phpMyAdmin-4.7.4-all-languages.tar.gz
・phpMyAdminのインストール
- tar zxvf phpMyAdmin-4.7.4-all-languages.tar.gz
- mv phpMyAdmin-4.7.4-all-languages /usr/local/apache2/htdocs/phpmyadmin
・phpMyAdmin用のDBを作成
- mysql -u root -p
mysql> source /usr/local/apache2/htdocs/phpmyadmin/sql/create_tables.sql - cd /usr/local/apache2/htdocs/phpmyadmin
- cp config.sample.inc.php config.inc.php
- vi config.inc.php
$cfg[‘blowfish_secret’] = ’’; ←MySQLのrootパスワードを入力
$cfg[‘Servers’][$i][‘auth_type’] = ‘cookie’;
$cfg[‘Servers’][$i][‘host’] = ‘localhost’;
$cfg[‘Servers’][$i][‘connect_type’] = ‘tcp’;
$cfg[‘Servers’][$i][‘compress’] = false;
$cfg[‘Servers’][$i][‘AllowNoPassword’] = false;
$cfg[‘DefaultLang’] = ‘ja-utf-8’;
以下のコメントを外す(//)
$cfg[‘Servers’][$i][‘pmadb’] = ‘phpmyadmin’;
$cfg[‘Servers’][$i][‘bookmarktable’] = ‘pma__bookmark’;
$cfg[‘Servers’][$i][‘relation’] = ‘pma__relation’;
$cfg[‘Servers’][$i][‘table_info’] = ‘pma__table_info’;
$cfg[‘Servers’][$i][‘table_coords’] = ‘pma__table_coords’;
$cfg[‘Servers’][$i][‘pdf_pages’] = ‘pma__pdf_pages’;
$cfg[‘Servers’][$i][‘column_info’] = ‘pma__column_info’;
$cfg[‘Servers’][$i][‘history’] = ‘pma__history’;
$cfg[‘Servers’][$i][‘table_uiprefs’] = ‘pma__table_uiprefs’;
$cfg[‘Servers’][$i][‘tracking’] = ‘pma__tracking’;
$cfg[‘Servers’][$i][‘userconfig’] = ‘pma__userconfig’;
$cfg[‘Servers’][$i][‘recent’] = ‘pma__recent’;
$cfg[‘Servers’][$i][‘favorite’] = ‘pma__favorite’;
$cfg[‘Servers’][$i][‘users’] = ‘pma__users’;
$cfg[‘Servers’][$i][‘usergroups’] = ‘pma__usergroups’;
$cfg[‘Servers’][$i][‘navigationhiding’] = ‘pma__navigationhiding’;
$cfg[‘Servers’][$i][‘savedsearches’] = ‘pma__savedsearches’;
$cfg[‘Servers’][$i][‘central_columns’] = ‘pma__central_columns’;
#vi /usr/local/apache2/conf/phpmyadmin.conf
Alias /phpmyadmin /usr/local/apache2/htdocs/phpmyadmin
<Directory /usr/local/apache2/htdocs/phpmyadmin/>
AddDefaultCharset UTF-8
- Apache 2.4の場合
Require ip 127.0.0.1
Require all granted
<Directory /usr/local/apache2/htdocs/phpmyadmin/libraries/>
Order Deny,Allow
Deny from All
Allow from None
<Directory /usr/local/apache2/htdocs/phpmyadmin/setup/lib/>
Order Deny,Allow
Deny from All
Allow from None
<Directory /usr/local/apache2/htdocs/phpmyadmin/setup/frames/>
Order Deny,Allow
Deny from All
Allow from None
- vi httpd.conf
以下追記
#phpMyAdmin settings
Include /usr/local/apache2/conf/phpmyadmin.conf
・PHPの設定ファイル作成
- cp php-7.1.7/php.ini-production /usr/local/lib/php.ini
- vi /usr/local/lib/php.ini
以下のコメントを外す
;extension=php_mysqli.dll
- systemctl restart httpd.service
以下のURLよりphpmyadminに接続
http://10.2.124.89/phpmyadmin/index.php
======================
●WordPressインストール 微妙な感じになってる
======================
- mysql -u root -p
mysql> CREATE DATABASE db_wordpress;
mysql> GRANT ALL PRIVILEGES ON db_wordpress.* TO ur_wordpress@localhost IDENTIFIED BY “P@ssw0rd”;
mysql> FLUSH PRIVILEGES;
mysql> EXIT
・wordpressのインストール
wget https://ja.wordpress.org/wordpress-4.8.1-ja.tar.gz
tar zxvf wordpress-4.8.1-ja.tar.gz
mv wordpress /usr/local/apache2/htdocs
cd /usr/local/apache2/htdocs/wordpress
cp wp-config-sample.php wp-config.php
vi wp-config.php
以下のURLよりwordpressに接続
http://10.2.124.89/wordpress/wp-admin/install.php