Ubuntu安装配置lnmp(Nginx+MySQL+PHP)自用非教程

由于之前使用树莓派,然后习惯性的把腾讯云上的服务器也装成了Debian,由于apt-get到的nginx版本低以及无法直接get到php7,于是换成了Ubuntu Server 16.04.1 LTS 64位

机器配置

操作系统 Ubuntu Server 16.04.1 LTS 64位 CPU 1核 内存 1GB 系统盘 20GB(云硬盘) 公网带宽 1Mbps

前期准备工作

《树莓派基本配置以及安装配置lnmp(Nginx+MySQL+PHP)》 一致的做一下更新:

#更新软件列表。
sudo apt-get update
#更新软件。
sudo apt-get upgrade
#更新系统版本。
sudo apt-get dist-upgrade

升级后出现了烦人的一串报错信息:

N: Ignoring file '50unattended-upgrades.ucf-old' in directory '/etc/apt/apt.conf.d/' as it has an invalid filename extension

参考信息

这里选择给他命名一下,就不删除了。

mv /etc/apt/apt.conf.d/50unattended-upgrades.ucf-old /etc/apt/apt.conf.d/50unattended-upgrades.ucf-old.bak

服务器默认使用的登录用户为ubuntu,为了方便su给root设置密码:

sudo passwd root

然后再来看看apt-get到的包的版本吧。可以使用apt-cache show 包名

apt-cache show nginx Version: 1.10.0-0ubuntu0.16.04.4 apt-cache show mysql-server Version: 5.7.11-0ubuntu6 apt-cache show php Version: 1:7.0+35ubuntu6

版本比debian get到的高,懒得编译系列。很棒~

安装篇

apt-get install nginx  mysql-server php

一路回车,或者上面直接加-y

关于php,会附带安装以下包 php php-common php7.0 php7.0-cli php7.0-common php7.0-fpm php7.0-json php7.0-opcache php7.0-readline

然后检查三个服务的状态:

systemctl status nginx
systemctl status php7.0-fpm
systemctl status mysql

P.s.:可以通过 php -i | grep -i openssl或者php -r "phpinfo();" | grep -i openssl查看PHP是否启用openssl 这样就完成了安装篇~

配置篇

  1. 配置nginx

nginx.conf

cp /etc/nginx/nginx.conf  /etc/nginx/nginx.conf.bak
vim /etc/nginx/nginx.conf

详细解释可以参照树莓派配置篇 user www-data; worker_processes auto; pid /run/nginx.pid;

events {
	worker_connections 768;
	# 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;

	##
	# SSL Settings
	##

	ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # 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;

	##
	# Gzip Settings
	##

	gzip on;
	gzip_disable "msie6";

	 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_types text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml text/javascript application/javascript application/x-javascript text/x-json application/json application/x-web-app-manifest+json text/css text/plain text/x-component font/opentype font/ttf application/x-font-ttf application/vnd.ms-fontobject image/x-icon;


	##
	# Virtual Host Configs
	##

	include /etc/nginx/conf.d/*.conf;
	include /etc/nginx/sites-enabled/*;
}


#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;
#	}
#}
  1. 配置站点文件

cp /etc/nginx/sites-available/default /etc/nginx/sites-available/wwww #拷贝一份默认配置文件 vim /etc/nginx/sites-available/wwww #编辑新的站点文件 ln -s /etc/nginx/sites-available/wwww /etc/nginx/sites-enabled #建立软连接

我的配置文件如下:

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#

server {
    listen 80 ;
    listen [::]:80 ;
    server_name wwww.lvmoo.com;
    rewrite ^/(.*) https://wwww.lvmoo.com/$1 permanent;
    #将访问请求强制转向到https加密传输
}

server {
	add_header Strict-Transport-Security "max-age=15552000; includeSubDomains; preload";
	add_header X-Frame-Options SAMEORIGIN;
	add_header X-Content-Type-Options nosniff;
	# SSL configuration
	#
	# listen 443 ssl default_server;
	# listen [::]:443 ssl default_server;
	#
	listen 443 ssl http2 ;
        listen [::]:443 ssl http2 ;
        ssl_certificate /srv/www/wwww.lvmoo.com.crt;
        ssl_certificate_key /srv/www/wwww.lvmoo.com.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        #ssl_ciphers   HIGH:!aNULL:!MD5;
	ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;

        ssl_prefer_server_ciphers   on;
	# 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 /srv/www/html/wwww;

	# Add index.php to the list if you are using PHP
	index index.php index.html index.htm index.nginx-debian.html;

	server_name wwww.lvmoo.com;

	location / {
	proxy_http_version       1.1;
    #typecho伪静态实现
	index index.html index.php; 
        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; 
}
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		try_files $uri $uri/ =404;
	}
		client_max_body_size 32m;
	# 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_param  SCRIPT_FILENAME /srv/www/html/wwww$fastcgi_script_name;
		include fastcgi_params;
	}

	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	#location ~ /\.ht {
	#	deny all;
	#}
	location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
    expires 30d;
    access_log off;
    }   

    location ~ .*\.(eot|ttf|otf|woff|svg)$ {
    expires 30d;
    access_log off;
    }

    location ~ .*\.(js|css)?$ {
    expires 7d;
    access_log off;
    }
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#	listen 80;
#	listen [::]:80;
#
#	server_name example.com;
#
#	root /var/www/example.com;
#	index index.html;
#
#	location / {
#		try_files $uri $uri/ =404;
#	}
#}
  1. 配置php、配置php-fpm

    cp /etc/php/7.0/fpm/php.ini /etc/php/7.0/fpm/php.ini.bak vim /etc/php/7.0/fpm/php.ini

    cp /etc/php/7.0/fpm/pool.d/www.conf /etc/php/7.0/fpm/pool.d/www.conf.bak vim /etc/php/7.0/fpm/pool.d/www.conf

参照树莓派配置篇

  1. 配置Mysql

    mysql_secure_installation

  2. 安装配置phpmyadmin

    apt-get install phpmyadmin

    sudo ln -s /usr/share/phpmyadmin /srv/www/html

附:开启了http2后,可以在Chrome浏览器地址栏键入

chrome://net-internals/#http2

以已检查是否成功

Licensed under CC BY-NC-SA 4.0
最后更新于 Mar 09, 2017 14:02 UTC
点击刷新🚌