在系统装好后,推荐将电脑的命名一个言简意赅的短小名称,这样在Linux shell或Windows命令行下,看着更舒服,特别是输入的内容较长时,效果会更明显。
最近被安排接手PHP项目,由于没接触过,所以做个记录整理,便于以后查看。不管做什么开发,搭建环境是第一步。
1. XMAPP 是Apache+MySQL+PHP+PERL的简写,支持Linux、Windows、OS X等多个操作系统。下载前先要弄清楚PHP、MySQL等的版本,根据需求下载相应的版本,会让你少碰到许多莫名其妙的问题。
2. LNMP 是Linux+Nginx+MySQL+PHP的简写,顾名思义,仅支持Linux。优势是在安装时才选择你需要的PHP、MySQL等软件的版本。
由于生产环境大部分是Linux,所以推荐使用LNMP搭载运行环境。最近使用Windows系统,发现WSL2(Windows Subsytem linux),尝试安装了下,感觉比较好用,所以给予WSL2搭建运行环境。
一般开发人员的电脑应该都能满足要求,如果以管理员身份运行powershell,执行如下命令,出现问题,建议参考微软官网文章: 旧版WSL的手动安装步骤 ,检查是否满足要求,或修复问题。
a). 启用适用于 Linux 的 Windows 子系统
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
b). 启用虚拟机功能
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
重启电脑,切记,一定要重启电脑。
c). 下载并安装Linux 内核更新包
d). 升级,重要的一步,一定要做
wsl --update
e). 检查版本
wsl --version
f). 将 WSL 2 设置为默认版本
wsl --set-default-version 2
特别注意:如果是Android开发人员,不要安装Android Emulator Hypervisor driver,否则会导致WSL2启动失败。
微软应用商店可以选择不同版本的Ubuntu下载安装,当然也可以选择支持的其他Linux系统。在装完后, 从开始菜单搜索选择你安装的Linux系统,第一次启动时,会慢一点要求输入用户名和密码,同样建议用户名,字符少些。设置完密码后,首先执行如下命令,更新软件源并升级。
由于网络的问题,最好更新软件源使用国内的镜像站点
sudo sed -i 's/http:\/\/archive.ubuntu.com/http:\/\/mirrors.aliyun.com/g' /etc/apt/sources.list
阿里云: http://mirrors.aliyun.com
网易: http://mirrors.163.com
清华大学: https://mirrors.tuna.tsinghua.edu.cn()
中国科学技术大学: https://mirrors.ustc.edu.cn
sudo apt update && sudo apt upgrade -y
Ubuntu的默认安装路径:%USERPROFILE%\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu22.04LTS_*
Windows访问Linux: \\wsl.localhost\Ubuntu-22.04\home\Linux下的用户名
Linux访问Windows: /mnt/c/Users/Windows下的用户名
安装MongoDB
sudo apt install gnupg curl
curl -fsSL https://pgp.mongodb.com/server-6.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-6.0.gpg --dearmor
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-6.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
sudo apt-get update
sudo apt-get install -y mongodb-org
sudo systemctl status mongod
sudo systemctl enable mongod
Created symlink /etc/systemd/system/multi-user.target.wants/mongod.service → /lib/systemd/system/mongod.service.
sudo systemctl restart mongod
mongosh
Current Mongosh Log ID: 64db75ad738e930a5d3fce25
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.10.5
Using MongoDB: 6.0.9
Using Mongosh: 1.10.5
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
------
The server generated these startup warnings when booting
2023-08-15T20:54:53.590+08:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
2023-08-15T20:54:54.418+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2023-08-15T20:54:54.419+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
2023-08-15T20:54:54.419+08:00: vm.max_map_count is too low
------
test> db.version()
6.0.9
test> exit
卸载MongoDB
sudo service mongod stop
sudo apt-get purge mongodb-org*
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb
安装RabbitMQ
sudo add-apt-repository -y ppa:rabbitmq/rabbitmq-erlang
wget -qO - https://packagecloud.io/install/repositories/rabbitmq/rabbitmq-server/script.deb.sh | sudo bash
sudo apt install -y rabbitmq-server
sudo rabbitmq-plugins enable rabbitmq_management
sudo rabbitmq-diagnostics server_version
sudo service rabbitmq-server status
sudo service rabbitmq-server restart
sudo systemctl enable rabbitmq-server
sudo rabbitmqctl add_user admin admin
sudo rabbitmqctl set_user_tags admin administrator
sudo rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"
sudo rabbitmqctl version
3.12.2
http://localhost:15672
安装Redis Server
sudo add-apt-repository ppa:redislabs/redis
sudo apt update
sudo apt install redis-server
redis-cli -h 127.0.0.1 -p 6379 ping
sudo systemctl restart redis-server
ss -an | grep 6379
redis-server -v
Redis server v=7.0.12 sha=00000000:0 malloc=jemalloc-5.2.1 bits=64 build=b56540d7933227fd
由于网络的问题,一般国内下载数据库安装文件比较慢,推荐自己单独下载。下载地址:https://mariadb.org/download/?t=mariadb&o=true&p=mariadb&r=10.4.29&os=Linux&cpu=x86_64&i=systemd
下载LNMP的完整版安装包,注意不要以为完整版,就包含数据库,https://www.lnmp.org/download.html
下载后,拷贝到下面的目录,其它目录也可以:Linux: \\wsl.localhost\Ubuntu-22.04\home\Linux下的用户名,然后解压
tar zxvf lnmp2.0-full.tar.gz
切换到解压后的目录
cd lnmp2.0-full/
配置离线安装
vi lnmp.conf
Download_Mirror='https://soft.vpser.net'
CheckMirror='n'
Nginx_Modules_Options=''
PHP_Modules_Options=''
##MySQL/MariaDB database directory##
MySQL_Data_Dir='/usr/local/mysql/var'
MariaDB_Data_Dir='/usr/local/mariadb/var'
##Default website home directory##
Default_Website_Dir='/home/wwwroot/default'
Enable_Nginx_Openssl='y'
Enable_Nginx_Lua='n'
Enable_Ngx_FancyIndex='n'
Enable_Swap='y'
Enable_PHP_Exif='n'
Enable_PHP_Fileinfo='n'
Enable_PHP_Ldap='n'
Enable_PHP_Bz2='n'
Enable_PHP_Sodium='n'
Enable_PHP_Imap='n'
将之前下载的数据库,拷贝到\\wsl.localhost\Ubuntu-22.04\home\cc\lnmp2.0-full\src
如果要修改要安装的数据库版本,可以编辑 \\wsl.localhost\Ubuntu-22.04\home\cc\lnmp2.0-full\include\main.sh
由于近年PHP使用MariaDB较多,推荐数据库选择MariaDB,根据需要选择版本即可。
执行安装命令
sudo ./install.sh lnmp
安装完成后,一定要注意查看
Checking ...
Nginx: OK
MariaDB: OK
PHP: OK
PHP-FPM: OK
Install lnmp V2.0 completed! enjoy it.
启动MariaDB服务
sudo service mariadb restart
测试MySQL是否启动成功,如果设置了密码,输入密码,否则回车即可。
mysql -h localhost -P 3306 -uroot -p
启动Nginx
sudo service nginx restart
启动FastCGI(FastCGI Process Manager)进程管理器
sudo service php-fpm restart
测试是否启动成功
在浏览器中,访问: http://localhost/, http://localhost/p.php, http://localhost/phpinfo.php
如果你不知道有哪些服务及服务名称怎么办呢,执行如下命令即可知道
sudo service --status-all
如果不知道nginx的配置文件在哪,执行如下命令
sudo ps -aux | grep nginx
Nginx配置文件: /usr/local/nginx/conf/nginx.conf
Nginx虚拟站点: /usr/local/nginx/conf/vhost/
PHP配置文件: /usr/local/php/etc/php.ini
默认站点: /home/wwwroot/default
修改php配置
sudo vi /usr/local/php/etc/php.ini
display_errors = On
display_startup_errors = On
# 禁用函数不建议删除全部禁用函数,但作为小白,这样操作最省事
disable_functions =
然后我们就可以在默认站点下添加修改php代码了。
安装MonoDB的PHP扩展
wget http://pecl.php.net/get/mongodb-1.16.1.tgz
tar zxfv mongodb-1.16.1.tgz
cd mongodb-1.16.1/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
sudo make
sudo make install
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20220829/
安装Redis的PHP扩展
wget http://pecl.php.net/get/redis-5.3.7.tgz
tar zxfv redis-5.3.7.tgz
cd redis-5.3.7/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
sudo make
sudo make install
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20220829/
安装RabbitMQ的PHP扩展
首先从https://github.com/alanxz/rabbitmq-c,下载zxf rabbitmq-c-0.13.0.tar.gz
tar zxf rabbitmq-c-0.13.0.tar.gz
cd rabbitmq-c-0.13.0/
mkdir build
cd build
cmake ..
sudo make install
-- Installing: /usr/local/lib/librabbitmq.so.0.13.0
-- Installing: /usr/local/lib/librabbitmq.so.4
-- Set runtime path of "/usr/local/lib/librabbitmq.so.0.13.0" to "$ORIGIN"
-- Installing: /usr/local/lib/librabbitmq.so
-- Installing: /usr/local/lib/librabbitmq.a
-- Installing: /usr/local/include/amqp.h
-- Installing: /usr/local/include/amqp_framing.h
-- Installing: /usr/local/include/amqp_tcp_socket.h
-- Installing: /usr/local/include/amqp_ssl_socket.h
-- Installing: /usr/local/include/rabbitmq-c/amqp.h
-- Installing: /usr/local/include/rabbitmq-c/framing.h
-- Installing: /usr/local/include/rabbitmq-c/tcp_socket.h
-- Installing: /usr/local/include/rabbitmq-c/ssl_socket.h
-- Installing: /usr/local/include/rabbitmq-c/export.h
-- Installing: /usr/local/lib/cmake/rabbitmq-c/rabbitmq-c-config.cmake
-- Installing: /usr/local/lib/cmake/rabbitmq-c/rabbitmq-c-config-version.cmake
-- Installing: /usr/local/lib/cmake/rabbitmq-c/rabbitmq-targets.cmake
-- Installing: /usr/local/lib/cmake/rabbitmq-c/rabbitmq-targets-release.cmake
-- Installing: /usr/local/lib/pkgconfig/librabbitmq.pc
wget http://pecl.php.net/get/amqp-1.11.0.tgz
tar zxfv amqp-1.11.0.tgz
cd amqp-1.11.0/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
sudo make
sudo make install
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20220829/
配置PHP扩展
sudo vi /usr/local/php/etc/php.ini
extension=redis
extension=mongodb
extension=amqp
在windows命令行执行 wsl --shutdown关闭Ubuntu
重新进入Ubuntu,启动服务
sudo service mariadb restart
sudo service php-fpm restart
sudo service nginx restart
访问: http://localhost/phpinfo.php,看到下面这些内容说明安装扩展成功。
参考:
https://learn.microsoft.com/zh-cn/windows/wsl/tutorials/wsl-database
https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-ubuntu/
标签:php,入门,--,sudo,rabbitmq,usr,PHP,local,搭建 From: https://www.cnblogs.com/terry-cc/p/17708957.html