当前位置:首页 > 计算机相关 > ubuntu系统 > 正文内容

ubuntu下apache2替换成nginx的方法

piikee3个月前 (01-20)ubuntu系统704

ubuntu下apache2实在太占内存,把它替换成nginx+php-fpm模式,节省很多内存,也支持http2。

一、网站证书先下载成nginx证书。

二、网站的伪静态文件.htaccess全部换成nginx的conf配置文件。

三、提前写好nginx的网站配置文件重装好了直接放进去,就比较快,不会导致网站长期打不开。

四、正式进入替换步骤:

    1,卸载apache2。

运行

apt remove apache2

卸载掉apache2,执行后再执行

apt autoremove

移除相关的不用的插件。

2,安装nginx和php-fpm

运行

apt install nginx

安装好nginx.

运行

sudo add-apt-repository ppa:ondrej/php
sudo apt update

添加和更新php源,然后运行

 apt install php7.4 php7.4-fpm php7.4-dev php7.4-mysql php7.4-mysqli php7.4-curl php7.4-json php7.4-mbstring php7.4-xml php7.4-intl php7.4-yaml php7.4-zip

把php和php-fpm和php所需的一些插件全部安装上。

五、更改nginx的日志目录权限为www-data

chown -R www-data:www-data /var/log/nginx

六、配置一下nginx日志按天存放

vi /etc/logrotate.d/nginx

里面内容配置成

/var/log/nginx/*.log {
	daily 
	missingok
	rotate 14
	compress
	delaycompress
	dateext
	notifempty
	create 0640 www-data adm #日志的目录的权限和用户
	sharedscripts
	olddir /var/log/nginx
	prerotate
		if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
			run-parts /etc/logrotate.d/httpd-prerotate; \
		fi \
	endscript
	postrotate
	 find /var/log/nginx -name "*.log-*" -type f -mtime +14 -exec rm -f {} \; #14填删除一下日志
		invoke-rc.d nginx rotate >/dev/null 2>&1
	 if [ -f /run/nginx.pid ]; then
            kill -USR1 `cat /run/nginx.pid`  # 通过USER1信号通知nginx重新打开日志文件
        fi
	endscript
}

七、配置一下nginx的总配置文件,主要设置网站目录

vi /etc/nginx/nginx.conf

里面设置参考:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
worker_rlimit_nofile 51200;
events {
	worker_connections 51200;
	 multi_accept on;
}

http {

	##
	# Basic Settings
	##

	sendfile on;
	tcp_nopush on;
	tcp_nodelay on;
	keepalive_timeout 65;
	types_hash_max_size 2048;
	# server_tokens off;

	# server_names_hash_bucket_size 64;
	# server_name_in_redirect off;

	include /etc/nginx/mime.types;
	default_type application/octet-stream;
	log_format  main  '"$http_x_forwarded_for"-$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent"  '; 
	##
	# SSL Settings
	##

	ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
	ssl_prefer_server_ciphers on;

	##
	# Logging Settings
	##

	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;



        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;
		fastcgi_intercept_errors on;
	##
	# Gzip Settings
	##

	gzip on;
  gzip_vary on;
	  gzip_proxied any;
	  gzip_comp_level 6;
	  gzip_buffers 16 8k;
	  gzip_http_version 1.1;
	  gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
        gzip_disable   "MSIE [1-6]\.";
	# gzip_vary on;
	# gzip_proxied any;
	# gzip_comp_level 6;
	# gzip_buffers 16 8k;
	# gzip_http_version 1.1;
	# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

	##
	# Virtual Host Configs
	##

	include /etc/nginx/conf.d/*.conf;
	include /etc/nginx/sites-enabled/*;
    include /data/siteconf/*.conf; #这里放网站配置文件的目录,自己写
}


#mail {
#	# See sample authentication script at:
#	# http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#	# auth_http localhost/auth.php;
#	# pop3_capabilities "TOP" "USER";
#	# imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#	server {
#		listen     localhost:110;
#		protocol   pop3;
#		proxy      on;
#	}
# 
#	server {
#		listen     localhost:143;
#		protocol   imap;
#		proxy      on;
#	}
#}

附PHP调优:

/etc/php/7.4/fpm/php.ini里面

memory_limit = 128M 根据内存大小配置,一般我是配置内存的一半。
memory_limit = 1024M
user_agent=”Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0“

/etc/php/7.4/fpm/pool.d/www.conf里面

listen.backlog = 8192
pm.max_children = 120
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20
pm.max_requests = 500


扫描二维码推送至手机访问。

版权声明:本文由萍客小居[www.piikee.net]发布,如需转载请注明出处。

本文链接:https://www.piikee.net/1520.html

分享给朋友:

相关文章

(In some cases useful info about processes that use the device is found by lsof(8) or fuser(1))

 (In some cases useful info about processes that use the device is found by lsof(8) or fuser(1))的解决办法。在ubuntu系统中,想umount...

find命令多目录多文件查找和批量删除

linux,ubuntu,centos等系统下find文件查找命令的多目录多文件查找:find 目录一 目录二 目录三 -iname *.swf -or -iname *.txt -or -iname *.inc -or -iname *....

vi编辑器删除第一行到当前光标位置的命令

vi编辑器删除第一行到当前光标位置的命令为:1,.d其中1表示从第一行开始,逗号是分隔符,.表示当前行,也可以输入3表示第三行,d表示删除。...

ubuntu升级内核的方法(命令行升级ubuntu的内核)

sudo apt-get install linux-generic-lts-xenial linux-image-generic-lts-xenial一路y下去,最后reboot重启。重启完uname -a就可以看到新内核版本了。...

vim文本处理

1. 基本的替换:s/vivian/sky/ 替换当前行第一个 vivian 为 sky:s/vivian/sky/g 替换当前行所有 vivian 为 sky:n,$s/vivian/sky/ 替换第 n 行开始到最后一行中每一行的第一个...

ubuntu下vi编辑器左右方向出错,删除和i键出错的解决办法

其实是ubuntu下vi编辑器是简单版本导致的,可以apt-get update,然后apt-get upgrade更新一下系统包。然后 apt-get install vim-full 安装全功能版本的vim编辑器。这样子就不会发生向右键...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。