时间:2023-05-25 01:39:02 | 来源:网站运营
时间:2023-05-25 01:39:02 来源:网站运营
小白学习之路-从零开始用SSH在Linux服务器上构建Wordpress网站:最近学习网站开发,由最简单的东西开始一步一步入门,不知不觉一个月间就学到了我难以想象多的知识. 虽然Wordpress会被一些高端开发者鄙视, 尤其是它是基于PHP的(其实我也不适应,因为我是主要学习Python研发的), 但是作为一个领你入门的师父,Wordpress毫无疑问是把你从零基础领到高端的好师父! 本文就是我这几天以Wordpress为契机,研究在各种网络服务器的成果. 内置一些常见的坑及跳坑的方法.要搭建wordpress,首先得有个VPS吧.
SSH
命令就可以了.Putty.exe
.用它就ok.root@username:~# sudo apt-get install php5-fpm nginx mysql-server php5-mysql虽然不知道这几个依赖包的顺序有什么讲究(看起来好像有一点点逻辑性?).
service nginx start
,就启动了nginx服务器.Welcome to nginx!
好方便!!/var/www/html
,所以很好找.wget http://wordpress.org/latest.zip
.unzip
解压(提示没有unzip程序,则apt-get install unzip
).chmod 777 -R /var/www/html/wpblog
, 777代表最高权限.chown
,虽然我不懂区别,但也没发现有什么问题.mysql -u root -p
登进mysql,然后输入密码,进入后create database 数据库名;
,就OK了.wp-config.php
文件了.http://abc.com/wpblog/
后,会出现403 Forbidden
字样./etc/nginx/sites-available/
下的default
文件.default-backup
文件.default
文件,遵照nginx语法改了几行.root@username:~# sudo service nginx restartJob for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
然后不管是start还是restart都是这句话.纳闷了.就按照出错指示,输入了systemctl status nginx.service
:root@username:~# systemctl status nginx.service nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: failed (Result: exit-code) since Tue 2020-10-23 08:27:51 EST; 22s ago Process: 8508 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS) Process: 8609 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=1/FAILURE) Main PID: 7194 (code=exited, status=0/SUCCESS)
完全看不懂...当然后来也证明,这个对我排除错误完全没作用.于是又照着指示输入了第二个命令:root@username:~# journalctl -xe-- Subject: Session 12 has been terminated-- Defined-By: systemd-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel-- Documentation: http://www.freedesktop.org/wiki/Software/systemd/multiseat---- A session with the ID 12 has been terminated.Oct 23 08:33:44 username sshd[8792]: Connection closed by 120.92.72.31 [preauth]Oct 23 08:33:45 username sshd[8770]: pam_unix(sshd:session): session closed for user rootOct 23 08:33:45 username systemd-logind[551]: Removed session 13.-- Subject: Session 13 has been terminated-- Defined-By: systemd-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel-- Documentation: http://www.freedesktop.org/wiki/Software/systemd/multiseat---- A session with the ID 13 has been terminated.Oct 23 08:34:06 username sshd[8795]: Connection closed by 120.92.72.31 [preauth]Oct 23 08:34:54 username sshd[8797]: Connection closed by 106.187.100.177 [preauth]Oct 23 08:35:16 username sshd[8799]: Connection closed by 120.92.72.31 [preauth]Oct 23 08:35:35 username sshd[8801]: Connection closed by 120.92.72.31 [preauth]Oct 23 08:36:33 username sudo[8806]: root : TTY=pts/1 ; PWD=/etc/nginx/sites-available ; USER=Oct 23 08:36:33 username sudo[8806]: pam_unix(sudo:session): session opened for user root by root(Oct 23 08:36:33 username sudo[8806]: pam_unix(sudo:session): session closed for user rootOct 23 08:36:48 username sudo[8829]: root : TTY=pts/1 ; PWD=/etc/nginx/sites-available ; USER=Oct 23 08:36:48 username sudo[8829]: pam_unix(sudo:session): session opened for user root by root(Oct 23 08:36:48 username sudo[8829]: pam_unix(sudo:session): session closed for user rootOct 23 08:36:50 username sshd[8852]: Connection closed by 120.92.72.31 [preauth]Oct 23 08:37:25 username sshd[8854]: Connection closed by 120.92.72.31 [preauth]Oct 23 08:37:57 username sshd[8856]: Connection closed by 101.200.80.28 [preauth]Oct 23 08:38:03 username sshd[8858]: Connection closed by 120.92.72.31 [preauth]
换着关键词关键句搜了好几种,才搜出来一种答案比较多的.root@username:~# nginx -tnginx: [emerg] a duplicate default server for 0.0.0.0:80 in /etc/nginx/sites-enabled/default-backup: 17nginx: configuration file /etc/nginx/nginx.conf test failed
就两句话,然后都了一下,眼睛瞪大--恍然大悟!nginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful
简单两句,看着真给力.server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; index index.html index.htm index.nginx-debian.html; server_name _; location / { try_files $uri $uri/ =404; }}
之前看过两三集的nginx教程, 有那么一点点印象而已.新手学东西不能怕错,但是不能少了猜.如果有人看到这里,也请不要嫌我业余.
listen 80 default_server; 好像是说默认服务器会监听80端口.然后就可以开始正式学习nginx语法了.
但listen [::]:80 default_server;就不知道了,看下一句.
root /var/www/html; 这个好理解,那就是网站(也叫站点, 应用, app等)的根目录位置.
看到一些老视频,好像过去nginx的默认根目录在/usr/share/html这里.
新nginx改到这里更好,和apache一样,省得去记别的了.
server_name _; 这个,估计是站点名字或者网址吧?
location / {...} 这可能定义根目录的一些url地址问题?
try_files $url $url/ =404, 这个还真有点不好猜,只知道估计是和404文件未找到有关.
# Upstream 用来抽象化php的后端连接upstream php { server unix:/tmp/php-cgi.socket; server 127.0.0.1:9000;}server { ## 把你的网址放在这里,如baidu.com server_name baidu.com; ## 只此一处的站点根目录地址的声明 root /var/www/wordpress; ## 这句应该写在http区块(类似http {...}这样的)里, ## 如果你写了http区块,就没必要放在这. index index.php; ## (设置网站favicon.ico图标的访问逻辑,) location = /favicon.ico { # (如果没找到这个文件,则不适用规则) log_not_found off; access_log off; } ## (设置robots.txt的访问逻辑) ## (允许所有对它的访问(因为每个访问者都应该遵守嘛)) location = /robots.txt { allow all; # (如果没找到这个文件,则不适用规则) log_not_found off; access_log off; } ## (根目录的访问逻辑. 注:"/"代表根目录) location / { # 这句话巨酷, 因为没有php会被当成静态文件访问 # 加入"?$args"这部分后, 非默认的固定链接在使用url查询字串时就不会中断 try_files $uri $uri/ /index.php?$args; } ## (目录中所有.php文件的访问逻辑) location ~ /.php$ { #注意: 你应该在php.ini中写了"cgi.fix_pathinfo = 0;" 这句话. # (引入fastcgi.conf这个文件) include fastcgi.conf; # (fastcgi相关的报错:开) fastcgi_intercept_errors on; fastcgi_pass php; } ## (目录中所有的js|css|png|jpg|jpeg|gif|ico文件访问逻辑) location ~* /.(js|css|png|jpg|jpeg|gif|ico)$ { ## 最大过期期限 expires max; ## 如果没有找到这个文件 则不适用规则 log_not_found off; }}
上面的中文是我自己翻译的, 带括号的注释中是我根据字面意思猜测的.location /wordpress { try_files $uri $uri/ /wordpress/index.php?$args;}location ~ /.php$ { fastcgi_split_path_info ^(/wordpress)(/.*)$;}
上面说是,如果你想把wordpress作为一个子目录(而不是网站的根目录)使用,那你应该在上面总配置中相应位置加上这几句代码.map $uri $blogname{ ~^(?P<blogpath>/[^/]+/)files/(.*) $blogpath ;}map $blogname $blogid{ default -999; #Ref: http://wordpress.org/extend/plugins/nginx-helper/ #include /var/www/wordpress/wp-content/plugins/nginx-helper/map.conf ;}server { server_name example.com ; root /var/www/example.com/htdocs; index index.php; location ~ ^(/[^/]+/)?files/(.+) { try_files /wp-content/blogs.dir/$blogid/files/$2 /wp-includes/ms-files.php?file=$2 ; access_log off; log_not_found off; expires max; } #avoid php readfile() location ^~ /blogs.dir { internal; alias /var/www/example.com/htdocs/wp-content/blogs.dir ; access_log off; log_not_found off; expires max; } if (!-e $request_filename) { rewrite /wp-admin$ $scheme://$host$uri/ permanent; rewrite ^(/[^/]+)?(/wp-.*) $2 last; rewrite ^(/[^/]+)?(/.*/.php) $2 last; } location / { try_files $uri $uri/ /index.php?$args ; } location ~ /.php$ { try_files $uri =404; include fastcgi_params; fastcgi_pass php; } #add some rules for static content expiry-headers here}
这里官网的注释解释不多,而且很难字面上理解了, 所以先跳过.# Upstream to abstract backend connection(s) for phpupstream up_php { server unix:/var/run/php5-fpm.sock; server 127.0.0.1:9000;}server { server_name _; root /var/www/html; index index.php; location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { allow all; log_not_found off; access_log off; } location / { try_files $uri $uri/ /index.php?$args; } location ~ /.php$ { include fastcgi.conf; fastcgi_pass up_php; } location ~* /.(js|css|png|jpg|jpeg|gif|ico)$ { expires max; log_not_found off; }}
然后重启nginx.哒哒哒!经典的wordpress配置页面出现啦!剩余步骤30秒内全部完成!哈哈,太高兴了.关键词:服务,学习