网站一直跑的是apache,觉得卡卡的。换nginx玩玩喽。nginx还是很强大的,感觉速度有些提升,奥力给!!!
已经安装了Apache2的话,要先删除再安装nginx!
service apache2 stop
update-rc.d -f apache2 remove
apt-get remove apache2
apt-get -y install nginx
service nginx start
输入ip或域名就能看到 Welcome to Nginx!
我们可以通过使nginx的PHP工作PHP-FPM(PHP-FPM(FastCGI进程管理器)是为任何规模的网站,尤其是繁忙的网站有用的一些附加功能的替代PHP的FastCGI实现),我们安装如下:
apt-get -y install php7.0-fpm
虚拟主机服务器{}容器定义。默认的虚拟主机是在文件中定义的/etc/nginx/sites-available/default
将index index.html index.htm index.nginx-debian.html;
改成index index.html index.htm index.php;
server_name _;这里改成你的主机名,一般是域名。例如:server_name zerlong.com;
找到下边这一段,把注释去掉
# 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;
}
注意上边fastcgi_pass只能用一个,不然会出错类似”fastcgi_pass”
directive is duplicate in /etc/nginx/sites-enabled/default
保存完重新加载nginx:
service nginx reload
编辑/etc/php/7.0/fpm/php.ini 设置 cgi.fix_pathinfo=0
service php7.0-fpm reload
建立个探针文件访问一下能出信息了,
使用下面的命令安装:
apt-get -y install php7.0-mysql php7.0-curl php7.0-gd php7.0-intl php-pear php-imagick php7.0-imap php7.0-mcrypt php-memcache php7.0-pspell php7.0-recode php7.0-sqlite3 php7.0-tidy php7.0-xmlrpc php7.0-xsl php7.0-mbstring php-gettext
APCu是随PHP7 PHP Opcache模块的扩展,它增加了一些兼容性功能的支持APC缓存(例如WordPress的插件缓存)软件。
APCu可以安装如下:
apt-get -y install php-apcu
重新加载 PHP-FPM:
service php7.0-fpm reload
好了,跑起来吧网站!
附送nginx+https SSL配置
同时启用HTTP和HTTPS
server {
listen 80;
listen 443 ssl;
server_name zerlong.com; #最好写绝对路径,你懂的
ssl_certificate zerlong.com.crt;
ssl_certificate_key zerlong.com.key;
...
自动跳转https
server {
listen 80;
server_name zerlong.com;
rewrite ^ https://$http_host$request_uri? permanent; # force redirect http to https
#return 301 https://$http_host$request_uri;
}
server {
listen 443 ssl;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
server_name zerlong.com;
#禁止在header中出现服务器版本,防止黑客利用版本漏洞攻击
server_tokens off;
#如果是全站 HTTPS 并且不考虑 HTTP 的话,可以加入 HSTS 告诉你的浏览器本网站全站加密,并且强制用HTTPS 访问
#add_header Strict-Transport-Security "max-age=31536000; includeSubdomains";
# ......
fastcgi_param HTTPS on;
fastcgi_param HTTP_SCHEME https;
}
在站点配置文件的server { } 大括号里面添加下面的代码,然后重启nginx
location / {
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}