用ngnix搭建一个网站 1. 安装nginx: 在yum.repos.d/目录下创建nginx.repo文件并写入(参考nginx官网): yum安装:yum -y install nginx 编写虚拟主机文件:/etc/nginx/conf.d/shopping.conf
server { listen 80; 监听端口 server_name www.mysite.com; 指定网站域名 location / { 用于匹配URI root /usr/share/nginx/html; 指定站点目录 index index.html index.htm; 指定首页文件 } error_page 500 502 503 504 /50x.html; 优雅地显示错误信息 location = /50x.html { root /usr/share/nginx/html; } }2.将网页文件放到站点目录下。 3.重启nginx服务: 检查nginx配置文件语法错误:nginx -t systemctl reload nginx 或者 nginx -s reload
搭建多个网站: 1.编写多个虚拟主机配置文件 /etc/nginx/conf.d/bbs.conf
server { listen 80; 监听端口 server_name bbs.mysite.com; 指定网站域名 location / { 用于匹配URI root /html/bbs; 指定站点目录 index index.html index.htm; 指定首页文件 } error_page 500 502 503 504 /50x.html; 优雅地显示错误信息 location = /50x.html { root /usr/share/nginx/html; } }/etc/nginx/conf.d/blog.conf
server { listen 80; 监听端口 server_name blog.mysite.com; 指定网站域名 location / { 用于匹配URI 只在局部配置有效 root /html/blog; 指定站点目录 index index.html index.htm; 指定首页文件 } error_page 500 502 503 504 /50x.html; 优雅地显示错误信息 location = /50x.html { root /usr/share/nginx/html; } }2.创建站点目录和首页文件
网站的LMNP架构: Linux Mysql nginx:处理静态页面请求 php:处理动态页面请求;和数据库交互。
数据库的安装使用:mariadb是Mysql的分支。 1.安装 2.初始化 执行mysql_install_db –basedir=path The path to the MariaDB installation directory. –datadir=path The path to the MariaDB data directory. –user=user_name The login username to use for running mysqld.
3.给数据库root用户设置密码: mysqladmin -u root password ‘123456’ 测试登录:-u指定用户名,-p参数后直接跟密码,或者不跟密码回车后输入
PHP安装使用: 1.更新yum源(不更新也可以安装但是安装的是最新版本,这里不安装最新版)并卸载系统自带PHP软件 2.yum安装: 3.编写配置文件: vi /etc/php-fpm.d/www.conf nginx和php服务的管理用户保持一致。 将user和group修改成管理nginx服务的用户。nginx将动态服务交给php处理。
4.启动php服务 systemctl start php-fpm
Nginx和php服务建立联系: 编辑虚拟主机文件,通过location区块实现静态请求和动态请求分别处理
server { listen 80; 监听端口 server_name bbs.mysite.com; 指定网站域名 location / { 用于匹配URI root /html; 指定站点目录 index index.html index.htm; 指定首页文件 } location ~ \.php$ { 匹配动态请求 root /html/bbs 站点目录 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass 127.0.0.1:9000; 9000是php-fpm服务端口号 include fastcgi_params; 加载变量配置文件,否则上面的变量$document_root$fastcgi_script_name无法识别。 } }Mysql和php服务建立联系:数据库的连接是在php文件中实现的。 编写一个测试连接的PHP文件: 再访问bbs.mysite.com/bbs/test_mysql.php就可以看到连接成功。
最后可以使用一些开源的网站代码做代码上线测试: 选取wordpress网站代码,将源代码解压到站点目录 修改站点目录权限:修改成nginx管理用户,这里配置的就叫nginx。 登录首页就可以看到页面了: