您好,登錄后才能下訂單哦!
本篇內容主要講解“Nginx+Apache動靜分離部署過程”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Nginx+Apache動靜分離部署過程”吧!
之前在講解基于LNMP架構的Discuz論壇搭建(原文鏈接:https://blog.51cto.com/14557673/2461480 )的時候對動靜分離有所提及,這邊簡述一下核心原因:
根據Nginx服務的特性,其擅長處理靜態網站(圖片文字視頻等文件)訪問資源,而Apache擅長動態處理(例如:賬號注冊的交互)。
因此我們可以結合這兩個服務特點與優勢,部署實現網站服務的動靜分離。
實驗環境:兩臺Centos7虛擬機,一臺為LAMP架構,另一臺為nginx服務
首先我們需要搭建LAMP架構,這次我們使用yum直接進行搭建LAMP,具體步驟如下:
===================LAMP簡易版搭建==================
1.安裝httpd
yum install -y httpd httpd-devel
systemctl start httpd.service
[root@lamp ~]# ifconfig ens33 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.68.144 netmask 255.255.255.0 broadcast 192.168.68.255 inet6 fe80::7330:498c:44ce:c5f7 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:cc:52:c8 txqueuelen 1000 (Ethernet) RX packets 659954 bytes 964992071 (920.2 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 339462 bytes 20930426 (19.9 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@lamp ~]# yum install -y httpd httpd-devel [root@lamp ~]# systemctl start httpd.service [root@lamp ~]# netstat -antp | grep httpd tcp6 0 0 :::80 :::* LISTEN 57584/httpd
2.防火墻設置(也可以直接關閉)
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload
[root@lamp ~]# firewall-cmd --permanent --zone=public --add-service=http success [root@lamp ~]# firewall-cmd --permanent --zone=public --add-service=https success [root@lamp ~]# firewall-cmd --reload success
3.安裝mariadb 數據庫
yum install mariadb mariadb-server mariadb-libs mariadb-devel -y
mariadb 快速簡單輕量的快捷數據庫
[root@lamp ~]# yum install mariadb mariadb-server mariadb-libs mariadb-devel -y
4.啟動服務
systemctl start mariadb
[root@lamp ~]# systemctl start mariadb.service [root@lamp ~]# netstat -antp | grep 3306 tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 67480/mysqld
5.執行mysql安全配置向導命令
mysql_secure_installation
[root@lamp ~]# mysql_secure_installation //需要交互 NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none): //沒有密碼。直接回車 OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation. Set root password? [Y/n] y //設置root密碼自己輸入 New password: Re-enter new password: Password updated successfully! Reloading privilege tables.. ... Success! By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB 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. Remove anonymous users? [Y/n] n //選擇是否移除匿名用戶,自己選擇 ... skipping. Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] n //是否不允許root遠程登錄 ... skipping. By default, MariaDB 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. Remove test database and access to it? [Y/n] n //是否移除測試數據庫 ... skipping. Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] y //重載刷新 ... Success! Cleaning up... All done! If you've completed all of the above steps, your MariaDB installation should now be secure. Thanks for using MariaDB!
6.安裝PHP
yum -y install php
[root@lamp ~]# yum -y install php
7.安裝PHP與mysql關聯包
yum install php-mysql -y
[root@lamp ~]# yum install php-mysql -y
8.安裝php插件
yum install -y php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap
[root@lamp ~]# yum install -y php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap
9.創建PHP網頁內容
cd /var/www/html
vim index.php
<?php
phpinfo();
?>
[root@lamp html]# cd /var/www/html/ [root@lamp html]# vim index.php [root@lamp html]# cat index.php <?php phpinfo(); ?>
10.重啟httpd服務
systemctl restart httpd
[root@lamp html]# systemctl restart httpd.service [root@lamp ~]# netstat -antp | grep httpd tcp6 0 0 :::80 :::* LISTEN 57584/httpd
11.測試架構是否搭建成功
=====================nginx服務器配置============================
手工編譯安裝Nginx服務
1.安裝環境包
yum -y install gcc gcc-c++ pcre-devel zlib-devel
[root@nginx ~]# ifconfig ens33 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.68.136 netmask 255.255.255.0 broadcast 192.168.68.255 inet6 fe80::f14b:5f19:2889:b137 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:f3:5e:0b txqueuelen 1000 (Ethernet) RX packets 69672 bytes 100983394 (96.3 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 34582 bytes 2297096 (2.1 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@nginx ~]# yum -y install gcc gcc-c++ pcre-devel zlib-devel
2.解壓縮軟件包
tar zxf nginx-1.12.2 tar.gz -C /opt/
[root@nginx LNMP-C7]# tar zxf nginx-1.12.2.tar.gz -C /opt/ [root@nginx LNMP-C7]# cd /opt/nginx-1.12.2/ [root@nginx nginx-1.12.2]# ls auto CHANGES.ru configure html man src CHANGES conf contrib LICENSE README
3.創建家目錄但不創建家目錄
useradd -M -s /sbin/nologin nginx
[root@nginx nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
4.配置相關參數
cd /opt/nginx-1.12.0
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
[root@nginx nginx-1.12.2]# ./configure \ > --prefix=/usr/local/nginx \ > --user=nginx \ > --group=nginx \ > --with-http_stub_status_module
5.編譯安裝
make &&make install
[root@nginx nginx-1.12.2]# make && make install
6.創建軟鏈接
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@nginx nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
7.配置文件設置
vim /usr/local/nginx/conf/nginx.conf
location ~ .php$ {
proxy_pass http://192.168.68.144;
}
[root@nginx nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ [root@nginx nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf [root@nginx nginx-1.12.2]# sed -n '59,61p' /usr/local/nginx/conf/nginx.conf location ~ \.php$ { proxy_pass http://192.168.68.144; }
8.開啟nginx服務
nginx
[root@nginx nginx-1.12.2]# nginx [root@nginx nginx-1.12.2]# netstat -antp | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 81215/nginx: master
9.關閉防火墻
systemctl stop firewalld
setenforce 0
[root@nginx nginx-1.12.2]# systemctl stop firewalld.service [root@nginx nginx-1.12.2]# setenforce 1
10.測試驗證是否通過nginx服務器訪問php網頁
到此,相信大家對“Nginx+Apache動靜分離部署過程”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。