CentOS 7.1.1053 搭建不同网段LNAMP

前端之家收集整理的这篇文章主要介绍了CentOS 7.1.1053 搭建不同网段LNAMP前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

前言

<> 非常感谢Kason老师给予的帮助!

> 马哥教到布署LNAMP时,我遇到了一些问题: * Nginx是代理,不需要与后端直接建立连接; * 我开始使用跨网段没有实现成功; * 后来我使用同网段的方法,但访问时就会跳转到后端,查看后端日志时,其访问者也是客户端IP;

>> 总结: >> * 对windows虚拟机中的vmnet#不了解; >>* wordpress程序定其只能根据IP访问;


windows上基于虚拟主机实现LNAMP



配置前提

  • 关闭防火墙、SELinux,如果要启动,则在所有配置成功以后,再行启动;
  • 同步时间;ntp,chrony
  • EPEL源和Base源;PHP-mbstring,PHP-mcrypt包依赖EPEL;
  1. # iptables -F
  2. # setenforce 0
  3. //Nginx主机上配置时间服务器同步时间
  4. # echo "allow 192.168.10/24" &gt;&gt; /etc/chrony.conf
  5. # systemctl start chronyd.service
  6. # systemctl enable chronyd.service
  7. # netstat -unlp
  8. Active Internet connections (only servers)
  9. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  10. udp 0 0 0.0.0.0:123 0.0.0.0:* 784/chronyd
  11. udp 0 0 127.0.0.1:323 0.0.0.0:* 784/chronyd
  12. udp6 0 0 :::123 :::* 784/chronyd
  13. udp6 0 0 ::1:323 :::* 784/chronyd
  14. # ntpdate 192.168.10.254
  15. 31 Dec 10:04:02 ntpdate[13649]: adjust time server 192.168.10.254 offset 0.181638 sec

Nginx主机172.16.0.6上配置

> * 安装程序包

  1. ~]# yum -y install Nginx

>* 启用Nginx

  1. ~]# systemctl start Nginx.service

>* 查看端口是否监听

  1. ~]# netstat -tnlp
  2. Active Internet connections (only servers)
  3. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  4. tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3660/Nginx: master
  5. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1029/sshd
  6. tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1917/master
  7. tcp6 0 0 :::80 :::* LISTEN 3660/Nginx: master
  8. tcp6 0 0 :::22 :::* LISTEN 1029/sshd
  9. tcp6 0 0 ::1:25 :::* LISTEN 1917/master

>* 查看Nginx进程

  1. ~]# ps axu
  2. root 3660 0.0 0.7 123456 5240 ? Ss 09:29 0:00 Nginx: master process /usr/sbin/Nginx
  3. Nginx 3674 0.0 0.5 125872 4168 ? S 09:35 0:00 Nginx: worker process

>* 配置反代

  1. ~]# vim /etc/Nginx/conf.d/ilinux.conf
  2. server {
  3. listen 80;
  4. server_name www.ilinux.io;
  5. location / {
  6. proxy_pass http://192.168.10.11:80;
  7. proxy_set_header X-Real-IP $remote_addr;
  8. add_header X-Via $server_addr;
  9. }
  10. }

>* 测试语法

  1. ~]# Nginx -t
  2. Nginx: the configuration file /etc/Nginx/Nginx.conf Syntax is ok
  3. Nginx: configuration file /etc/Nginx/Nginx.conf test is successful

>* 重载服务

  1. ~]# Nginx -s reload

在LAMP主机 192.168.10.11上配置

>* 安装程序包:httpd PHP PHP-MysqL mariadb-server PHP-mbstring PHP-mcrypt

  1. ~]# yum -y install httpd PHP PHP-MysqL maraidb-server PHP-mbstring PHP-mcrypt

>* 启动httpd

  1. ~]# systemctl start httpd.service

>* 查看端口是否端口

  1. ~]# netstat -tnlp
  2. Active Internet connections (only servers)
  3. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  4. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1366/sshd
  5. tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2506/master
  6. tcp6 0 0 :::80 :::* LISTEN 13442/httpd
  7. tcp6 0 0 :::22 :::* LISTEN 1366/sshd
  8. tcp6 0 0 ::1:25 :::* LISTEN 2506/master

>* 查看进程

  1. ~]# ps axu
  2. root 13442 0.0 1.3 396676 13128 ? Ss 09:45 0:00 /usr/sbin/httpd -DFOREGROUND
  3. apache 13444 0.0 0.6 398760 6872 ? S 09:45 0:00 /usr/sbin/httpd -DFOREGROUND
  4. apache 13445 0.0 0.6 398760 6872 ? S 09:45 0:00 /usr/sbin/httpd -DFOREGROUND
  5. apache 13446 0.0 0.6 398760 6872 ? S 09:45 0:00 /usr/sbin/httpd -DFOREGROUND
  6. apache 13447 0.0 0.6 398760 6872 ? S 09:45 0:00 /usr/sbin/httpd -DFOREGROUND
  7. apache 13448 0.0 0.6 398760 6872 ? S 09:45 0:00 /usr/sbin/httpd -DFOREGROUND

>* 准备测试文件 1.准备Index.html

  1. ~]# vim /var/www/html/index.html
  2. &lt;h1&gt;192.168.10.11&lt;/h1&gt;
  3. http://192.168.10.11/
  4. http://www.ilinux.io/

2.准备PHPinfo.PHP

  1. ~]# vim /var/www/html/PHPinfo.PHP
  2. &lt;html&gt;
  3. &lt;title&gt;Test Page&lt;/title&gt;
  4. &lt;body&gt;
  5. &lt;h1&gt;192.168.10.11&lt;/h1&gt;
  6. &lt;?PHP
  7. PHPinfo();
  8. ?&gt;
  9. &lt;/body&gt;
  10. &lt;/html&gt;
  11. http://192.168.10.11/PHPinfo.PHP
  12. http://www.ilinux.io/PHPinfo.PHP

3.准备PHP-MysqL.PHP

  1. ~]# vim /var/www/html/PHP-MysqL.PHP
  2. &lt;?PHP
  3. $conn = MysqL_connect('192.168.10.11','wpuser','wppass');
  4. if ($conn)
  5. echo "connect 192.168.10.11 success";
  6. else
  7. echo "connect 192.168.10.11 failure";
  8. ?&gt;
  9. http://192.168.10.11/PHP-MysqL.PHP
  10. http://www.ilinux.io/PHP-MysqL.PHP

4.准备wordpress

  1. [root@localhost html]# pwd
  2. /var/www/html
  3. [root@localhost html]# ls
  4. index.html PHPinfo.PHP PHP-MysqL.PHP wordpress-4.9.1-zh_CN.tar.gz
  5. [root@localhost html]# tar xf wordpress-4.9.1-zh_CN.tar.gz
  6. [root@localhost html]# ln -sv wordpress wp
  7. wp -&gt; wordpress
  8. [root@localhost html]# ll
  9. total 9912
  10. -rw-r--r-- 1 root root 20 Dec 18 14:04 index.html
  11. -rw-r--r-- 1 root root 109 Dec 18 14:04 PHPinfo.PHP
  12. -rw-r--r-- 1 root root 157 Dec 18 14:16 PHP-MysqL.PHP
  13. drwxr-xr-x 1 nobody nfsnobody 498 Nov 30 20:20 wordpress
  14. -rw-r--r-- 1 root root 10130710 Dec 1 18:57 wordpress-4.9.1-zh_CN.tar.gz
  15. lrwxrwxrwx 1 root root 9 Dec 18 14:21 wp -&gt; wordpress
  16. [root@localhost html]# cp wp/wp-config-sample.PHP wp/wp-config.php
  17. [root@localhost html]# vim wp/wp-config.php
  18. /** wordpress数据库名称 */
  19. define('DB_NAME','wordpress');
  20. /** MysqL数据库用户名 */
  21. define('DB_USER','wpuser');
  22. /** MysqL数据库密码 */
  23. define('DB_PASSWORD','wppass');
  24. /** MysqL主机 */
  25. define('DB_HOST','192.168.10.11');

>* 配置mariadb

  1. # vim /etc/my.cnf
  2. [MysqLd]
  3. skip_name_resolve=ON
  4. innodb_file_per_table=ON

>* 启动mariadb-server

  1. ~]# systemctl start mariadb.service
  2. # netstat -tunlp
  3. Active Internet connections (only servers)
  4. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  5. tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 13915/MysqLd
  6. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1366/sshd
  7. tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2506/master
  8. tcp6 0 0 :::80 :::* LISTEN 13442/httpd
  9. tcp6 0 0 :::22 :::* LISTEN 1366/sshd
  10. tcp6 0 0 ::1:25 :::* LISTEN 2506/master

>* 安全初始化

  1. # MysqL_secure_installation

>* 授权用户

  1. ~]# msyql -uroot -h localhost -pmagedu
  2. MariaDB [(none)]&gt; CREATE DATABASE wordpress;
  3. Query OK,1 row affected (0.00 sec)
  4. MariaDB [(none)]&gt; GRANT ALL ON wordpress.* TO 'wpuser'@'%' IDENTIFIED BY 'wppass';
  5. Query OK,0 rows affected (0.00 sec)
  6. MariaDB [(none)]&gt; FLUSH PRIVILEGES;
  7. Query OK,0 rows affected (0.00 sec)

>* 测试访问

  1. ~]# MysqL -uwpuser -h192.168.10.11 -pwppass
  2. MariaDB [(none)]&gt;

配置wordpress

>* 访问http://www.ilinux.io/wp,wfc 会自动 跳转如下 >* 此时输入如下,点击安装 >* 看见此页面时,在浏览器器中输入www.ilinux.io/wp/wp-login.PHP >* 输入刚才设定的账号和密码,点击登陆 >* 查看后端服务器的日志是否是Nginx代理?

  1. ~]# ifconfig eno16777736 | awk 'NR == 2{print}'
  2. inet 192.168.10.11 netmask 255.255.255.0 broadcast 192.168.10.255
  3. ~]# tail -n 10 /var/log/httpd/access_log
  4. 192.168.10.1 - - [31/Dec/2017:10:27:08 +0800] "POST /wp/wp-admin/admin-ajax.PHP HTTP/1.1" 200 47 "http://192.168.10.11/wp/wp-admin/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/62.0.3202.94 Safari/537.36"
  5. /*这个是vmnet1,访问本机的vmnet1是vmnet1虚拟网卡的地址
  6. 并非是192.168.10.254,即Nginx并没有反代
  7. */

>* 点击设置 >* 修改如下URL中的IP为Nginx主机的FQDN,点击'保存更改' >* 此时清空缓存

>* 查看日志

  1. ~]# tail -n 2 /var/log/httpd/access_log
  2. 192.168.10.254 - - [31/Dec/2017:10:45:44 +0800] "POST /wp/wp-admin/admin-ajax.PHP HTTP/1.0" 200 23 "http://www.ilinux.io/wp/wp-admin/options-general.PHP?settings-updated=true" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/62.0.3202.94 Safari/537.36"
  3. 192.168.10.254 - - [31/Dec/2017:10:46:13 +0800] "POST /wp/wp-admin/admin-ajax.PHP HTTP/1.0" 200 47 "http://www.ilinux.io/wp/wp-admin/options-general.PHP?settings-updated=true" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/62.0.3202.94 Safari/537.36"
  4. ~}# grep "LogFormat" /etc/httpd/conf/httpd.conf
  5. LogFormat "%{X-Real-IP}i %l %u %t \"%r\" %&gt;s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

>* ww.ilinux.io,意此时在172.16.0.0/16网段内只要可以访问Nginx的主机均可以访问访问此wordpress

猜你在找的CentOS相关文章