LNMP网站环境搭建

几天前收到阿里云发来的短信和邮件,提醒我shuxueben.cn网站未指向阿里云服务器,如果3天内不修正就要删除我的网站备案接入信息。 我又去阿里云买了一个“轻量应用服务器”,环境选择了Ubuntu。


所有的命令我都是以root身份执行的。 首先更新系统[1]

apt-get update
apt-get upgrade
apt-get dist-upgrade

安装Nginx

apt-get install nginx

查看是否安装成功

nginx -v 

这时候,访问服务器ip地址或指向该ip地址的域名,就可以看到一个Nginx的欢迎信息。 安装Mysql

apt-get install mysql-server (中间会让输入root密码)
apt-get install mysql-client

查看是否安装成功

mysql -u root -p
回车,输入密码
select version();
exit; #退出mysql环境

安装php

apt-get install php
php -v(查看版本)

安装FastCgi

apt-get install spawn-fcgi

修改Nginx站点配置文件

cd /etc/nginx/sites-available/
cp default default.backup
vim default

添加index.php

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

去掉部分注释[2]:

	# 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;
	}

安装mysql扩展

apt-get install php7.0-mysql

安装必要扩展

apt-get install php7.0 php-pear
apt-get install php7.0-curl
apt-get install php7.0-json
apt-get install php7.0-cgi

在根目录(/var/www/html 可以在default配置文件中看到)中新建test.php文件,测试php

vim test.php
按i进入编辑模式,插入:
php echo phpinfo(); ?
按Esc,输入“:wq”保存并退出

在浏览器中访问“你的ip/test.php”测试。 nginx相关命令

service nginx restart
service nginx start
service nginx stop

Nginx配置文件夹中的site-available和sites-enabled的区别[3][4]

If you are coming from Apache, the "sites-available" and "sites-enabled" 
directories will be familiar.

These directories are used to define configurations for your websites. 
Files are generally created in the "sites-available" directory, and then
symbolically linked to the "sites-enabled" directory when they are ready 
to go live.

...

In the "nginx.conf" file, we can see that the end of the "http" block has:

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

# How To Configure Nginx
# https://www.digitalocean.com/community/tutorials/how-to-configure-the-nginx-web-server-on-a-virtual-private-server

Nginx不能正常启动的常见原因[5] nginx端口被占用

netstat -tnlp

ps -ef | grep nginx
pkill -9 nginx

service nginx restart

nginx 日志文件出现”fastcgi_pass” directive is duplicate 错误[6] 这是在修改刚才那个配置文件default的时候,同时去掉了“fastcgi_pass 127.0.0.1:9000;”和“fastcgi_pass unix:/run/php/php7.0-fpm.sock;”这两行的注释。


启动php-fpm进程[7]

systemctl start php7.0-fpm
或
service start php7.0-fpm

而非

systemctl start php-fpm
service start php-fpm

Nginx添加虚拟主机Virtual Host 把Nginx配置文件default最下面的一些代码取消注释,把关键信息修改成你自己的即可。 我在添加虚拟主机的同时,还将顶级域名301重定向到www二级域名[8][9],我的代码如下:

# 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 www.shuxueben.cn;

	root /var/www/www.shuxueben.cn;
	index index.html index.php;

	location / {
		try_files $uri $uri/ =404;
	}
}
server {
	listen 80;
	listen [::]:80;

	server_name shuxueben.cn;
	return 301 http://www.shuxueben.cn$request_uri;
}

listen 80;和listen [::]:80;均表示监听80端口的链接,后者是前者的ipv6版本[10]。 在访问php页面时总是弹出下载页面[11] 这是因为在nginx的配置文件中没有添加解析php的代码,我们把default文件中虚拟主机那部分的代码修改为如下形式:

server {
	listen 80;
	listen [::]:80;

        server_name www.shuxueben.cn;

	root /var/www/www.shuxueben.cn;
	index index.html index.php;

	location / {
		try_files $uri $uri/ =404;
	}

	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;
        }
}

安装phpMyAdmin[12]

apt install phpmyadmin;

提示选择web服务器时,因为列表中没有Nginx,按Tab键跳过。再按回车。 Configure database for phpmyadmin with dbconfig-common? 选择Yes MySQL application password for phpmyadmin,这个密码仅仅在phpMyAdmin内部用于和MySQL通信,留空即可,会自动生成密码。登录phpMyAdmin时还是用你的MySQL密码。 创建符号链接

ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin

访问“你的ip/phpmyadmin”测试。这里的用户名和密码是root和你的MySQL密码。


上传网站代码,以WordPress为例

cd /var/www/www.shuxueben.cn
wget https://wordpress.org/latest.tar.gz
tar zxf latest.tar.gz
#得到wordpress文件夹

修改文件所有者和用户组为www-data[13][14]

chown www-data:www-data -R wordpress

文件权限不需要修改。修改权限可以使用chmod命令。 批量给文件设置644权限、给文件夹设置755权限[15]

chmod 644 -R wordpress
find wordpress -type d -print|xargs chmod 755

创建www用户组和用户(用不到)[16] 我的wordpress上传文件时提示输入ftp账户和密码,是文件所有者和所在用户组的问题。我以为应该把文件的所有者和用户组设为www,其实应该设为www-data。 系统中没有www用户和用户组。其实不需要手动添加的。

id www
groupadd www
useradd -g www -s /sbin/nologin www
id www

后来我发现没用,又把www用户和用户组删了。 删除用户和用户组[17]

userdel www
groupdel www

wordpress上传文件大小最大只有2MB,需要修改php.ini文件。 找到php.ini文件,可用浏览器访问我们刚才写的test.php文件,里面有php.ini的路径。[18]

/etc/php/7.0/fpm/php.ini

修改wordpress固定链接[19] 需要修改Nginx的网站配置文件代码,要添加的代码为:

	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;
	}

完整的虚拟主机配置代码为:

# 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 www.shuxueben.cn;

	root /var/www/www.shuxueben.cn;
	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;
	}
	location / {
		try_files $uri $uri/ =404;
	}

	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;
        }
}
server {
	listen 80;
	listen [::]:80;

	server_name shuxueben.cn;
	return 301 http://www.shuxueben.cn$request_uri;
}

wordpress后台工具里有一个“站点健康”,提醒我php没有安装bcmath和imagick模块。[20][21]

apt-get install php-bcmath
php -m #如果出现bcmath就说明安装成功了

apt-get install php-imagick
php -m #如果出现imagick就说明安装成功了, 如果没有,可重启php-fpm服务
service php7.0-fpm restart

#或者重启apache 2019年8月31日0:40更新
systemctl restart apache2

参考文献 我想说明的是,我不保障下面所列网址里的内容为作者原创,但它们都给予了我很大的帮助。

[1] Ubuntu 16 安装Nginx+Php+Mysql - 西贝小小凤 - 博客园 https://www.cnblogs.com/xbxxf/p/9122920.html
[2] 在树莓派上安装 Nginx + PHP - 简书 https://www.jianshu.com/p/81a75b4b51bd
[3] nginx里面的sites-available和sites-enabled有什么区别 - SegmentFault 思否 https://segmentfault.com/q/1010000009726769
[4] How To Configure The Nginx Web Server On a Virtual Private Server | DigitalOcean https://www.digitalocean.com/community/tutorials/how-to-configure-the-nginx-web-server-on-a-virtual-private-server
[5] 关于Nginx不能正常启动的问题 - 秋天未成熟的博客 - CSDN博客 https://blog.csdn.net/u012832088/article/details/80729002
[6] 解决安装nginx 日志文件出现"fastcgi_pass" directive is duplicate 错误 - qq_26184091的博客 - CSDN博客 https://blog.csdn.net/qq_26184091/article/details/45419201
[7] Ubuntu安装PHP7 - 程序小工 - 博客园 https://www.cnblogs.com/zqunor/p/8524646.html
[8] nginx 301 将不带www域名,重定向到www域名 - beyond__devil的博客 - CSDN博客 https://blog.csdn.net/beyond__devil/article/details/79880947
[9] Converting rewrite rules Converting rewrite rules
[10] Listen 0.0.0.0:80 Listen [::0]:80 - weixin_34037173的博客 - CSDN博客 https://blog.csdn.net/weixin_34037173/article/details/93885997
[11] centos系统,基于nginx服务器,用https访问php页面总弹出下载页面问题完美解决 - leeten的博客 - CSDN博客 https://blog.csdn.net/jinx_leeten/article/details/51769154
[12] 在Ubuntu 18.04上为Nginx安装phpMyAdmin - 斑驳的岁月 - CSDN博客 https://blog.csdn.net/weixin_39467231/article/details/83958552
[13] 要执行请求的操作,WordPress 需要访问您网页服务器的权限。 请输入您的 FTP 登录XXXX完美解决方法 - Michael Wing's workspace - CSDN博客 https://blog.csdn.net/micwing/article/details/12004987
[14] LNMP添加、删除虚拟主机及伪静态使用教程 - LNMP一键安装包 https://lnmp.org/faq/lnmp-vhost-add-howto.html
[15] linux 批量设置文件夹755 文件644权限 - 正风三才的博客 - CSDN博客 https://blog.csdn.net/tty521/article/details/73123857
[16] linux创建www用户组和用户 http://www.dreamwu.com/post-20.html
[17] Ubuntu基础命令(六)--添加和删除用户和用户组 - Mikowoo007的博客 - CSDN博客 https://blog.csdn.net/Mikowoo007/article/details/84952068
[18] 查找php配置文件php.ini所在路径的二种方法 - 齐泽文的Blog - CSDN博客 https://blog.csdn.net/qq_17054989/article/details/79832507
[19] nginx 配置wordpress固定链接(自定义) - 微笑阳光哈*_* - 博客园 https://www.cnblogs.com/holdon521/p/5675729.html
[20] Ubuntu PHP安装bcmath模块 - GoodByeZ - 博客园 https://www.cnblogs.com/jingjingdidunhe/p/6945355.html
[21] how to install imagemagick for php7 on ubuntu? - Ask Ubuntu https://askubuntu.com/questions/769396/how-to-install-imagemagick-for-php7-on-ubuntu

2019年8月6日更新 shuxueben.cn 出现错误: 413 Request Entity Too Large 我在nginx.conf(/etc/nginx)中添加了一行

client_max_body_size 50m;