阿里云服务器 篇一:申请和初始化
阿里云服务器 篇二:搭建静态网站
阿里云服务器 篇三:提交搜索引擎收录
阿里云服务器 篇四:404页面模板
官网:https://yourls.org/
YOURLS是一组强大的PHP脚本,使您能够在您的服务器上运行Your Own URLShortener。使用YOURLS,您可以完全控制您的链接、详细的统计数据、插件支持等,所有这些都包含在一个免费的开源包中。
sudo yum install epel-release -y sudo yum update -y
sudo yum install httpd -y # 启动Apache服务并将其设置为系统启动时自动启动 sudo systemctl start httpd.service sudo systemctl enable httpd.service
修改Apache配置文件:
sudo vi /etc/httpd/conf/httpd.conf
,修改Apache配置文件的Listen 80
为Listen 127.0.0.1:8000
,即只监听本机的8000端口,不直接对外提供服务。
sudo vi /etc/nginx/nginx.conf
,修改nginx配置文件,找到http
块下的server
块,添加转发到8000服务的内容。server块示例:
server { # change to your domain or _ for everything server_name cnfaq.cn www.cnfaq.cn; # root /usr/share/nginx/html; root /var/www/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; # location /css { # alias /usr/share/nginx/html/css; # } # location /js { # alias /usr/share/nginx/html/js; # } location / { try_files $uri $uri/ =404; } # Forward requests to Gunicorn, removing the "/pybackend/" prefix from the request path location /pybackend/ { proxy_pass http://127.0.0.1:8080/; # The trailing slash is used to remove the matched part proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # If the interface supports WebSocket, add the following content # proxy_http_version 1.1; # proxy_set_header Upgrade $http_upgrade; # proxy_set_header Connection "upgrade"; } # Forward requests to Apache, removing the "/apache/" prefix from the request path location /apache/ { proxy_pass http://127.0.0.1:8000/; # The trailing slash is used to remove the matched part proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # If the interface supports WebSocket, add the following content # proxy_http_version 1.1; # proxy_set_header Upgrade $http_upgrade; # proxy_set_header Connection "upgrade"; }
检查mod_rewrite是否已启用:
# 默认应该是启用,并输出显示 mod_rewrite_module (shared) sudo apachectl -M | grep rewrite # 如果没有启用,使用以下命令进行加载 mod_rewrite_module (shared) mod_rewrite_module (shared)
可选:删除Apache欢迎页面:sudo sed -i 's/^/#&/g' /etc/httpd/conf.d/welcome.conf
验证并重新加载nginx配置和重启Apache并查看状态:
sudo nginx -t sudo nginx -s reload sudo systemctl restart httpd.service ps axu | grep -i apache
如果没有安装MySQL,可以参见:阿里云服务器 篇一:申请和初始化相关内容进行安装,不再赘述。
为YOURLS创建数据库:
# 以root身份登录MySQL shell mysql -u root -p # 创建一个数据库yourls、一个数据库用户yourlsuser和数据库用户的密码yourpassword CREATE DATABASE yourls DEFAULT CHARACTER SET UTF8 COLLATE utf8_unicode_ci; CREATE USER 'yourlsuser'@'localhost' IDENTIFIED BY 'yourpassword'; GRANT ALL PRIVILEGES ON yourls.* TO 'yourlsuser'@'localhost' IDENTIFIED BY 'yourpassword' WITH GRANT OPTION; FLUSH PRIVILEGES; EXIT;
# 需要添加 Remi 仓库,这是一个提供最新 PHP 版本的第三方仓库 sudo yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm -y # Remi 仓库提供了多个 PHP 版本,需要明确启用 PHP 8.3 的存储库 sudo yum-config-manager --enable remi-php83 # 安装 PHP 和相关模块 sudo yum install php php-mysqlnd php-common php-cli php-curl php-gd php-json php-mbstring php-opcache php-pdo php-process php-soap php-xml php-xmlrpc php-zip -y # 安装 Apache 的 PHP 模块(在 PHP 7 中,mod_php 不再是默认的 PHP 模块,通常推荐使用 php-fpm) sudo yum install php-fpm -y # 启动并启用服务 sudo systemctl start php-fpm sudo systemctl enable php-fpm # 注:如果以后需要升级到更高版本的php,使用如下命令: # sudo yum-config-manager --disable remi-php83 # sudo yum-config-manager --enable remi-php<新版本号> # sudo yum update <上面的模块列表>
配置 Apache 以启用 php-fpm:
在Apache配置文件(sudo vi /etc/httpd/conf/httpd.conf
)中寻找以下的AddType等关键字,然后更新或添加如下的内容(
DirectoryIndex index.php index.html AddType application/x-httpd-php .php LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so SetHandler "proxy:unix:/run/php-fpm/php-fpm.sock|fcgi://localhost"
注意:Include conf.modules.d/*.conf
语句加载了 proxy_fcgi_module 所需的 proxy_module,所以,LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
应该添加到该行配置之后。
更改Apache根目录为专门的目录:
# 新建 /var/www/php 目录 sudo chmod 777 /var/www mkdir -p /var/www/php # 编辑 Apache 配置文件,更改网站根目录 sudo sed -i -e 's@DocumentRoot "/var/www/html"@DocumentRoot "/var/www/php"@' -e 's@@@' /etc/httpd/conf/httpd.conf # 重启 Apache 生效 sudo systemctl restart httpd
修改nginx的配置,复制一份/apache/的location块,改为/php/,从而让/php/的请求也转发到Apache服务器:
sudo vi /etc/nginx/nginx.conf # 复制location /apache/并修改为location /php/ sudo nginx -t sudo nginx -s reload
新建测试文件,并访问测试:
在 /var/www/php 目录下创建 test.php 文件,并写入以下内容:
然后访问 https://yourdomain/php/test.php
,应该就可以看到php的信息页。它暴露了关于 PHP 安装的详细信息,包括潜在的敏感信息,对于正式环境,在测试之后应该删除。
sudo yum install git -y cd /var/www/php/ sudo git clone https://github.com/YOURLS/YOURLS.git sudo chown -R ecs-user:ecs-user /var/www/php/YOURLS cd YOURLS
配置YOURLS:
cp user/config-sample.php user/config.php
使用vi文本编辑器打开/var/www/php/YOURLS/user/config.php文件:vi user/config.php
找到以下行:
define( 'YOURLS_DB_USER', 'your db user name' ); define( 'YOURLS_DB_PASS', 'your db password' ); define( 'YOURLS_DB_NAME', 'yourls' ); define( 'YOURLS_SITE', 'http://your-own-domain-here.com' ); define( 'YOURLS_COOKIEKEY', 'modify this text with something random' ); $yourls_user_passwords = array( 'username' => 'password',
将它们逐一替换如下:
define( 'YOURLS_DB_USER', 'yourlsuser' ); define( 'YOURLS_DB_PASS', 'yourpassword' ); define( 'YOURLS_DB_NAME', 'yourls' ); define( 'YOURLS_SITE', 'http://example.com' ); define( 'YOURLS_COOKIEKEY', 'fmoi4jfsjfasfjlkfjalfgcggjkihdgfjjgdfolsfmwemlgjhgigjgitjaaewesfsdfsdogmbnsin' ); // Use a long string consists of random characters. $yourls_user_passwords = array( 'username1' => 'password1', // Use your own username and password.
为YOURLS创建虚拟主机:
cat < ServerAdmin admin@example.com DocumentRoot /var/www/html/YOURLS/ ServerName yourls.example.com ServerAlias www.yourls.example.com Options FollowSymLinks AllowOverride All Order allow,deny allow from all ErrorLog /var/log/httpd/yourls.example.com-error_log CustomLog /var/log/httpd/yourls.example.com-access_log common EOF
然后更改Apache配置sudo vi /etc/httpd/conf/httpd.conf
,添加监听本地的8001端口的配置:
Listen 127.0.0.1:8001
然后更改nginx配置sudo vi /etc/nginx/nginx.conf
,为8001端口添加/s/的映射,在/apache/的location下方添加如下配置:
location /s/ { proxy_pass http://127.0.0.1:8001; # No trailing slash is used to KEEP the matched part proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; }
重启nginx、Apache生效配置:
sudo nginx -t sudo nginx -s reload sudo systemctl restart httpd.service
在浏览器访问页面“https://yourdomain/s/admin”,然后单击Install YOURLS链接,根据页面提示进行配置。
如果提示“Could not write file .htaccess in YOURLS root directory. ”,执行vi /var/www/php/YOURLS/.htaccess
,然后添加以下内容:
# BEGIN YOURLS RewriteEngine On RewriteBase / # 移除前缀 /s/ RewriteRule ^s/(.*)$ $1 [NC] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^.*$ /yourls-loader.php [L] # END YOURLS
访问 https://yourdomain/s/admin/ 然后输入用户名密码,即可正常使用。
注意: 我们现在是使用的/s/前缀的方式来使用YOURLS系统,包括生成的短链也是如此。另一种不带/s/前缀的方案是使用二级域名,需要将Apache服务通过二级域名的方式独立出来,当然,这也要绑定二级域名的DNS和申请新的二级域名的SSL证书。
这是因为YOURLS无法编辑和保存你的config.php文件,因为我们为了方便编辑,将文件改为了ecs-user用户权限。
长期解决办法:将用户改为Apache用户权限,但是编辑会不方便:sudo chown -R apache:apache /var/www/php/YOURLS
临时解决办法:临时添加权限:sudo chmod 0666 /var/www/php/YOURLS/user/config.php
,之后还可以将该文件的权限改回去。
编辑YOURLS配置文件:vi /var/www/php/YOURLS/user/config.php
,找到以下关键字并添加zh_CN语言:define( 'YOURLS_LANG', 'zh_CN' );
从这里下载翻译文件:https://github.com/taozhiyu/yourls-translation-zh_CN/releases,例如:wget 'https://github.com/taozhiyu/yourls-translation-zh_CN/archive/refs/tags/V1.9.2.zip'
,然后解压到/var/www/php/YOURLS/user/language
目录下(需要两个文件:zh_CN.mo、zh_CN.po)
cd /var/www/php/YOURLS cp sample-public-front-page.txt public-front-page.php
访问 https://yourdomain/s/public-front-page.php 即可使用。
当短链无效时,会自动跳转到网站首页,即“https://yourdomain/s/”,故我们可以在/var/www/php/YOURLS
目录下新建index.html
文件,用该文件的内容来进行错误提示。
Bookmarklets 本质上是一段包含 JavaScript 代码的书签。用户将其添加到浏览器的书签栏后,通过点击该书签,就可以执行这段 JavaScript 代码所对应的功能,例如快速生成短链接并进行分享。
这样的功能具有很大的便利性和实用性。它简化了生成短链接的过程,节省了时间,并且可以在各种支持添加书签的浏览器中使用。
举例来说,当你在浏览一个网页,想要将该网页的链接缩短时,无需访问 YOURLS 的网站进行操作,只需点击添加好的 YOURLS Bookmarklets,它就会自动使用 YOURLS 服务将当前网页的链接进行缩短,并可能提供相应的短链接供你复制或分享。
参见:https://cnfaq.cn/s/public-front-page.php