基于LNMP/LAMP架构的完整建站指南。
我将会在这里讲解两种架构的网站搭建,这两种架构分别是LNMP和LAMP。
首先讲一下,LNMP和LAMP的他们都是什么。
“L”的意思就是Linux,他是一个操作系统。
“N”的意思就是Nginx,“A”的意思是Apache,他们都是Web程序,Nginx是目前最火热的,很多网站都在采用,Apache的话是一个老牌Web程序,搭建起来十分简单。
“M”的意思就是Mariadb或者MySQL,这是数据库程序,因为写软件的作者都是一个人,所以操作方法几乎一模一样,MySQL是收费软件,而 Mariadb 是开源免费的,所以这里讲Mariadb。
“P”的意思就是PHP,这是给Web页面提供支持的,因为有很多的Web页面都是用PHP写的。
现在先准备“L”,这里我选择的操作系统是Centos7(对于这个架构而言小版本号无需在意),我也推荐这个操作系统,现在的话服务器主要采用的都是Centos6或者Centos7,但随着时间的推移以后肯定是Centos7越来越多。
接下来是“N”,搭建Nginx。
首先我们去Nginx官网下载Nginx源码准备编译安装,直接下载到Linux服务器上,具体参考命令如下。
http://nginx.org/
wget http://nginx.org/download/nginx-1.14.2.tar.gz
如果运行程序出错或者说命令不存在请使用如下命令安装这个下载工具,同时在这里安装以后可能会用到的编译软件和其他程序。
yum -y install wget vim gcc openssl-devel pcre-devel zlib-devel
程序下载完成过后开始编译安装。(如果需要隐藏Nginx程序的版本号和程序名参考: https://teddyou.com/?p=14 )
创建一个无法登录的用户提升安全性。
useradd -s /sbin/nologin nginx
解压Nginx程序并进入程序主目录:
tar xf nginx-1.14.2.tar.gz cd nginx-1.14.2
指定用户、用户组、安装目录、需要安装的模块,禁用不安全的模块,编译并安装:
./configure --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module \ --with-stream --without-http_autoindex_module --without-http_ssi_module && make && make install
利用系统开机脚本并且赋予权限,设置Nginx开启启动:
echo ‘/usr/local/nginx/sbin/nginx’ >> /etc/rc.d/rc.local chmod +x /etc/rc.d/rc.local
启动Nginx程序。
/usr/local/nginx/sbin/nginx
也都看到了,Nginx的启动路径比较长,所以我们添加一个快捷方式,以后我们直接使用命令“nginx”就可以启动程序。
ln -s /usr/local/nginx/sbin/nginx /sbin/nginx
停止Nginx:
nginx -s stop
重新载入Nginx配置文件:
nginx -s reload
到这里,Nginx的安装就完成了,如果启动服务报错,则考虑因素为:
Nginx默认占用的端口是80端口,考虑是否存在端口占用问题。用以下命令查看,找到占用端口的程序,关闭或杀死其进程。
ss -nutlp|grep :80
这里讲解安装Apache程序,安装过程十分简单,只需要一个命令:
yum -y install httpd
启动/重启Apache命令如下:
systemctl restart httpd
关闭Apache:
systemctl stop httpd
设置开启Apache启动:
systemctl enable httpd
取消Apache开机启动:
systemctl disable httpd
同样的,如果启动报错依然考虑80端口问题,没有改动任何配置文件的情况下不考虑配置文件的问题。用同样的方法去排查错误,另外,Nginx和Apache在不修改默认端口的情况下只能存在一个!
接下来是“M”,配置数据库。MySQL的安装较为复杂,需要去官网下载安装包,这里也不做讲解。
使用以下命令安装Mariadb数据库。
yum -y install mariadb mariadb-server
用和刚刚Apache的方法启动Mariadb数据库并设置开机启动:
systemctl start mariadb systemctl enable mariadb
这里使用最简单的方法对数据库进行一键初始化配置,在配置过程中任何询问输入”y”,注意在这个过程中需要设置数据库的root密码。(注意,此root非系统用户root,而是数据库的管理员,并非Linux系统管理员root!)
mysql_secure_installation
其他数据库操作参考: https://teddyou.com/search.php?q=mysql
到最后就是“P”的配置了,针对Nginx环境和Apache环境,他们的php配置方法有所不同。
先是Nginx的php程序配置,安装所需要的软件包:
yum -y install php-fpm php-mysql
启动服务,并设置开机启动:
systemctl start php-fpm systemctl enable php-fpm
php-fpm提供php语言解释且需要占用9000端口,php-mysql是数据库关联程序。
修改Nginx配置文件,使其支持php页面:
vim /usr/local/nginx/conf/nginx.conf //在http中写入或修改为以下内容
server { listen 80; server_name localhost; index index.php; charset "utf-8"; root html/; //指定网站目录 location ~ \.php$ { root html/; //指定网站目录 fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } }
重新载入Nginx配置文件:
nignx -s reload
Nginx的php配置完毕,这里是Apache的配置方法,执行以下命令即可,无需其他操作:
yum -y install php php-bcmath php-cli php-common php-gd php-ldap php-mbstring php-mysqlnd php-pear php-pdo php-xml php-xmlrpc
到这里,LNMP/LAMP的架构就全部配置完毕了,但配置完成过后还没有网站程序,所以接下来就配置网站程序。
这里选择破有名气的 WordPress(官网:https://cn.wordpress.org/ ) ,这个程序不论是制作公司首页还是个人博客都毫无压力。强大的插件功能和个性化主题都是选择他的理由之一。
架构搭好以后放置程序就非常简单了,只需要下载解压到指定目录就行了。
wget https://cn.wordpress.org/latest-zh_CN.tar.gz
解压程序:
tar xf latest-zh_CN.tar.gz
移动主程序目录:
cp -rp ./wordpress /usr/local/nginx/html/
授予权限(因为php程序的默认用户是apache,所以授予权限为apache):
chown -R apache:apache /usr/local/nginx/html/
然后就可以访问了,首次访问会进行初始化配置,需要配置数据库地址和帐户,因为是本地数据库没有分离来做,所以地址是localhost,帐户密码可以进入数据库新建库表并创建对此表有权限的用户来提升安全性,如果不会的话就用root用户也可以。
访问路径应该是:http://服务器IP地址/wordpress
注意! wordpress 可能在使用中会出现需要FTP的问题,绕过FTP的解决方案参考合作站点 https://eternalcenter.com/?p=1396
下面附上一个经过调优打开SSL功能的且较为完整的Nginx配置参考。
#user nobody; worker_processes auto; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 20000; } http { sendfile on; keepalive_timeout 65; include mime.types; default_type application/octet-stream; #gzip压缩 gzip on; gzip_min_length 1000; gzip_comp_level 4; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; #防攻击模块 limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; #定义缓冲区大小 client_body_buffer_size 1k; client_header_buffer_size 1k; client_max_body_size 4m; large_client_header_buffers 4 4k; #ouyang_blog server { listen 80; server_name teddyou.com; rewrite ^/(.*)$ https://teddyou.com/$1; } server { listen 80; server_name *.teddyou.com; rewrite ^/(.*)$ https://teddyou.com/$1; } #ouyang_blog_ssl server { listen 443 ssl; server_name teddyou.com; index index.php; charset "utf-8"; root ****; limit_req zone=one burst=5; #sll加密 ssl_certificate /****/1_teddyou.com_bundle.crt; ssl_certificate_key /****/2_teddyou.com.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; #错误页面 error_page 404 400 402 403 /400.html; error_page 401 /401.html; error_page 500 501 502 /500.html; location = /400.html { root /usr/local/****; } location = /500.html { root /usr/local/****; } location = /401.html { root /usr/local/****; } #PHP本地解释器调用 location ~ \.php$ { root ****; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } #CSS调用 location ~ \.css$ { add_header Content-Type text/css; } #定义客户端缓存 location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ { expires 30d; } #拒绝非法请求 if ( $request_method !~ ^(GET|POST)$ ) { return 401; } } #错误页面调用信息 server { listen 80; server_name err.teddyou.com; charset "utf-8"; root ****; location ~ \.css$ { add_header Content-Type ./css; } error_page 404 400 402 403 /400.html; error_page 401 /401.html; error_page 500 501 502 /500.html; location = /400.html { root ****; } location = /500.html { root ****; } location = /401.html { root ****; } #定义客户端缓存 location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ { expires 30d; } } #ouyang_old server { listen 80; server_name test.teddyou.com; index index.php; charset "utf-8"; root ****; location ~ \.php$ { root ****; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } location ~ \.css$ { add_header Content-Type text/css; } } #Apache本地调度 upstream teddyou.cn{ server localhost:4443; } server { listen 80; server_name *.teddyou.cn teddyou.cn; rewrite ^/(.*)$ https://teddyou.com/$1; location / { proxy_pass https://teddyou.cn; } limit_req zone=one burst=5; location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ { expires 30d; } if ($request_method !~ ^(GET|POST)$) { return 401; } } }
发表评论