假期里无聊,把VPS上的HTTP Server从Apache迁移到了Nginx(对,就是因为无聊)。下面做一下大概的记录。
安装Nginx
先把原来的Apache服务停掉,不然默认都要监听80端口,会存在冲突: sudo service apache2 stop Ubuntu系统下,还是使用仓库进行安装: sudo apt-get install nginx
配置Nginx
安装完成后,默认的站点会指向/var/www/html。这个目录可以在/etc/nginx/sites-available/default文件中配置。 因为Wordpress放在二级域名下,而且是一个PHP的服务,所以需要额外做一些配置。复制/etc/nginx/sites-available/default,命名为/etc/nginx/sites-available/blog.xiaoyu.im.conf,打开进行编辑,内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
server { listen 80; listen [::]:80; # SSL configuration # # listen 443 ssl default_server; # listen [::]:443 ssl default_server; # # Note: You should disable gzip for SSL traffic. # See: https://bugs.debian.org/773332 # # Read up on ssl_ciphers to ensure a secure configuration. # See: https://bugs.debian.org/765782 # # Self signed certs generated by the ssl-cert package # Don't use them in a production server! # # include snippets/snakeoil.conf; root /var/www/blog; # Add index.php to the list if you are using PHP index index.html index.htm index.php; server_name blog.xiaoyu.im; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. if (!-e $request_filename) { rewrite ^([_0-9a-zA-Z-]+)?(/wp-.*) $2 last; rewrite ^([_0-9a-zA-Z-]+)?(/.*\.php)$ $2 last; rewrite ^ /index.php last; } } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { include snippets/fastcgi-php.conf; # With php7.0-cgi alone: #fastcgi_pass 127.0.0.1:9000; # With php7.0-fpm: fastcgi_pass unix:/run/php/php7.0-fpm.sock; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one location ~ /\.ht { deny all; } } |
然后将该配置文件链接到sites-enabled目录: sudo ln -s /etc/nginx/sites-available/blog.xiaoyu.im.conf /etc/nginx/site-enabled/ 让Nginx重新加载配置文件: sudo service nginx reload
至此,原来运行于Apache的站点,正常迁移至Nginx了。