在Centos7上将Apache(httpd)切换为Nginx的过程记录

简介: 近期要上线几个基于tornado+motor的移动端接口服务,众所周知,Apache和tornado天生八字不合,尤其apache对python3尤为的不友好,tornado和nginx才是木石前盟,另外由于apache目前系统占用确实比较高,不光进程数多,httpd竟然占用了200多M,太庞大,决定换为较轻量级,高并发的nginx。

近期要上线几个基于tornado+motor的移动端接口服务,众所周知,Apache和tornado天生八字不合,尤其apache对python3尤为的不友好,tornado和nginx才是木石前盟,另外由于apache目前系统占用确实比较高,不光进程数多,httpd竟然占用了200多M,太庞大,决定换为较轻量级,高并发的nginx。

如上图所示:系统也就2g ,除了mysql占用的100M, httpd 占了1/2 还多

首先由于apache和nginx默认都是监听80端口,所以首先要停止apache服务(为了保险起见,只是停止服务,不要卸载)

systemctl  stop httpd

第二步,安装nginx

#设置源
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

#安装
yum install -y nginx

#启动服务
systemctl start nginx.service

#开机自启
systemctl enable nginx.service

由于机器上还有一些php服务,而nginx需要php-fpm的支持才能代理php,所以安装php-fpm

yum install php-fpm

yum install php-mysql php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt

开启php服务
systemctl start php-fpm

开机启动php服务
systemctl enable php-fpm

然后将原来apache的服务配置翻译成nginx的,二者大同小异,值得一提的是,关于https服务,nginx配置要简单很多

apache配置:

<VirtualHost _default_:443>

#   General setup for the virtual host
DocumentRoot "/opt/v3u"
ServerName v3u.cn:443
ServerAdmin you@example.com
ErrorLog "/opt/lampp/logs/error_log"
TransferLog "/opt/lampp/logs/access_log"

#   SSL Engine Switch:
#   Enable/Disable SSL for this virtual host.
SSLEngine on


SSLCertificateFile "证书目录"
SSLCertificateKeyFile "证书目录"
SSLCertificateChainFile "证书目录"


<FilesMatch ".(cgi|shtml|phtml|php)$">
    SSLOptions +StdEnvVars
</FilesMatch>
<Directory "/opt/lampp/cgi-bin">
    SSLOptions +StdEnvVars
</Directory>


BrowserMatch "MSIE [2-5]" 
         nokeepalive ssl-unclean-shutdown 
         downgrade-1.0 force-response-1.0

CustomLog "/opt/lampp/logs/ssl_request_log" 
          "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x "%r" %b"

</VirtualHost> 

翻译为ngixn的配置:

server {
        listen 443 ssl;
        server_name v3u.cn;
        root /opt/v3u;
location / {
            index  index.html index.htm index.php;
        rewrite ^/sitemap.xml$ /Index_sitemap last;
    rewrite ^/resume$ /Index_resume last;
    rewrite ^/search_(.+?)$ /Index_search_text_$1 last;
    rewrite ^/a_id_(.+?)$ /Index_a_id_$1 last;
    rewrite ^/ls_id_(.+?)$ /Index_ls_id_$1 last;
    rewrite ^/l_id_(.+?)$ /Index_l_id_$1 last;
         
         if (!-e $request_filename) {
   rewrite  ^(.*)$  /index.php?s=$1  last;
   break;
    }

        }

        ssl_certificate   证书目录;
        ssl_certificate_key  证书目录;

        location ~ .php(.*)$ {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param PATH_INFO $1;
                include fastcgi_params;
        }
}

修改好配置文件,重启nginx

systemctl restart nginx.service

最后别忘了将apache的开机自启关闭

systemctl disable httpd

ok,到此从apache迁移到nginx就配置结束了,总体上没啥难度

由图上可知,应用了nginx之后,系统感觉清爽了很多,内存也节约了大约300多m,系统告别臃肿,轻装上阵。

相关文章
|
3月前
|
Java 应用服务中间件 Apache
简介Nginx,Tomcat和 Apache
简介Nginx,Tomcat和 Apache
简介Nginx,Tomcat和 Apache
|
5月前
|
Linux 网络安全 Apache
Centos下操作Apache httpd
Centos下操作Apache httpd
74 0
|
3月前
|
Linux 应用服务中间件 网络安全
CentOS7搭建本地离线局域网yum源(Httpd/Nginx+yum)
CentOS7搭建本地离线局域网yum源(Httpd/Nginx+yum)
421 0
|
2月前
|
Java Linux
Flume【环境搭建 01】CentOS Linux release 7.5 安装配置 apache-flume-1.9.0 并验证
【2月更文挑战第16天】Flume【环境搭建 01】CentOS Linux release 7.5 安装配置 apache-flume-1.9.0 并验证
39 0
|
2天前
|
Linux Apache
Centos7 安装Apache
安装Apache的步骤
16 6
|
19天前
|
运维 Apache 数据安全/隐私保护
CentOS6.5搭建SVN服务器(Apache+SVN)
CentOS6.5搭建SVN服务器(Apache+SVN)
|
27天前
|
数据可视化 Linux Apache
CentOS部署Apache Superset大数据可视化BI分析工具并实现无公网IP远程访问
CentOS部署Apache Superset大数据可视化BI分析工具并实现无公网IP远程访问
|
1月前
|
Linux Apache
CentOS 7 源码安装LAMP环境源 和apache监听别的端口
CentOS 7 源码安装LAMP环境源 和apache监听别的端口
21 0
|
2月前
|
Shell Linux 网络安全
【Shell 命令集合 网络通讯 】Linux 管理Apache HTTP服务器 httpd命令 使用指南
【Shell 命令集合 网络通讯 】Linux 管理Apache HTTP服务器 httpd命令 使用指南
36 0
|
17天前
|
移动开发 前端开发 JavaScript
前端vue2、vue3去掉url路由“ # ”号——nginx配置(一)
前端vue2、vue3去掉url路由“ # ”号——nginx配置
49 0