ubuntu下apache2替换成nginx的方法
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