linux+httpd+php模块+mysqlphp
rpm包的安装方式html
环境:mysql
系统: CentOS Linux release 7.1.1503 (Core)linux
yum源:epel,cdromweb
主机:sql
httpd+php |
172.16.40.30 |
mysql |
172.16.40.31 |
目的:
数据库
一、两台主机实现httpd+php和mysql服务。
二、httpd提供两个虚拟主机,一个虚拟主机提供phpMyAdmin,另外一个虚拟主机提供wordpress。
三、为php安装xcache。
四、为phpMyAdmin提供https;apache
目录:vim
1、httpd
windows
2、php
3、mysql
4、安装phpMyadmin和wordpress
5、为phpMyadmin虚拟主机启用ssl。(包括创建私有CA)
我这里的selinux和iptables都在开启状态,为了简单我这里把它们都关闭了。
selinux:
[root@localhost ~]# getenforce #查看selinux状态。 Enforcing [root@localhost ~]# setenforce 0 #设置selinux不阻止进程,只记录。当前环境生效,重启失效。 [root@localhost ~]# vim /etc/selinux/config #修改配置文件, 重启生效。 SELINUX=disabled
iptables:
[root@localhost ~]# systemctl stop firewalld #关闭防火墙 [root@localhost ~]# systemctl disable firewalld #开机不自动启动。 rm '/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service' rm '/etc/systemd/system/basic.target.wants/firewalld.service'
注意:在mysql上面127.0.0.1与localhost是不一样的。127.0.0.1会用网络套接字来通讯。而 localhost用的才是unix_sock,本地文件。若是要用localhost通讯,有可能要在/etc/php.ini中为3个链接器指定mysql 的sock文件所在位置。
mysql.default_socket = pdo_mysql.default_socket= mysql.default_socket =
1、httpd
一、安装:
[root@localhost ~]# yum install httpd -y
[root@localhost ~]# rpm -ql httpd /etc/httpd /etc/httpd/conf /etc/httpd/conf.d /etc/httpd/conf.d/README /etc/httpd/conf.d/autoindex.conf /etc/httpd/conf.d/userdir.conf /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.modules.d /etc/httpd/conf.modules.d/00-base.conf #加载模块 /etc/httpd/conf.modules.d/00-dav.conf /etc/httpd/conf.modules.d/00-lua.conf /etc/httpd/conf.modules.d/00-mpm.conf #用以修改所使用的mpm。 /etc/httpd/conf.modules.d/00-proxy.conf #代理相关 /etc/httpd/conf.modules.d/00-systemd.conf /etc/httpd/conf.modules.d/01-cgi.conf /etc/httpd/conf/httpd.conf #主配置文件 ..... [root@localhost ~]# systemctl start httpd [root@localhost ~]# ss -tnl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 :::80 :::* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* [root@localhost ~]#
80端口启动了。能够尝试一下打开网页了。
工做正常。
主配置文件中的一些指令能够到官网查看,有专门介绍指令的,如我这里2.4版本的:
http://httpd.apache.org/docs/2.4/mod/directives.html
直接ctrl+f搜索。
二、虚拟主机:
首先把/etc/httpd/conf/httpd.conf配置文件中的DocumentRoot关闭。
#DocumentRoot "/var/www/html" #中心主机的网页目录,虚拟主机里面也要指定。
添加一个虚拟主机的配置文件:
[root@localhost httpd]# vim conf.d/vhosts.conf #当前目录:/etc/httpd <VirtualHost *:80> ServerName www.star.com DocumentRoot /web/www CustomLog "/var/log/httpd/www/access_log" combined ErrorLog "/var/log/httpd/www/error_log" <Directory "/web/www"> Options None Require all granted </Directory> </VirtualHost> <VirtualHost *:80> ServerName myadm.star.com DocumentRoot /web/myadm CustomLog "/var/log/httpd/myadm/access_log" combined ErrorLog "/var/log/httpd/myadm/error_log" <Directory "web/myadm"> Options None Require all granted </Directory> </VirtualHost>
建立所需目录:
[root@localhost httpd]# mkdir -pv /web/{www,myadm} -pv mkdir: created directory ‘/web’ mkdir: created directory ‘/web/www’ mkdir: created directory ‘/web/myadm’ [root@localhost httpd]# mkdir -pv /var/log/httpd/{www,myadm} mkdir: created directory ‘/var/log/httpd/www’ mkdir: created directory ‘/var/log/httpd/myadm’ [root@localhost httpd]#
给两个虚拟主机分别来个网页测试一下。
[root@localhost httpd]# vim /web/www/index.html <h1>www.star.com</h1> [root@localhost httpd]# vim /web/myadm/index.html <h1>myadm.star.com</h1>
测试一下:
[root@localhost httpd]# httpd -t AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message Syntax OK [root@localhost httpd]#
这个错误是由于在主配置文件中的serverName没有配置,httpd会反解本机的IP地址,得出的结果若是与本机的主机名不适合,或者是根本就不能反解, 就会报这个错误。 能够在主配置文件中指定serverName或在/etc/hosts中给个ip和本机主机名的条目。我这里就在主配置文件中指定了,名称任意。咱们用的是虚拟主机,而虚拟主机里面也已经指定serverName了。
下面咱们所说的主机名都是表示FQDN而不是系统上的主机名。
ServerName www.star.com
[root@localhost httpd]# httpd -t Syntax OK [root@localhost httpd]# [root@localhost httpd]# systemctl reload httpd
还差一步,咱们是用主机名来区分不一样的虚拟主机的,若是用IP登陆只会解析到第一个虚拟主机上面。
因此在测试机的hosts的文件中加上IP与主机名的条目。linux系统在/etc/下面。 windows系统在C:\Windows\System32\drivers\etc下面。C表示系统盘。
windows上面若是不能保存,极可能是由于权限的问题, 在hosts属性里面的安全里面设置用户和权限。
我这里能够了,网页测试一下:
2、php
注意:在mysql上面127.0.0.1与localhost是不一样的。127.0.0.1会用网络套接字来通讯。而localhost用的才是 unix_sock,本地文件。若是要用localhost通讯,要在/etc/php.ini中为3个链接器指定mysql的sock文件所在位置。
mysql.default_socket = pdo_mysql.default_socket= mysqli.default_socket =
一、安装:
[root@localhost ~]# yum install php php-mysql -y
php-mysql是php链接mysql的链接器(驱动)。
[root@localhost ~]# rpm -ql php-mysql /etc/php.d/mysql.ini /etc/php.d/mysqli.ini /etc/php.d/pdo_mysql.ini /usr/lib64/php/modules/mysql.so /usr/lib64/php/modules/mysqli.so /usr/lib64/php/modules/pdo_mysql.so
mysql mysqli pdo_mysql 是三种不一样的链接器。 在之后编译php的时候要选择这三个的。
链接器介绍:http://php.net/manual/zh/mysqli.overview.php#mysqli.overview.mysqlnd
[root@localhost ~]# rpm -ql php /etc/httpd/conf.d/php.conf #关于php的设置 /etc/httpd/conf.modules.d/10-php.conf #httpd加载php模块 /usr/lib64/httpd/modules/libphp5.so #php模块 /usr/share/httpd/icons/php.gif /var/lib/php/session #存储session会话文件的目录 [root@localhost ~]#
二、测试:
rpm安装的php不用怎么配置
注意:rpm安装的php只支持prefork模型的httpd。若是要用work或event这种线程模型就要本身编译php了。
分别给两个虚拟主机测试文件。
[root@localhost ~]# vim /web/www/index.php <h1>www.star.com</h1> <?php phpinfo(); ?>
[root@localhost ~]# vim /web/myadm/index.php <h1>myadm.star.com</h1> <?php phpinfo(); ?>
测试:
[root@localhost ~]# httpd -t Syntax OK [root@localhost ~]# systemctl restart httpd
如今在不指定主页的状况下仍是优先找的index.html。能够在主配置文件中配置一下:
<IfModule dir_module> DirectoryIndex index.php index.html </IfModule>
在index.html前面加上index.php
三、xcache
xcache epel源中的rpm包可能有问题, 性能不升反降。不知道是什么问题。
要编译安装xcache。
安装xcache:
要先安装php-devel。 而后在xcache目录里面执行phpize,
php-config和phpize是php的配置接口,添加第三方的模块。
[root@localhost xcache-3.2.0]# yum install php-devel -y [root@localhost xcache-3.2.0]# phpize
编译很简单就两个选项,一个是启用xcache的扩功能, 一个是php-config,要指定php-config的位置。
[root@localhost xcache-3.2.0]# ./configure --enable-xcache --with-php-config=/usr/bin/php-config [root@localhost xcache-3.2.0]# make [root@localhost xcache-3.2.0]# make install [root@localhost xcache-3.2.0]# cp xcache.ini /etc/php.d/
安装完xcache的时候为提示xcache模块所在路径:
Installing shared extensions: /usr/lib64/php/modules/
若是php不能找到xcache模块,须要手动添加到xcache.ini文件中。
把安装xcache完成之后显示的那个路径写在extension指令后面,不过要指定xcache.so
如:extension = /usr/lib64/php/modules/xcache.so
[root@localhost xcache-3.2.0]# httpd -t Syntax OK [root@localhost conf.d]# systemctl reload httpd
网页测试一下:
这个info页面,在用ab测试的时候,不用xcache是600/sec。而用了之后反而变成了300/sec。 一下子再试试phpMyadmin的页面看看。
3、mysql
一、安装:
[root@localhost ~]# yum install mariadb-server -y [root@localhost ~]# systemctl start mariadb
进入mysql添加wordpress要用到的库和用户。
MariaDB [(none)]> CREATE DATABASE wordpress; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> GRANT ALL ON wordpress.* TO 'wpuser'@'172.16.40.30' IDENTIFIED BY 'abcdefg'; Query OK, 0 rows affected (0.00 sec)
给root用户添加密码,否则在phpMyadmin中不能登陆:
MariaDB [(none)]> UPDATE mysql.user SET password=PASSWORD('testpasswd') WHERE user='root'; Query OK, 4 rows affected (0.00 sec) Rows matched: 4 Changed: 4 Warnings: 0 MariaDB [(none)]> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec)
4、安装phpMyadmin和wordpress
https://files.phpmyadmin.net/phpMyAdmin/4.4.15.2/phpMyAdmin-4.4.15.2-all-languages.tar.xz
最新的4.5.3只有php5.5或以上版本才能够用。rpm安装的php版本是5.4.16.
https://cn.wordpress.org/wordpress-4.4.1-zh_CN.zip
删除/web/www和/web/myadmin里面的文件
分别把phpMyadmin和wordpress里面的文件放到/web/www和/web/myadmin里面。
我这里把wordpress放到了www里面。这是在/web/目录下面操做的。
[root@localhost web]# rm www/* -rf [root@localhost web]# mv wordpress/* www/ [root@localhost web]# rm myadm/* -rf [root@localhost web]# mv phpMyAdmin-4.4.15.2-all-languages/* myadm/
先来设置一下phpMyadmin。
[root@localhost web]# cd myadm [root@localhost myadm]# cp config.sample.inc.php config.inc.php [root@localhost myadm]# openssl rand -base64 15 eqt6V09NTIq4K8aILMz0 [root@localhost myadm]# vim config.inc.php
把生成的随机码放到里面。
上面这个地址把原来的localhost改为mysql数据库所在位置。
网页访问一下试试:
安装mbsting,多字符处理库。若是没有这个,phpMyAdmin不能支持汉字。
[root@localhost myadm]# yum install php-mbstring -y [root@localhost myadm]# systemctl reload httpd
而后就能够用root登陆了。
若是跟我这同样老是提示没法登陆,能够从新复制一下config.sample.inc.php文件试试。
ab测试一下看看。把/etc/php.d/xcache.ini文件移走。要reload啊。
我这是笔记本,速度太慢了。
Requests per second: 25.91 [#/sec] (mean)
再把文件放回去。
[root@localhost yum.repos.d]# ab -n 1000 -c 100 http://myadm.star.com/index.php ..... Requests per second: 125.05 [#/sec] (mean) ..... Connection Times (ms) min mean[+/-sd] median max Connect: 0 2 3.9 1 16 Processing: 49 769 170.4 783 1483 Waiting: 23 725 158.2 750 1288 Total: 51 771 167.8 783 1484 Percentage of the requests served within a certain time (ms) 50% 783 66% 815 75% 838 80% 852 90% 916 95% 999 98% 1093 99% 1173 100% 1484 (longest request)
安装wordpress:
进入www的目录。
[root@localhost www]# cp wp-config-sample.php wp-config.php [root@localhost www]# vim wp-config.php
测试:
我这里访问http://www.star.com/
5、给myadm虚拟主机提供https。
首先要创建私有CA。
我这里就用httpd这台主机来作CA了。这个CA是静态的,由openssl软件提供,不是服务程序,因此不会占用什么资源。
CA工做目录,是在/etc/pki/tls/openssl.cnf文件中设置的。默认是在/etc/pki/CA
/etc/pki/tls/openssl.cnf中关于CA的设置。
建立ca私钥。
[root@localhost CA]# pwd /etc/pki/CA [root@localhost CA]# (umask 077;openssl genrsa -out private/cakey.pem 4096) Generating RSA private key, 4096 bit long modulus ............................++ .........++ e is 65537 (0x10001) [root@localhost CA]# ll private/ total 4 -rw-------. 1 root root 3243 Jan 20 03:19 cakey.pem
CA自签证书:
[root@localhost CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem ..... Country Name (2 letter code) [XX]:CN State or Province Name (full name) []:BEIJING Locality Name (eg, city) [Default City]:BEIJING Organization Name (eg, company) [Default Company Ltd]:LinuxOPS Organizational Unit Name (eg, section) []:Ops Common Name (eg, your name or your server's hostname) []:ca.star.com Email Address []: [root@localhost CA]#
建立所必须的文件:
[root@localhost CA]# touch index.txt [root@localhost CA]# touch serial [root@localhost CA]# echo 00 > serial #这个是序列号文件,要指定序列号的起始
CA完成了,也就两步。剩下的就是为httpd建立私钥、生成证书申请而后签署就能够了。
我这里在/etc/httpd/目录下面创建一个ssl目录用以存储私钥和证书。这个目录任意,只要一下子在httpd配置文件中指定私钥和证书的路径就能够。
[root@localhost CA]# cd /etc/httpd [root@localhost httpd]# (umask 077;openssl genrsa -out ssl/myadm.key 2048) Generating RSA private key, 2048 bit long modulus .......................................................................................................................+++ .................+++ e is 65537 (0x10001) [root@localhost httpd]# openssl req -new -key ssl/myadm.key -out ssl/myadm.csr ..... Country Name (2 letter code) [XX]:CN State or Province Name (full name) []:BEIJING Locality Name (eg, city) [Default City]:BEIJING Organization Name (eg, company) [Default Company Ltd]:LinuxOPS Organizational Unit Name (eg, section) []:Ops Common Name (eg, your name or your server's hostname) []:myadm.star.com .....
这里的地区要同样,公司好像也要同样,域名能够不同。
上面生成的是证书申请,如今咱们来签署。
[root@localhost httpd]# openssl ca -in /etc/httpd/ssl/myadm.csr -out /etc/httpd/ssl/myadm.crt ..... Certificate is to be certified until Jan 18 19:48:15 2017 GMT (365 days) Sign the certificate? [y/n]:y 1 out of 1 certificate requests certified, commit? [y/n]y Write out database with 1 new entries Data Base Updated [root@localhost httpd]#
[root@localhost httpd]# ll ssl total 16 -rw-r--r--. 1 root root 5718 Jan 20 03:48 myadm.crt -rw-r--r--. 1 root root 1009 Jan 20 03:32 myadm.csr -rw-------. 1 root root 1679 Jan 20 03:31 myadm.key [root@localhost httpd]#
注意:CA与httpd是没有关系的,虽然都使用openssl命令。 若是CA在另外一台主机上面,咱们也只是要把csr文件发到CA的主机,CA签署完之后再发回来而已。
还有,由于咱们是使用的私有CA。客户端的主机上面没有CA的根证书,因此要把CA的自签证书发给客户端。就是/etc/pki/CA/cacert.pem。
一些公有CA不用这样作的缘由是由于系统或浏览器已经自带那些CA的根证书了。
既然是私有CA,系统或浏览器上确定是没有了。这么作的目的,是让浏览器信任由此CA所颁发的证书。
我这里复制到个人主机上面了,windows的系统。
火狐浏览器是在选项--高级--证书--查看证书--证书机构--导入,找到证书文件导入便可。
Chrome浏览器在设置--显示高级设置--管理证书--受信任的根证书颁发机构--导入便可。
设置httpd支持ssl
myadm.csr文件如今没有用了,能够删除。
安装ssl模块。
[root@localhost httpd]# yum install mod_ssl -y [root@localhost httpd]# rpm -ql mod_ssl /etc/httpd/conf.d/ssl.conf #ssl的设置文件 /etc/httpd/conf.modules.d/00-ssl.conf #加载ssl模块的。 /usr/lib64/httpd/modules/mod_ssl.so #ssl模块 /usr/libexec/httpd-ssl-pass-dialog /var/cache/httpd/ssl [root@localhost httpd]#
ssl是基于IP地址的,在单IP的主机上面只能用一个ssl的链接。而客户端访问的地址必定要是证书里面显示的主机名。咱们在服务器上要作的就是要正确的把这个请求解析到正确的虚拟主机。
https的端口是443。
如今在conf.d/ssl.conf配置文件中除了通常ssl的设置之外,还有一个443端口的虚拟主机。而vhosts文件
中的phpMyadmin虚拟主机要用ssl也要以443端口。因此我这里就把ssl.conf文件中带的虚拟主机给注释了。 并把所须要的指令放到vhosts文件中去。
如今的vhosts配置文件。
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" %{SSL_PROTOCOL}x %{SSL_CIPHER}x " custom1 #自定义日志格式,加入了显示ssl协议版本和加密协议。 <VirtualHost *:80> ServerName www.star.com DocumentRoot /web/www CustomLog "/var/log/httpd/www/access_log" combined ErrorLog "/var/log/httpd/www/error_log" <Directory "/web/www"> Options None Require all granted </Directory> </VirtualHost> <VirtualHost *:443> ServerName myadm.star.com DocumentRoot /web/myadm CustomLog "/var/log/httpd/myadm/access_log" custom1 #使用新定义的日志格式 ErrorLog "/var/log/httpd/myadm/error_log" SSLEngine on #启用ssl引擎 SSLProtocol all -SSLv2 #使用全部ssl协议版本,除了SSLV2版本。 SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5 #这个也能够不加,我直接复制过来了。 SSLCertificateFile /etc/httpd/ssl/myadm.crt #证书路径 SSLCertificateKeyFile /etc/httpd/ssl/myadm.key #私钥路径 <Directory "web/myadm"> Options None Require all granted </Directory> </VirtualHost>
虚拟主机若是非要有优先级的话:IP>端口>FQDN。
因此这里有个有意思的地方,访问https://www.star.com/会怎么样。443端口,会优先使用端口的虚拟主机,因此会访问phpMyadmin的虚拟主机。可是访问名称与证书中的主机名不一样。因此:
而www.star.com是没有问题的。