首页 > 编程知识 正文

apache配置文件详解,nginx为什么比apache快

时间:2023-05-05 01:28:49 阅读:28338 作者:164

Apache和nginx动静分离文章目录Apache和nginx动静分离前言实验环境实验分析实验步骤1 .构建1.LAMP体系结构2 .构建nginx服务器3 .动静分离实验总结

前言

在以前的几个博客中,我们发现Apache对动态页面的处理能力非常高,但对静态页面的处理能力不是很高。 nginx具有非常高的静态页面处理能力和高并发能力,但动态页面的处理能力不如Apache高。 因此,在线网络中,公司普遍使用动静分离技术来提高公司的业务处理。

实验环境使用两台centos7虚拟机作为服务器

作为客户端的窗口S10主机

实验分析基于LAMP架构和LNMP架构,分为LAMP架构、LNMP架构、动静分离三大步骤

LMP服务器主要是nginx,不能使用MySQL和PHP,因此可以简化一些步骤

LAMP体系结构、nginx服务器、动态和静态隔离

实验步骤1 .构建LAMP体系结构1.1将服务1重命名为LAMP,指示此服务用于构建LAMP服务

[ root @ web~] # hostnamectlset-hostname lamp [ root @ web~~ ] # su [ root @ lamp~] # 1.2安装lamp体系结构的Apache服务

[ root @ lamp~] # yum-yinstallhttpdhttpd-devel [ root @ lamp~] # rpm-qhttpdhttpd-2.4.6-90.el7.centos.xtpd rpm-qhttpd-devel httpd-devel-2.4.6-90.el7.centos.x8888 systemctlstarthttpd [ root @ grephttpdtcp 603360336033603360336033603360336033603360338/httpd [ root @ lamp~] # # firewall-cmd---permanent---zone=public-- add-service=http success [ root @ lamp~] # firewall-cmd--

[ root @ lamp~] # yum-yintsallmariadbmariadb- servermariadb-libsmariadb-devel [ root @ lamp~] # systemctlstartmaria MySQL _ secure _ installation note 3360 runningallpartsofthisscriptisrecommendedforallmariadbserversinproducsioduce PLEASE READ EACH STEP CAREFULLY! nordertologintomariadbtosecureit,we ' llneedthecurrentpasswordfortherootuser.if you ' vejustinstalledmariadb,and you haven the password will be blank,soyoushouldjustpressenterhere.entercurrentpasswordforroot (enterfornone ) : //确定返回,succecece moving on . settingtherootpasswordensuresthatnobodycanlogintothemariadbrootuserwithouttheproperauthorisation.set rootpasword root用户设置登录密码New password: //密码为ABC 123 re-enter new password 3360//密码为ABC 123 passwordupdatedsuccessfully reloadingprivilegetables . success! By default,amariadbinstallationhasananonymoususer,allowinganyonetologintomariadbwithouthavingtohaveauseraccountcreatedf

orthem. 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? [Y/n] n //是否删除匿名用户,我们不删除 ... 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? [Y/n] n //不允许root用户远程登录,我们允许远程登录 ... 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? [Y/n] n //是否删除测试数据库和访问数据库,选择否 ... skipping.Reloading the privilege tables will ensure that all changes made so farwill 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 MariaDBinstallation should now be secure.Thanks for using MariaDB!

1.4安装LAMP架构的PHP服务,并对PHP服务进行优化

[root@lamp ~]# yum -y install php[root@lamp ~]# yum -y install php-mysql[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 ~]# rpm -q phpphp-5.4.16-46.1.el7_7.x86_64[root@lamp ~]# rpm -q php-mysqlphp-mysql-5.4.16-46.1.el7_7.x86_64[root@lamp ~]# rpm -q php-gdphp-gd-5.4.16-46.1.el7_7.x86_64[root@lamp ~]# rpm -q php-odbcphp-odbc-5.4.16-46.1.el7_7.x86_64[root@lamp ~]# rpm -q php-pearphp-pear-1.9.4-21.el7.noarch[root@lamp ~]# rpm -q php-xmlphp-xml-5.4.16-46.1.el7_7.x86_64[root@lamp ~]# rpm -q php-xmlrpcphp-xmlrpc-5.4.16-46.1.el7_7.x86_64[root@lamp ~]# rpm -q php-mbstringphp-mbstring-5.4.16-46.1.el7_7.x86_64[root@lamp ~]# rpm -q php-snmpphp-snmp-5.4.16-46.1.el7_7.x86_64[root@lamp ~]# rpm -q php-soapphp-soap-5.4.16-46.1.el7_7.x86_64[root@lamp ~]# rpm -q curlcurl-7.29.0-54.el7_7.1.x86_64[root@lamp ~]# rpm -q php-bcmathphp-bcmath-5.4.16-46.1.el7_7.x86_64[root@lamp ~]# rpm -q libcurl-devellibcurl-devel-7.29.0-54.el7_7.1.x86_64[root@lamp ~]# cd /var/www/html/[root@lamp html]# ls[root@lamp html]# vim index.php<?phpphpinfo();?>

1.5重启服务,并在win10主机上面查看

[root@lamp html]# systemctl restart httpd[root@lamp html]# netstat -ntap|grep 80tcp6 0 0 :::80 :::* LISTEN 5941/httpd [root@lamp html]# systemctl restart mariadb[root@lamp html]# netstat -ntap|grep 3306tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 6172/mysqld

在win10主机里面测试

测试没有问题,我们继续进行下一步。

2.nginx服务器的搭建

2.1先将nginx的源码包解压到/opt/目录下面,然后安装必要的软件包,修改主机名为nginx

[root@nginx ~]# lsanaconda-ks.cfg initial-setup-ks.cfg LNMP-C7 公共 模板 视频 图片 文档 下载 音乐 桌面[root@nginx ~]# cd LNMP-C7/[root@nginx LNMP-C7]# lsDiscuz_X3.4_SC_UTF8.zip nginx-1.12.2.tar.gz php-7.1.20.tar.bz2mysql-boost-5.7.20.tar.gz php-5.6.11.tar.bz2 php-7.1.20.tar.gzncurses-5.6.tar.gz php-7.1.10.tar.bz2 zend-loader-php5.6-linux-x86_64_update1.tar.gz[root@nginx LNMP-C7]# tar -zxvf nginx-1.12.2.tar.gz -C /opt/[root@nginx LNMP-C7]# useradd -M -s /s执着的奇迹/nologin nginx //创建程序管理用户[root@nginx LNMP-C7]# yum -y install pcre-devel zlib-devel gcc gcc-c++ pcre make[root@nginx LNMP-C7]# rpm -q gccgcc-4.8.5-39.el7.x86_64[root@nginx LNMP-C7]# rpm -q gcc-c++gcc-c++-4.8.5-39.el7.x86_64[root@nginx LNMP-C7]# rpm -q pcrepcre-8.32-17.el7.x86_64[root@nginx LNMP-C7]# rpm -q pcre-develpcre-devel-8.32-17.el7.x86_64[root@nginx LNMP-C7]# rpm -q makemake-3.82-24.el7.x86_64[root@nginx LNMP-C7]# rpm -q zlibzlib-1.2.7-18.el7.x86_64

2.2开始手工编译安装nginx

[root@nginx LNMP-C7]# cd /opt[root@nginx opt]# lsnginx-1.12.2 rh[root@nginx opt]# cd nginx-1.12.2/[root@nginx nginx-1.12.2]# lsauto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src[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

2.3对nginx进行优化

[root@nginx nginx-1.12.2]# ln -s /usr/local/nginx/s执着的奇迹/nginx /usr/local/s执着的奇迹/[root@nginx nginx-1.12.2]# vim /etc/init.d/nginx#!/执着的奇迹/bash# chkconfig: - 99 20 # description: Nginx Service Control ScriptPROG="/usr/local/nginx/s执着的奇迹/nginx"PIDF="/usr/local/nginx/logs/nginx.pid"case "$1" in start) $PROG ;; stop) kill -s QUIT $(cat $PIDF) ;; rstart) $0 stop $0 start ;; reload) kill -s HUP $(cat $PIDF) ;; *) echo "Usage: $0 {start|stop|restart|reload}" exit 1esacexit 0[root@nginx nginx-1.12.2]# cd /etc/init.d/[root@nginx init.d]# lsfunctions netconsole network nginx README[root@nginx init.d]# chmod +x nginx [root@nginx init.d]# lsfunctions netconsole network nginx README[root@nginx init.d]# chkconfig --add nginx[root@nginx init.d]# chkconfig --level 35 nginx on[root@nginx init.d]# service nginx start[root@nginx init.d]# systemctl stop firewalld[root@nginx init.d]# setenforce 0

在win10主机里面测试nginx主机

3.动静分离 [root@nginx init.d]# vim /usr/local/nginx/conf/nginx.conf location ~ .php$ { proxy_pass http://192.168.73.144; }[root@nginx init.d]# service nginx stop[root@nginx init.d]# service nginx start

测试动静分离的结果

实验总结

通过动静分离技术,我们可以看到资源的利用率和吞吐量有了一个质的提升。目前线网中,都采用了动静分离技术

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。