目录
ColinOL Home
Ubuntu 搭建LNMP服务器(Linux/Nginx/MySQL/PHP)
准备
相关文章:
更新软件源
sudo apt-get install software-properties-common sudo add-apt-repository ppa:ondrej/php sudo apt-get update
安装 Nginx
关于php网站的配置
DokuWiki网站的Nginx配置
- Dokuwiki的安全设置 https://www.dokuwiki.org/security#web_access_security
- Dokuwiki 2018-04-22b “Greebo” 不支持 php7.3 (亲测支持php7.0)
安装php后,需要在配置文件中开启php功能。 以 vim /etc/nginx/sites-available/default 为例
# 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中的php版本应与当前安装的版本一致
安装 PHP
默认安装php
sudo apt-get install php
或者直接指定版本安装php
sudo apt-get install php7.0
查看安装的php版本(本次安装的为 php7.0)
php -v
安装php*-fpm模块
sudo apt-get install php7.0-fpm
安装其他可能需要的模块
sudo apt-get install php7.0-gd sudo apt-get install php7.0-mbstring
php相关服务的控制
sudo systemctl start php7.0-fpm sudo systemctl stop php7.0-fpm sudo systemctl restart php7.0-fpm sudo systemctl status php7.0-fpm
查询、安装php的可选模块
查询可用模块
apt-cache search php7.0
安装模块
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*.*-fpm 相关的文件权限
chown -R www-data:www-data /var/www/test && chmod -R 755 /var/www/test
一些问题的解决办法
nginx搭建php提示No input file specified.
打开fastcgi.conf
vim /etc/nginx/fastcgi.conf
设置fastcgi的可操作目录(防止跨站),在最后添加:
fastcgi_param PHP_VALUE open_basedir=/opt/:/tmp/;
重启nginx
service nginx restart
安装 MySQL
注意事项
安装
卸载
完全移除Nginx
sudo apt-get --purge autoremove nginx
重置Nginx配置文件
sudo apt-get -o DPkg::options::=--force-confmiss --reinstall install nginx-common
怎么完全卸载、安装Nginx,参考:
查看已安装的相关软件(以nginx为例)
dpkg --get-selections | grep nginx
Nginx的安装与配置(Ubuntu)
适用版本:Ubuntu 16.04、Ubuntu 18.04
安装 Nginx
安装
sudo apt-get install nginx
如果要查看加载的是哪个配置文件,可以用这个命令
sudo nginx -t
或者
ps -ef | grep nginx
配置文件一般在这里
/etc/nginx/nginx.conf
控制 Nginx 方式1
通过这种方式安装的,会自动创建服务,会自动在 /etc/init.d/nginx 新建服务脚本,然后就可以使用下面的命令启动、停止等。
sudo service nginx {start|stop|restart|reload|force-reload|status|configtest|rotate|upgrade}
如
sudo service nginx start sudo service nginx stop sudo service nginx restart
控制 Nginx 2(推荐)
可以通过键入以下命令来检查systemd init系统以确保服务正在运行:
systemctl status nginx
要停止您的Web服务器,请键入:
sudo systemctl stop nginx
停止时要启动Web服务器,请输入:
sudo systemctl start nginx
要停止并再次启动服务,请键入:
sudo systemctl restart nginx
如果您只是简单地进行配置更改,Nginx通常可以重新加载而不会丢失连接。 为此,请输入:
sudo systemctl reload nginx
默认情况下,Nginx配置为在服务器引导时自动启动。 如果这不是您想要的,可以通过输入以下命令来禁用此行为:
sudo systemctl disable nginx
要重新启用服务以在启动时启动,您可以键入:
sudo systemctl enable nginx
管理网站
Nginx默认启用了一个服务器模块,该模块被配置为在以下目录:
/var/www/html
虽然这适用于单个站点,但如果您托管多个站点,它可能会变得很笨重。 我们不必修改/var/www/html ,而是在/var/www为我们的example.com网站创建一个目录结构,并将/var/www/html 保留为默认目录,如果客户端请求没有匹配任何其他网站。
按如下所示为example.com创建目录,使用-p标志创建任何必需的父目录:
sudo mkdir -p /var/www/example.com/html
接下来,使用$USER环境变量分配目录的所有权:
sudo chown -R $USER:$USER /var/www/example.com/html
如果你没有修改你的umask值,你的web根目录的权限应该是正确的,但是你可以通过输入:
sudo chmod -R 755 /var/www/example.com
如果是使用的php*-fpm,则用户应为 www-data
sudo chown -R www-data:www-data /var/www/test && chmod -R 755 /var/www/test #或者在当前目录设置 sudo chown -R www-data:www-data * && chmod -R 755 *
接下来,使用编辑器创建一个index.html页面示例:
vim /var/www/example.com/html/index.html
在里面,添加下面的示例HTML:
<html> <head> <title>Welcome to Example.com!</title> </head> <body> <h1>Success! The example.com server block is working!</h1> </body> </html>
完成后保存并关闭文件。
为了让Nginx提供这些内容,有必要创建一个具有正确指令的服务器块。 我们不要直接修改默认配置文件,而是在/etc/nginx/sites-available/example.com上创建一个新文件: sudo vim /etc/nginx/sites-available/example.com
粘贴到以下配置块中,该块类似于默认值,但已更新为我们的新目录和域名:
server { listen 80; listen [::]:80; root /var/www/example.com/html; index index.html index.htm index.nginx-debian.html; server_name example.com www.example.com; location / { try_files $uri $uri/ =404; } }
请注意,我们已将root配置更新到我们的新目录,并将server_name为我们的域名。
接下来,让我们通过创建一个链接到启动sites-enabled目录来启用该文件,该目录是Nginx在启动过程中读取的:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
现在启用两个服务器模块并将其配置为基于listen和server_name指令响应请求(您可以阅读关于Nginx如何处理这些指令的更多信息):
- example.com :将响应example.com和www.example.com请求。
- default :将响应端口80上与其他两个块不匹配的任何请求。
为了避免添加额外的服务器名称可能导致的哈希桶内存问题,有必要调整/etc/nginx/nginx.conf文件中的单个值。 打开文件:
sudo vim /etc/nginx/nginx.conf
找到server_names_hash_bucket_size指令并删除#符号以取消注释该行:
... http { ... server_names_hash_bucket_size 64; ... } ...
接下来,测试以确保您的Nginx文件中没有语法错误:
sudo nginx -t
完成后保存并关闭文件。
如果没有任何问题,请重新启动Nginx以启用您的更改:
sudo systemctl restart nginx
Nginx现在应该为您的域名提供服务。
你可以通过导航到http://example.com来测试它!
一些问题
出现类似 nginx depends on nginx-core (>= 1.10.3-0ubuntu0.16.04.4) 的错误的解决办法:
首先看是否有 Apache2 服务正在启动,关闭它
sudo systemctl stop apache2.service
防止 Apache2 服务自启
sudo systemctl disable apache2.service
重新安装 Nginx
sudo apt-get install nginx
Nginx主要的文件和目录
内容
- /var/www/html :默认情况下,实际的网页内容仅包含您之前看到的默认Nginx页面,它将在/var/www/html目录中提供。 这可以通过改变Nginx配置文件来改变。
服务器配置
- /etc/nginx :Nginx配置目录。 所有的Nginx配置文件都驻留在这里。
- /etc/nginx/nginx.conf :主要的Nginx配置文件。 这可以修改,以更改Nginx全局配置。
- /etc/nginx/sites-available/ :可存储每个站点服务器块的目录。 除非将Nginx链接到sites-enabled了sites-enabled目录,否则Nginx不会使用此目录中的配置文件。 通常,所有服务器块配置都在此目录中完成,然后通过链接到其他目录启用。
- /etc/nginx/sites-enabled/ :存储启用的每个站点服务器块的目录。 通常,这些是通过链接到sites-available目录中的配置文件创建的。
- /etc/nginx/snippets :这个目录包含可以包含在Nginx配置其他地方的配置片段。 可重复配置的片段可以重构为片段。
服务器日志
- /var/log/nginx/access.log :除非Nginx配置为其他方式,否则每个对您的Web服务器的请求都会记录在此日志文件中。
- /var/log/nginx/error.log :任何Nginx错误都会记录在这个日志中。
Dokuwiki网站的配置
/boot 空间不足,导致安装、更新失败
错误信息
gzip: stdout: No space left on device E: mkinitramfs failure cpio 141 gzip 1 update-initramfs: failed for /boot/initrd.img-#.#.#-##-generic with 1. dpkg: error processing initramfs-tools (--configure): subprocess installed post-installation script returned error exit status 1 Errors were encountered while processing: initramfs-tools E: Sub-process /usr/bin/dpkg returned an error code (1)
或者
Not enough free disk space The upgrade has aborted. The upgrade needs a total of 101 M free space on disk '/boot'. Please free at least an additional 101 M of disk space on '/boot'. You can remove old kernels using 'sudo apt autoremove' and you could also set COMPRESS=xz in /etc/initramfs-tools/initramfs.conf to reduce the size of your initramfs.
或者
The following packages were automatically installed and are no longer required: linux-headers-4.4.0-131 linux-headers-4.4.0-131-generic linux-headers-4.4.0-133 linux-headers-4.4.0-133-generic linux-headers-4.4.0-134 linux-headers-4.4.0-134-generic linux-headers-4.4.0-137 linux-headers-4.4.0-137-generic linux-headers-4.4.0-139 linux-headers-4.4.0-139-generic linux-headers-4.4.0-62 linux-headers-4.4.0-62-generic linux-image-4.4.0-131-generic linux-image-4.4.0-133-generic linux-image-4.4.0-134-generic linux-image-4.4.0-137-generic linux-image-4.4.0-139-generic linux-image-4.4.0-62-generic linux-image-extra-4.4.0-131-generic linux-image-extra-4.4.0-133-generic linux-image-extra-4.4.0-134-generic linux-image-extra-4.4.0-137-generic linux-image-extra-4.4.0-139-generic linux-image-extra-4.4.0-62-generic Use 'apt autoremove' to remove them. The following additional packages will be installed: linux-image-4.4.0-139-generic linux-image-4.4.0-164-generic linux-modules-4.4.0-164-generic Suggested packages: fdutils linux-doc-4.4.0 | linux-source-4.4.0 linux-tools The following NEW packages will be installed: linux-image-4.4.0-139-generic linux-image-4.4.0-164-generic linux-modules-4.4.0-164-generic 0 upgraded, 3 newly installed, 0 to remove and 135 not upgraded. 7 not fully installed or removed. Need to get 41.0 MB of archives. After this operation, 135 MB of additional disk space will be used.
解决办法
空间不够,先清理空间
cd /boot rm abi-4.4.0-133-generic rm config-4.4.0-133-generic rm initrd.img-4.4.0-133-generic rm retpoline-4.4.0-133-generic rm System.map-4.4.0-133-generic rm vmlinuz-4.4.0-133-generic
查看当前系统内核
uname -a
查看已安装的内核
dpkg -l 'linux-image-*' | grep '^ii'
逐个删除不用的内核
sudo apt-get purge linux-image-4.4.0-137-generic sudo apt-get purge linux-image-4.4.0-138-generic sudo apt-get purge linux-image-4.4.0-139-generic sudo apt-get purge linux-image-4.4.0-164-generic sudo apt-get purge linux-image-4.4.0-62-generic
参考:
Linux 升级内核
1# 查看当前系统的linux内核版本
执行下面的命令:
uname -mrs
命令输出如下:
Linux 4.4.0-62-generic x86_64
2# 查看当前可用的最新版本的linux 内核
执行下面的命令:
apt-cache search linux-generic
示例2:
Ubuntu检查内核版本:
uname -a
确定你的系统是32位还是64位的:
getconf LONG_BIT
直接通过wget命令,去http://kernel.ubuntu.com/~kernel-ppa/mainline/去下载最新的程序包,例如我去下载64位的4.10.2的内核:
wget http://kernel.ubuntu.com/~kernel-ppa/mainline/v4.10.2/linux-image-4.10.2-041002-generic_4.10.2-041002.201703120131_amd64.deb
切换到你的文件下载目录,执行下面的命令进行升级:
dpkg -i linux-image-4.10.2-041002-generic_4.10.2-041002.201703120131_amd64.deb
更新grub引导装入程序:
update-grub
重启机器:
reboot
再查看自己的内核版本就会发现已经更新到4.10.2了~
linux 查看正在占用的端口
1.使用lsof
lsof -i
查看单个端口使用情况:
lsof -i:port
2.使用netstat
方法1:
netstat -anp|grep 80
方法2:
netstat -nultp
参考
评论