项目实战:
搭建LNMP环境:Linux+Nginx+Mysql(MariaDB)+php(php-fpm),创建多个虚拟主机:
主机1提供正常的http服务,用于安装wordpress博客;
主机2提供正常的https服务,用于安装phpMyAdmin,用图形界面去管理MySQL(MariaDB)数据库;
项目实战步骤:
1.安装nginx:
(1)使用已经下载好的nginx的rpm包进行安装:
CentOS6:yum install -y nginx-1.6.2-1.el6.ngx.x86_64.rpm
CentOS7:yum install -y nginx-1.10.0-1.el7.ngx.x86_64.rpm
(2)使用阿里云的epel源进行直接安装
CentOS6:http://mirrors.aliyun.com/epel/6/x86_64
CentOS7:http://mirrors.aliyun.com/epel/7/x86_64
yum -y install nginx
2.安装数据库:
CentOS6:yum -y install mysql mysql-server
CentOS7:yum -y install mariadb mariadb-server
vim /etc/my.cnf
[mysqld]
skip_name_resolve=ON
innodb_file_per_table=ON
mysql_secure_installation(启动服务之后,进行初始化mysql数据库)
3.安装php的插件:
yum install -y php-fpm php-mysql php-gd php-mcrypt php-mbstring
4.启动服务:
CentOS6:
service nginx start
service mysqld start
service php-fpm start
CentOS7:
systemctl start nginx.service
systemctl start mariadb.service
systemctl start php-fpm.service
5.关闭防火墙和SELinux:
iptables -F
setenforce 0
6.http配置wordpress;
(1)配置php-fpm:
vim /etc/php-fpm.d/www.conf
将里面的user和group 由apache更改为nginx
创建/var/lib/php/session并将其属主属组更改为nginx
mkdir /var/lib/nginx/session
chown -R nginx:nginx /var/lib/nginx/session
systemctl restart php-fpm.service
(2)配置php-fpm的虚拟主机:
将默认的default.conf进行备份:
mv /etc/nginx/conf.d/default.conf{,.bak}
新创建虚拟nginx主机的配置文件:
vim /etc/nginx/conf.d/wordpress.conf
server {
listen 80;
server_name www.sjsir.com;
location / {
root /var/www/html;
index index.php index.html index.htm;
}
location ~* \.(php)$ {
root /var/www/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
include fastcgi_params;
}
}
(3)搭建wordpress;(wordpress-4.6.1.zip设置界面为英文版)
cd /var/www/html
rz wordpress-4.6.1.zip
unzip wordpress-4.6.1.zip
rm -rf wordpress-4.6.1.zip
mv wordpress/* .
(4)为wordpress创建mysql数据库的数据;
#mysql -uroot -p(root用户的mysql密码)
mysql>CREATE DATABASE wordpress;
mysql>GRANT ALL ON wordpress.* TO 'wordpress'@'127.0.0.1' IDENTIFIED BY 'wordpresspass';
mysql>exit
(5)用浏览器访问192.168.1.104:
点击Let's go!
按照之前设置的信息,将下面修改正确,之后Submit;
将上图蓝色中的内容复制下来,并创建后粘贴到/var/www/html/wp-config.php
vim /var/www/html/wp-config.php
粘贴到此处
回到浏览器点击Run the install
按照下图填入必要信息:
然后会出现以下页面,表示安装成功;
登录wordpress,会发现英文版的wordpress-4.6.1已经安装成功:
7.https配置pma:
(1)配置pma的虚拟主机:
server {
listen 443 ssl;
server_name www.sjsir.wang;
ssl on;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
ssl_session_cache shared:sslcache:20m;
location / {
root /var/www/pma;
index index.php index.html index.htm;
}
location ~* \.(php)$ {
root /var/www/pma;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/pma$fastcgi_script_name;
include fastcgi_params;
}
}
(2)配置https:
cd /etc/pki/CA/
(umask 077;openssl -out private/cakey.pem 4096)
openssl genrsa -new -x509 private/cakey.pem -out cecurt.pem
CN Beijing Beijing Sjsir Ops ca.sjsir.cn
touch index.txt
echo 01 > serial
cd /etc/nginx
mkdir ssl
cd ssl
(umask 077;openssl genrsa -out nginx.key 4096)
openssl req -new -key nginx.key -out nginx.csr
CN Beijing Beijing Sjsir Ops ca.sjsir.cn
openssl ca -in nginx.csr -out nginx.crt
(3)配置pma:
mkdir /var/www/pma
cd /var/www/pma
rz phpMyAdmin-4.0.5-all-languages.zip
unzip phpMyAdmin-4.0.5-all-languages.zip
mv phpMyAdmin/* .
rm -rf phpMyAdmin/
mv config.sample.inc.php config.inc.php
(4)测试:https://192.168.1.104
就可以看到:
输入账号和密码登录进去,就可以看到mariadb数据库:
注意:
(1)在实验之前一定要关闭防火墙和SELinux,以免系统因为iptables和SELinux的设置而连接不上;
(2)配置pma时,一定要配置php的fastcgi模块,或单独的php模块,不配置的话,用php编写的pma就无法使用;
(3)配置pma时,一定要安装php-mcrypt模块,该模块位于epel源中(以阿里云的7系列的epel源:http://mirrors.aliyun.com/epel/7/x86_64)。
(4)搭建该实验环境时,应该注意参考日志文件,以确定发生什么样的问题,/var/log/messages /var/log/nginx/error.log /var/log/nginx/access.log
当测试时,发现无法连接服务器,例如500的错误,需要查看/var/log/nginx/error.log
当测试时,发现页面能打开,但是不正常显示,而且/var/log/nginx/error.log日志无日志记录时,一定要去查看/var/log/nginx/access.log日志信息。有时服务器已经响应了,但是可能由于wordpress或pma的安装包配置的问题而报错,error.log不会存储这些已经响应的日志,需要去查看access.log。
原创文章,作者:sjsir,如若转载,请注明出处:http://www.178linux.com/56083