服务器系统:CentOS 7.7 准备工作
安装编译环境 首先要让安装主机具备编译程序源码的环境。这需要具备C语言、C++语言、Perl语言的编译器,以及各种常见的编译支持函数库程序 因此请先配置妥当软件仓库,然后把下面列出的这些软件包都统统安装上:
[root@iZ0jl0gl3nj3g34ts6cjt ~]# yum -y install apr* autoconf automake numactl bison bzip2-devel cpp curl-devel fontconfig-devel freetype-devel gcc gcc-c++ gd-develgettext-devel kernel-headers keyutils-libs-devel krb5-devel libcom_err-devel libpng-devel libjpeg* libsepol-devel libselinux-devel libstdc++-devel libtool*libxml2-devel libXpm* libxml* libXaw-devel libXmu-devel libtiff* make openssl-devel patch pcre-devel perl php-common php-gd telnet zlib-devel libtirpc-devel gtk* ntpstat na* bison* lrzsz cmake ncurses-devel libzip-devel libxslt-devel gdbm-devel readline-devel gmp-devel ………………省略部分输出信息……………… Complete!
安装LNMP动态网站部署架构所需的4个软件源码包和1个用于检查效果的博客系统软件包
[root@iZ0jl0gl3nj3g34ts6cjt ~]# mkdir /lnmp [root@iZ0jl0gl3nj3g34ts6cjt ~]# cd /lnmp/ #安装LNMP动态网站部署架构的源码包 [root@iZ0jl0gl3nj3g34ts6cjt lnmp]# wget https://www.linuxprobe.com/Software/rpcsvc-proto-1.4.tar.gz [root@iZ0jl0gl3nj3g34ts6cjt lnmp]# wget https://www.linuxprobe.com/Software/nginx-1.16.0.tar.gz [root@iZ0jl0gl3nj3g34ts6cjt lnmp]# wget https://www.linuxprobe.com/Software/wordpress.tar.gz
查看下载好的源码包
[root@iZ0jl0gl3nj3g34ts6cjt lnmp]# ls rpcsvc-proto-1.4.tar.gz nginx-1.16.0.tar.gz wordpress.tar.gz
源码包安装服务程序具有两个优势
1.源码包的可移植性非常好,几乎可以在任何Linux系统中安装使用,而RPM软件包是针对特定系统和架构编写的指令集,必须严格地符合执行环境才能顺利安装(即只会去“生硬地”安装服务程序)
2.使用源码包安装服务程序时会有一个编译过程,因此能够更好地适应安装主机的系统环境,运行效率和优化程度都会强于使用RPM软件包安装的服务程序
编译rcpsvc压缩包 rpcsvc-proto是一款包含rcpsvc协议文件的支持软件包名称,rcpsvc协议在后续Nginx与MySQL服务程序的部署过程中都需要被调用到
[root@iZ0jl0gl3nj3g34ts6cjt lnmp]# tar xzvf rpcsvc-proto-1.4.tar.gz #解压源码包文件 [root@iZ0jl0gl3nj3g34ts6cjt lnmp]# cd rpcsvc-proto-1.4 [root@iZ0jl0gl3nj3g34ts6cjt rpcsvc-proto-1.4]# ./configure #编译源码包代码 [root@iZ0jl0gl3nj3g34ts6cjt rpcsvc-proto-1.4]# make #生成二进制安装程序 [root@iZ0jl0gl3nj3g34ts6cjt rpcsvc-proto-1.4]# make install #运行二进制的服务程序安装包 [root@iZ0jl0gl3nj3g34ts6cjt rpcsvc-proto-1.4]# cd ..
环境搭建
配置Nginx服务
- Nginx是一款相当优秀的用于部署动态网站的轻量级服务程序
- Nginx服务程序的稳定性源自于采用了分阶段的资源分配技术,降低了CPU与内存的占用率
- Nginx具备的模块数量与Apache具备的模块数量几乎相同,而且现在已经完全支持proxy、rewrite、mod_fcgi、ssl、vhosts等常用模块
- Nginx还支持热部署技术,可以7×24小时不间断提供服务,还可以在不暂停服务的情况下直接对Nginx服务程序进行升级
在新建账户时应使用-M参数不创建对应的家目录,以及使用-s参数指定登录后的Shell解释器为/sbin/nologin,确保任何人都不能通过这个账号登录主机
[root@iZ0jl0gl3nj3g34ts6cjt lnmp]# useradd nginx -M -s /sbin/nologin [root@iZ0jl0gl3nj3g34ts6cjt lnmp]# id nginx uid=1000(nginx) gid=1000(nginx) groups=1000(nginx)2.编译安装Nginx网站服务程序
--prefix=/usr/local/nginx指定安装路径
用with-http_ssl_module参数来开启Nginx服务的SSL加密模块,以便日后开启HTTPS协议功能
[root@iZ0jl0gl3nj3g34ts6cjt lnmp]# tar zxvf nginx-1.16.0.tar.gz [root@iZ0jl0gl3nj3g34ts6cjt lnmp]# cd nginx-1.16.0/ [root@iZ0jl0gl3nj3g34ts6cjt nginx-1.16.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module [root@iZ0jl0gl3nj3g34ts6cjt nginx-1.16.0]# make [root@iZ0jl0gl3nj3g34ts6cjt nginx-1.16.0]# make install [root@iZ0jl0gl3nj3g34ts6cjt nginx-1.16.0]# cd .. [root@iZ0jl0gl3nj3g34ts6cjt lnmp]#3.进入最终配置阶段
把第2行的注释符删除,在后面写上负责运行网站服务程序的账户名称和用户组名称
这里假设由nginx用户及nginx用户组负责管理网站服务
[root@iZ0jl0gl3nj3g34ts6cjtyZ lnmp]# vim /usr/local/nginx/conf/nginx.conf
1
2 user nginx nginx;
修改第45行的首页文件名称,在里面添加index.php的名字
这个文件也是让用户浏览网站时第一眼看到的文件,也叫首页文件
43 location / {
44 root html;
45 index index.php index.html index.htm;
46 }
删除第65~71行前面的注释符来启用虚拟主机功能
将第69行后面对应的网站根目录修改为/usr/local/nginx/html
其中的fastcgi_script_name参数用于指代脚本名称,也就是用户请求的URL
只有信息填写正确了,才能使Nginx服务正确解析用户请求,否则访问的页面会提示“404 Not Found”的错误
65 location ~ .php$ {
66 root html;
67 fastcgi_pass 127.0.0.1:9000;
68 fastcgi_index index.php;
69 fastcgi_param script_FILENAME /usr/local/nginx/html$fastcgi_scrip t_name;
70 include fastcgi_params;
71 }
4.将/usr/local/nginx/sbin路径加入到PATH变量中
让Bash解释器在后续执行命令时自动搜索到它。然后在source命令后加载配置文件,让参数立即生效。下次就只需要输入nginx命令即可启动网站服务了
注: 通过编译源码方式安装的服务默认不能被systemctl命令所管理,而要使用Nginx服务本身的管理工具进行操作,相应命令所在的目录是/usr/local/nginx/sbin。使用绝对路径的形式输入命令太麻烦
[root@iZ0jl0gl3nj3g34ts6cjt lnmp]# vim ~/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin:/usr/local/nginx/sbin
export PATH
[root@iZ0jl0gl3nj3g34ts6cjt lnmp]# source ~/.bash_profile #让配置立即生效
[root@iZ0jl0gl3nj3g34ts6cjt lnmp]# nginx
配置Mysql服务 1.在系统中创建一个名为mysql的用户,专门用于负责运行MySQL数据库 记得要把这类账户的Bash终端设置成nologin解释器,避免黑客通过该用户登录到服务器中,从而提高系统安全性
[root@iZ0jl0gl3nj3g34ts6cjt lnmp]# useradd mysql -M -s /sbin/nologin [root@iZ0jl0gl3nj3g34ts6cjt lnmp]# id mysql uid=1001(mysql) gid=1001(mysql) groups=1001(mysql)
2.安装MariaDB数据库
[root@iZ0jl0gl3nj3g34ts6cjt ~]# yum -y install mariadb
3.重启mariadb服务并加入开机启动项
[root@iZ0jl0gl3nj3g34ts6cjt ~]# systemctl restart mariadb [root@iZ0jl0gl3nj3g34ts6cjt ~]# systemctl enable mariadb
4.数据库初始化
[root@iZ0jl0gl3nj3g34ts6cjt ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS script IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none): #输入管理员原始密码,默认为空值,直接回车
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y #y(设置管理员密码)
New password: #输入新的密码
Re-enter new password: #再次输入密码
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y # y(删除匿名账户)
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y #y(禁止管理员从远程登录)
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y #y(删除测试数据库及其访问权限)
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y #y(刷新授权表,让初始化后的设定立即生效)
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
5.切换到mysql数据库
MariaDB [(none)]> use mysql; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed MariaDB [mysql]> show tables; +---------------------------+ | Tables_in_mysql | +---------------------------+ | columns_priv | | db | | event | | func | | general_log | | help_category | | help_keyword | | help_relation | | help_topic | | host | | ndb_binlog_index | | plugin | | proc | | procs_priv | | proxies_priv | | servers | | slow_log | | tables_priv | | time_zone | | time_zone_leap_second | | time_zone_name | | time_zone_transition | | time_zone_transition_type | | user | +---------------------------+ MariaDB [mysql]> create database xiuxian; #新建数据库 Query OK, 1 row affected (0.00 sec)
配置PHP服务
PHP(Hypertxt Preprocessor,超文本预处理器)是一种通用的开源脚本语言,发明于1995年,它吸取了C语言、Java语言及Perl语言的很多优点,具有开源、免费、快捷、跨平台性强、效率高等优良特性,是目前Web开发领域最常用的语言之一
使用源码包的方式编译安装PHP语言环境其实并不复杂,难点在于解决PHP的程序包和其他软件的依赖关系
解压php安装包并编译
[root@iZ0jl0gl3nj3g34ts6cjt lnmp]# tar xvf php-7.3.5.tar.gz [root@iZ0jl0gl3nj3g34ts6cjt lnmp]# cd php-7.3.5 使用prefix参数指定安装路径,使用--with-mysqli等参数开启对数据库的支持模块,为后面在线安装网站做好准备 [root@iZ0jl0gl3nj3g34ts6cjt php-7.3.5]# ./configure --prefix=/usr/local/php --enable-fpm --with-mysqli --with-curl --with-pdo_mysql --with-pdo_sqlite --enable-mysqlnd --enable-mbstring --with-gd 生成二进制文件并进行安装,时间大约为10~20分钟 [root@iZ0jl0gl3nj3g34ts6cjt php-7.3.5]# make [root@iZ0jl0gl3nj3g34ts6cjt php-7.3.5]# make install
将生成出的php服务配置文件复制到安装目录中(/usr/local/php/) 还需要php-fpm的配置文件,在/usr/local/php/etc/目录中已经提供,只需要复制模板
[root@iZ0jl0gl3nj3g34ts6cjt php-7.3.5]# cp php.ini-development /usr/local/php/lib/php.ini [root@iZ0jl0gl3nj3g34ts6cjt php-7.3.5]# cd /usr/local/php/etc/ [root@iZ0jl0gl3nj3g34ts6cjt etc]# mv php-fpm.conf.default php-fpm.conf
复制一个模板文件到php-fpm.d的目录中,用于后续控制网站的连接性能
[root@iZ0jl0gl3nj3g34ts6cjt etc]# mv php-fpm.d/www.conf.default php-fpm.d/www.conf.conf
把php服务加入到启动项中,使其重启后依然生效
[root@iZ0jl0gl3nj3g34ts6cjt php-7.3.5]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm [root@iZ0jl0gl3nj3g34ts6cjt php-7.3.5]# chmod 755 /etc/init.d/php-fpm
编辑php.ini配置文件,在第310行的disable_functions参数后面追加上要禁止的功能
[root@iZ0jl0gl3nj3g34ts6cjt php-7.3.5]# vim /usr/local/php/lib/php.ini 307 ; This directive allows you to disable certain functions for security reasons. 308 ; It receives a comma-delimited list of function names. 309 ; http://php.net/disable-functions 310 disable_functions = passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_ope n,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlin k,popepassthru,stream_socket_server
启动php服务
[root@iZ0jl0gl3nj3g34ts6cjt php-7.3.5]# /etc/init.d/php-fpm start Starting php-fpm done
搭建Wordpress博客
WordPress是一个以PHP和MySQL为平台的开源博客软件,具有丰富的插件和模板系统,截止于2021年5月,全球排名前1000万的网站中已有超过41%使用,是当前最受欢迎的网站内容管理系统
清空Nginx服务程序根目录的内容
[root@iZ0jl0gl3nj3g34ts6cjtyZ php-7.3.5]# cd .. [root@iZ0jl0gl3nj3g34ts6cjtyZ lnmp]# rm -f /usr/local/nginx/html/* [root@iZ0jl0gl3nj3g34ts6cjtyZ lnmp]# tar xzvf wordpress.tar.gz [root@iZ0jl0gl3nj3g34ts6cjtyZ lnmp]# mv wordpress/* /usr/local/nginx/html/
设置目录所有身份及可读写的权限
[root@iZ0jl0gl3nj3g34ts6cjtyZ lnmp]# chown -Rf nginx:nginx /usr/local/nginx/html/ [root@iZ0jl0gl3nj3g34ts6cjtyZ lnmp]# chmod -Rf 777 /usr/local/nginx/html/
输入本机IP访问网站首页



