如何建立一个网站


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /www/wwwroot/fawdlstty.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /www/wwwroot/fawdlstty.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /www/wwwroot/fawdlstty.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /www/wwwroot/fawdlstty.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /www/wwwroot/fawdlstty.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /www/wwwroot/fawdlstty.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /www/wwwroot/fawdlstty.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /www/wwwroot/fawdlstty.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /www/wwwroot/fawdlstty.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /www/wwwroot/fawdlstty.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /www/wwwroot/fawdlstty.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /www/wwwroot/fawdlstty.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /www/wwwroot/fawdlstty.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /www/wwwroot/fawdlstty.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /www/wwwroot/fawdlstty.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /www/wwwroot/fawdlstty.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /www/wwwroot/fawdlstty.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /www/wwwroot/fawdlstty.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /www/wwwroot/fawdlstty.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /www/wwwroot/fawdlstty.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /www/wwwroot/fawdlstty.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /www/wwwroot/fawdlstty.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

world_wide_web
昨天经过一整天的配置终于把博客搭建好了呢,分享下建站步骤,供后面来的小伙伴们参考学习。由于本人对linux系统不太熟,如有发现错误的地方请指正,谢谢。

1、购买服务器
我选择的是搬瓦工,链接,小伙伴推荐的,貌似还不错,我这个是19.99刀一年的,256M内存10G硬盘,美国亚利桑那州的机房
不太详细的购买教程以及优惠码在这上面能找到http://banwagong.cn/

2、购买域名
我选择的是godaddy,链接,如何购买的这个步骤就不说咯,你要说你给服务商送钱送不出去,我倒不信,十几美刀一年,果断注册了个

3、DNS解析
首先在服务器的管理页面上可以看到自己买的服务器的IP地址,记录下来,然后,点开域名管理界面,开启域名解析。DNS全球同步一般是48h,不过我这次貌似四五个小时就配置好了,这个时间足够我们配置好服务器咯

4、配置服务器
首先装个系统,推荐CentOS6 x86_64,成熟稳定,效果杠杠的

4.1、远程ssh
系统装好后,一般会有一个端口号和一个密码,瞧瞧的告诉你,千万不要告诉任何人哦
这个端口号运行的是ssh服务,到了这步了,接下来,网上去下一个putty客户端,只需要填IP与端口就阔以了。然后,用户名填root,密码填装好系统后显示的密码。这儿密码按了之后是没有******这样的标识的,所以呢,凭感觉输密码吧。显示这样的界面就代表这一步成功咯
putty

4.2、安装基本软件和php
centos、redhat系列使用yum来安装软件,debian、ubuntu系列使用apt-get系列来安装软件。这儿我们用yum吧

1
2
yum -y install pcre ntp vim-enhanced gcc gcc-c++ flex bison autoconf automake bzip2-devel ncurses-devel openssl-devel libtool*zlib-devel libxml2-devel libjpeg-devel libpng-devel libtiff-devel fontconfig-devel freetype-devel libXpm-develgettext-devel curl curl-devel pam-devel e2fsprogs-devel krb5-devel libidn libidn-devel
yum -y install php php-fpm php-mysql php-common php-mbstring php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc

好像很高级的样子,不是很懂呢。反正将上面两条复制到putty然后执行就行咯。记住是一条一条的执行。接下来的是php的配置,第一行代码为启动php服务,第二行代码为设置php自动启动。这是php-cgi方式,容易导致502错误。我之前就是这样设置的,服务器将会隔三四小时就502,当时检查了好久,最后听说女孩不哭也遇到过这种问题,才知道502的原因。所以呐,下面两条仅供欣赏,使用者后果自负

1
2
php-cgi -b 9000 -c /etc/php.ini
echo "php-cgi -b 9000 -c /etc/php.ini" >>/etc/rc.local

除了php-cgi外,还有php-fpm方式,下面两行代码分别为运行php-fpm、设置其开机启动

1
2
/usr/sbin/php-fpm
echo "/usr/sbin/php-fpm" >>/etc/rc.local

4.3、安装MYSQL
执行下面这行代码,意思就是安装php服务咯

1
yum -y install mysql-server

然后配置一下 默认utf-8编码:

1
vim /etc/my.cnf

在文件末尾加两行代码:

1
2
[mysql]
default-character-set = utf8

然后启动服务,以及设置自动启动,然后是设置一个密码,最后是修改密码

1
2
3
4
service mysqld start
chkconfig mysqld on
mysqladmin -u root password 新密码
mysqladmin -u root -p'旧密码' password '新密码'

然后开启MYSQL的远程连接,酱就阔以在自己电脑上直接连接数据库咯

1
2
3
4
mysql -u root -p mysql
grant all privileges on *.* to 'root'@'%' identified by '密码' with grant option;
flush privileges;
select host, user from user;

4.4、安装pcre
这东西有很多坑,下面的代码如果某几行执行不了的不用管,执行后面的,反正执行完就行咯

1
2
3
4
5
6
7
8
9
cp /lib/libpcre.so.0 /
rpm -e --nodeps pcre-7.8-7.el6.x86_64
wget http://downloads.sourceforge.net/project/pcre/pcre/8.38/pcre-8.38.tar.gz?r=https%3A%2F%2Fsourceforge.net%2Fprojects%2Fpcre%2Ffiles%2Fpcre%2F8.38%2F&ts=1457002611&use_mirror=jaist
mv pcre-8.38.tar.gz?r=https:%2F%2Fsourceforge.net%2Fprojects%2Fpcre%2Ffiles%2Fpcre%2F8.38%2F pcre-8.38.tar.gz
tar zxvf pcre-8.38.tar.gz
cd pcre-8.38
cp /libpcre.so.0 /lib/
./configure --prefix=/usr/local/pcre-8.38
make && make install

4.5、安装配置nginx
依次执行下面的代码

1
2
3
4
5
6
wget http://nginx.org/download/nginx-1.9.12.tar.gz
tar xvf nginx-1.9.12.tar.gz
cd nginx-1.9.12
./configure --prefix=/usr/local/nginx --with-pcre=../pcre-8.38
make && make install
cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak

执行之后,nginx就安装完毕咯。

下面分别是运行nginx、重启nginx、设置nginx自启动、修改配置

1
2
3
4
/usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -s reload
echo "/usr/local/nginx/sbin/nginx" >>/etc/rc.local
vi /usr/local/nginx/conf/nginx.conf

nginx的配置对于高手来说简单,但对于新手来说简直如天书。我还是说说这个怎么配。
首先,root参数代表网站根目录。如果把网站搭建到/wwwroot,那么就把所有root html;改为root /wwwroot;
然后是php这段较多的,替换成如下代码

1
2
3
4
5
6
7
location ~ \.php$ {
root /wwwroot;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#user  root;
worker_processes  4;
 
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
 
#pid        logs/nginx.pid;
 
events {
    worker_connections  1024;
}
 
http {
    include       mime.types;
    default_type  application/octet-stream;
 
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
 
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
 
    #access_log  logs/access.log  main;
 
    sendfile        on;
    #tcp_nopush     on;
 
    #keepalive_timeout  0;
    keepalive_timeout  65;
 
    #gzip  on;
 
    server {
        listen       80;
        server_name  fawdlstty.com www.fawdlstty.com;
        location / {
            set $flag 1;
            if ($http_user_agent ~* (baiduspider|soso|sogou|yahoo|sohu-search|yodao|YoudaoBot|robozilla|msnbot|MJ12bot|NHN|Twiceler)){
                set $flag 0;
            }
            if ($flag = 1){
                rewrite ^/(.*)$ https://www.fawdlstty.com/$1 redirect;
            }
 
            root   /wwwroot/blog;
            index  index.html index.htm index.php;
            if (!-e $request_filename){
                rewrite ^/archives/(\d+)\.html$ /index.php?p=$1;
                rewrite ^/([^\.]+)$ /index.php?$1;
            }
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ \.php$ {
            root           /wwwroot/blog;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name; #/scripts$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
 
    # HTTPS server
    #
    server {
        listen       443 ssl;
        server_name  fawdlstty.com www.fawdlstty.com;
 
        ssl_certificate      /.../1_fawdlstty.com_bundle.crt;
        ssl_certificate_key  /.../2_fawdlstty.com.key;
 
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
 
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
 
        location / {
            root   /wwwroot/blog;
            index  index.html index.htm index.php;
            if (!-e $request_filename){
                rewrite ^/archives/(\d+)\.html$ /index.php?p=$1;
                rewrite ^/([^\.]+)$ /index.php?$1;
            }
        }
 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 
        location ~ \.php$ {
            root           /wwwroot/blog;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name; #/scripts$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}

使用以上代码前需要注意以下几点:
1)、替换所有root指向的路径
2)、替换所有server_name所对应的域名。这儿实际上写localhost就可以了,指定具体域名一般是一个nginx服务器多个域名的配置的写法
3)、决定是否启用HTTPS服务,如果需要启用,参考这儿,网站https加密访问实现,如果不启用,将ssl部分全部删掉或注释掉,并将80端口服务的跳转到HTTPS删掉
然后重启一下,基础设施就搭建完成咯

4.6、搭建ftp环境
为方便网站文件管理,ftp还是得搭一个。执行以下命令:

1
2
3
4
5
6
7
yum install vsftpd
service vsftpd restart
chkconfig vsftpd on
/usr/sbin/adduser -d /wwwroot -g ftp -s /sbin/nologin ftp用户名
chmod -R 777 /wwwroot
passwd blog
vi /etc/vsftpd/vsftpd.conf

注意上面的路径是ftp用户默认根目录,也就是ftp目录,倒数第二条是修改ftp的密码,最后一条是修改配置。首先将原配置备份,然后修改如下形式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
xferlog_std_format=YES
#idle_session_timeout=600
#data_connection_timeout=120
#nopriv_user=ftpsecure
async_abor_enable=YES
 
ascii_upload_enable=YES
ascii_download_enable=YES
#ftpd_banner=Welcome to blah FTP service.
#deny_email_enable=YES
#banned_email_file=/etc/vsftpd/banned_emails
 
chroot_local_user=YES
#chroot_list_enable=YES
#chroot_list_file=/etc/vsftpd/chroot_list
ls_recurse_enable=NO
listen=YES
#listen_ipv6=YES
 
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES

重启vsftpd,就成功搭建ftp服务。可惜的是,ftp这服务并不安全,需要将其升级为FTP over TLS,一个更安全的FTP服务,采用非对称加密方式加密。加密前首先生成秘钥,执行以下代码:

1
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /.../vsftpd.pem -out /.../vsftpd.pem

如果执行失败说明你没安装openssl,或者路径都不改一下。这个执行之后有许多提示,输入公司名啊国家名啊什么的,这个随意输,完成之后指定目录下将会生成vsftpd.pem,然后在vsftpd配置文件后面加上以下代码:

1
2
3
4
5
6
7
8
9
sslssl_enable=YES
allow_anon_ssl=NO
force_local_data_ssl=YES
force_local_logins_ssl=YES
ssl_tlsv1=YES
ssl_sslv2=NO
ssl_sslv3=NO
rsa_cert_file=/.../vsftpd.pem
ssl_ciphers=HIGH

rsa_cert_file这儿就是输出的pem文件路径。重启vsftpd,然后访问,就可以发现ftp服务已加密。个人推荐使用FileZilla来上传下载ftp文件,相当方便的说。

4.7、搭建wordpress
这步其实可以通过ftp来完成,不过不知道为啥,我这边比较慢。如果妮可以用ftp完成那么就不用执行下面命令咯

1
2
3
4
5
6
cd /wwwroot/
wget https://cn.wordpress.org/wordpress-4.4.2-zh_CN.tar.gz
tar zxvf wordpress-4.4.2-zh_CN.tar.gz
rm wordpress-4.4.2-zh_CN.tar.gz
mv wordpress/* ./
rmdir wordpress

然后,重启服务器吧

5、一系列的坑
这里做个记录,以免下次遇到后还不知道什么情况

5.1、不时出现502 Bad Gateway
由于之前搭建网站时使用了php-cgi方式,从而导致nginx配置出现某些问题。我另外再改了一些参数,这些在你的博客没出问题前酌情使用
nginx设置超时时长,避免因php文件执行太久导致502或无响应

1
2
3
4
5
vi /usr/local/nginx/conf/nginx.conf
 
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;

设置最大进程数,当时设置这个是为了避免php-cgi进程过多导致502

1
2
echo 'ulimit -HSn 65536' >> /etc/profile
echo 'ulimit -HSn 65536' >> /etc/rc.local

从php-cgi转移到php-fpm后,nginx出现一个问题,执行/usr/local/nginx/sbin/nginx -s reload这行代码会提示nginx: [error] open() "/usr/local/nginx/logs/nginx.pid
这问题通过以下代码解决

1
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

问题是解决了,不过这行代码设置到自启动后会出错。所以呐,暂时也只能在重启服务器后执行一遍咯

5.2、占用内存过大
用了php-fpm之后,内存飙升,一晚的时间,已用内存就从20M升至300M,通过ps -aux命令查询,得知罪魁祸首就是php-fpm。原来这东西有点坑,默认是不释放内存的,所以还得改配置
首先执行vi /etc/php-fpm.conf,看看配置文件,里面有一行代码:include=/etc/php-fpm.d/*.conf,可以获知网站配置文件的目录为/etc/php-fpm.d/,然后再进入目录,执行ls,只有一个www.conf,那么就vi www.conf,找到pm = static这行,改为pm = ondemand,保存退出,重启服务器,然后内存就正常了

5.3、数据库密码出错
这个坑是帮蓝灵侠武龙博客时遇到的,一样的命令不一样的烟火,我也是醉了。效果是登录数据库时密码错误,解决方法如下:

1
2
3
4
5
6
7
service mysqld stop
mysqld_safe --skip-grant-tables &
#输入 mysql -uroot -p 回车进入
use mysql;
update user set password=PASSWORD("newpass")where user="root";
flush privileges;
quit

6、总结
到目前为止共有3个账号密码咯,ssh一个、mysql一个、ftp一个,待会还有wordpress一个,记得把密码保存好哦
搭建服务整个步骤不多,我这种linux新手可是折腾了一整天才搭建好,重装服务器系统不亚于10次,网上的资料太凌乱,所以在这儿做个总结,同时也方便后面的朋友可以少走弯路
最后感谢女孩不哭提供帮助

发布者

fawdlstty

又一只萌萌哒程序猿~~

《如何建立一个网站》上有5条评论

  1. 虽然看不懂但是感觉好厉害不过博客点到别的浏览器标签老是崩溃切回来又好了哎

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注