Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Deploy Nginx+Apache to separate movement and movement (actual combat! You can do it! )

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

The static and static separation of Nginx introduces that the static processing capacity of 1.Nginx is very strong, but the dynamic processing is insufficient, so the static and static separation technology is commonly used in enterprises. Static and dynamic separation for PHP

Static pages are handed over to Nginx for processing

Dynamic pages are handed over to PHP+FPM module or Apache for processing

3. In the configuration of Nginx, the reverse proxy principle of different processing methods of static and dynamic pages is realized through location configuration segment combined with regular matching.

1.Nginx not only acts as a Web server, but also has the functions of reverse proxy, load balancing and caching.

2.Nginx uses the proxy module to proxy the client's request to the upstream server, where nginx and. The upstream server is connected through the http protocol

The most important instruction of 3.Nginx when implementing the reverse proxy function is proxy_pass, which can and can schedule user requests to the upstream server according to URI, client parameters or other processing logic.

Configure Nginx to realize static and dynamic separation

1. In this case, according to the needs of the enterprise, the static and dynamic separation is realized by configuring Nginx, and the request for the php page is forwarded to LAMP for processing, while the static page is handed over to Nginx for processing to achieve static and dynamic separation.

two。 The architecture is shown in the figure:

Configuration steps:

1. Configure Nginx to handle dynamic page requests and add them to server {};

two。 In Apache. New test.php for working directory

3. Restart Nginx and test

[root@nginx php5] # vim / usr/local/httpd/conf/nginx.conf server {. Location ~\ .php$ {proxy_pass http://192.168.9.237:8080;...... / / IP address of LAMP Demo: environment preparation: two CentOS 7s, of which 7-3 is used as lamp 7-4 as the first step of nginx: install httpd [root @ localhost ~] # yum install httpd httpd-devel-y [root@localhost ~] # firewall-cmd-- permanent-- zone=public-- add-service=httpsuccess [root@localhost ~] # firewall-cmd-- permanent-- zone=public-- add-service=httpssuccess [root@localhost ~] # firewall-cmd-- reloadsuccess [root@localhost ~] # systemctl start httpd at this time, you can use the host to access Apache's home page as shown below:

Step 2: install mariadb database (fast and lightweight database) [root@lamp ~] # yum install mariadb mariadb-server mariadb-libs mariadb-devel-y [root@lamp ~] # systemctl start mariadb [root@lamp ~] # systemctl start mariadb [root@lamp ~] # netstat-ntap | grep 3306tcp 000.0.0.00,306 0.0.0.0yum install mariadb mariadb-server mariadb-libs mariadb-devel * LISTEN 16836/mysqld [root@lamp ~] # mysql_secure_installation / / set NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE to the database! PLEASE READ EACH STEP CAREFULLY!In order to log into MariaDB to secure it, we'll need the currentpassword for the root user. If you've just installed MariaDB, andyou 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): / / set a password for the root administrator, and directly enter OK, successfully used password, moving on...Setting the root password ensures that nobody can log into the MariaDBroot user without the proper authorisation.Set root password? Select yesNew password: / / enter the new password Re-enter new password: / / re-enter the new password Password updated fulfilled Reloading privilege tables.. ... Success!By default, a MariaDB installation has an anonymous user, allowing anyoneto log into MariaDB without having to have a user account created forthem. This is intended only for testing, and to make the installationgo a bit smoother. You should remove them before moving into aproduction environment.Remove anonymous users? [YBO] n / / whether to delete anonymous users or not, select no... Skipping.Normally, root should only be allowed to connect from 'localhost'. Thisensures that someone cannot guess at the root password from the network.Disallow root login remotely? [Yopin] n / / whether to deny remote login to root users, select no. Skipping.By default, MariaDB comes with a database named 'test' that anyone canaccess. This is also intended only for testing, and should be removedbefore moving into a production environment.Remove test database and access to it? [YBO] n / / whether to delete the test database, select no. Skipping.Reloading the privilege tables will ensure that all changes made so farwill take effect immediately.Reload privilege tables now? [Yopin] y / / whether to load the permission list, select yes... Success!Cleaning up...All done! If you've completed all of the above steps, your MariaDBinstallation should now be secure.Thanks for using MariaDB! Step 3: install php [root @ lamp ~] # yum install php- y [root@lamp ~] # yum install php-mysql-y [root@lamp ~] # yum install-y php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel php-bcmath [root@lamp ~] # cd / var/www/html/ [root@lamp html] # ls [root@lamp html] # vim index.php input: wq save exit [root@lamp html] # systemctl restart httpd.service enter the address: http://192.168.18.128/index.php in the host browser at this time You can visit lamp's php home page.

Test preparation: [root@lamp html] # vim index.php input: wq saves and exits and the content displayed on the http://192.168.18.128/index.php address is as follows:

Operation on Nginx: [root@nginx ~] # mkdir / aaa [root@nginx ~] # mount.cifs / / 192.168.10.193/rpm / aaaPassword for root@//192.168.10.193/rpm: [root@nginx ~] # cd / aaa [root@nginx aaa] # lsapr-1.6.2.tar.gz error.png nginx-1.12.2.tar.gzapr-util-1.6. 0.tar.gz httpd-2.4.29.tar.bz2 php-7.1.10.tar.bz2awstats-7.6.tar.gz lf.jpg php-7.1.20.tar.gzcronolog-1.6.2-14.el7.x86_64.rpm mysql-5.6.26.tar.gzDiscuz_X3.4_SC_UTF8.zip mysql-boost -5.7.20.tar.gz [root@nginx aaa] # tar zxvf nginx-1.12.2.tar.gz-C / opt/ [root@nginx aaa] # cd / opt [root@nginx opt] # lsnginx-1.12.2 rh [root@nginx opt] # cd nginx-1.12.2/ [root@nginx nginx-1.12.2] # useradd-M-s / sbin/nologin nginx [root@nginx nginx-1.12.2] # yum install gcc gcc-c++ pcre -devel zlib-devel-y [root@nginx nginx-1.12.2] #. / configure\-- prefix=/usr/local/nginx\-- user=nginx\-- group=nginx\-with-http_stub_status_ module [root @ nginx nginx-1.12.2] # make & & make install [root@nginx nginx-1.12.2] # ln-s / usr/local/nginx/sbin/nginx / usr/local/sbin/ [root@nginx nginx-1.12.2] # vim / Chkconfig:-99 2 percent description: Nginx Service Control ScriptPROG= "/ usr/local/nginx/sbin/nginx" PIDF= / usr/local/nginx/logs/nginx.pid "case" $1 "in start) $PROG ; stop) kill-s QUIT $(cat $PIDF);; restart) $0 stop $0 start;; reload) kill-s HUP $(cat $PIDF) *) echo "Usage: $0 {start | stop | restart | reload}" exit 1 esac exit 0 input: wq save exit [root@nginx nginx-1.12.2] # chmod + x / etc/init.d/nginx [root@nginx nginx-1.12.2] # chkconfig-add nginx [root@nginx nginx-1.12.2] # yum install elinks-y [root@nginx nginx-1.12.2] # service nginx start [root@nginx Nginx-1.12.2] # netstat-ntap | grep 80tcp 0 0 0. 0 0. 0. 0. 0. 0. 0. 0. 0 of the elinks * LISTEN 42028/nginx: master [root@nginx nginx-1.12.2] # systemctl stop firewalld.service [root@nginx nginx-1.12.2] # setenforce 0root@nginx nginx-1.12.2] # elinks http://192.168.18.136/ gets the following interface Press Q, select yes, and press enter to exit.

At this point, enter the URL http://192.168.18.136/index.html in the host, and you will get the following interface

At this point, if you enter: http://192.168.18.136/index.php, it cannot be processed, and you will get the following interface:

Step 4: do forwarding processing: [root@nginx nginx-1.12.2] # vim / usr/local/nginx/conf/nginx.conflocation ~\ .php$ {proxy_pass http://192.168.18.128; } # the above means: who will transfer the dynamic request to handle # We find the above and remove the previous comments And change the IP address to another 7-3 address input: wq save exit [root@nginx nginx-1.12.2] # service nginx stop [root@nginx nginx-1.12.2] # service nginx start enter: http://192.168.18.136/index.php this URL can get the following interface: because here to Apache to deal with the php request

Conclusion: the suffix html is a static element, and the suffix php is a dynamic element. The address does not need to be changed because we have done forwarding processing in it! The static and dynamic separation experiment was successful!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report